Archive for the ‘VB.Net’ Category

How to restart application in vb.net

Use application.restart

,

No Comments


Exception: Thread was being aborted

Try using flush method instead of end method of object.

Read Forum

, ,

No Comments


How to Replace Multiple space with only one space

Use regular expression

RegexOptions options = RegexOptions.None;
Regex regex = new Regex(@”[ ]{2,}”, options);
tempo = regex.Replace(tempo, @” “);

VB.Net

Dim str As String = “Sample space 01″
Dim opts As RegexOptions = RegexOptions.None
Dim regx As New Regex(“[ ]{2,}”, opts)

str = regx.Replace(str, ” “)

,

No Comments


Subtracting Days of the Date

To subtract the date with a specified number of days, use the built in shared function of the Date which Date.Today.Add(Days), where Days is a day number. Instead of passing a positive number of days, put a negative sign in it.

Example to subtract 2 days from the current date.

Date.Today.Add(-2)

or you can also use the DateAdd() function and pass a negative day number.

,

No Comments


How to assign Null Values to Date Type Variable in .Net

Actually, you can’t assign a null value to a Date Type variable but you can check if the variable has been set or not by comparing its current value versus default value (#12:00:00 AM#) of date/datetime, you can get the default value by using

  1. Date.MinValue or DateTime.MinValue

To do this, let say you have a variable called LastUpdate with a data type of Datetime

  1. Dim LastUpdate  as DateTime
  2. dim dr as Datarow
  3.  
  4. If DateTime.MinValue = LastUpdate   Then
  5.         'Its Null value
  6.         'if you want to save the value to the database then use the DBNull.Value
  7.         dr("LastUpdate") = DBNull.Value
  8. Else
  9.         dr("LastUpdate") =LastUpdate  
  10. End If

, ,

No Comments


Operation Timeout on GetRequestStream

Resolution:

1. Just make sure the HttpWebResponse instance is closed. Example you have this

  1.     Dim Response As HttpWebResponse
  2.  
  3.     Response = wr.GetResponse()
  4.  
  5. 'You have to close Response
  6. Response.Close

,

No Comments


How to Convert String to Date Format

To Convert String To Date Format, use the DateTime.ParseExact function

DateTime.ParseExact(s as String, format as String, provider As IFormatProvider) as DateTime

Parameters:

s
Type: System.String
A string that contains a date and time to convert. 
format
Type: System.String
A format specifier that defines the required format of s
provider
Type: System.IFormatProvider
An object that supplies culture-specific format information about s

Example:
 

  1.     Private Function ToDate() As DateTime
  2.  
  3.         Dim str As String = "20060505"
  4.  
  5.         Dim mDate As DateTime
  6.  
  7.  
  8.  
  9.         mDate = DateTime.ParseExact(str, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat)
  10.  
  11.         Return mDate
  12.  
  13.     End Function

Read More about DateTime.ParseExact Method (String, String, IFormatProvider)

,

No Comments


How to create Temporary filename in vb.Net
  1. Private Sub TempFile()
  2.      Dim sTempFileName AsString = System.IO.Path.GetTempFileName()
  3.      Dim fsTemp AsNew System.IO.FileStream(sTempFileName, IO.FileMode.Create)
  4.  
  5.      MessageBox.Show(sTempFileName)
  6.  
  7.      'write data to the temp file
  8.      fsTemp.Close()
  9.  
  10.      System.IO.File.Delete(sTempFileName)
  11.  End Sub

, , ,

No Comments


How to Insert Xml Node from One document to another XML Document in vb.net

If you want to insert xmlnode from one document to another, you have to import the xmlnode. 

dim xmlDoc01 as XMLDocument

dim xmlDoc02 as XMLDocument

private sub InsertNod()  ’insert xmlnode from xmlDoc01 to xmlDoc02

{

xmldoc01.AppendChild(xmlDoc01.ImportNode(xmlDoc01.ChildNodes(0), true))

}

, , ,

No Comments


Debugging Problem

Problem:

The breakpoint will not currently be hit. No symbols have been loaded for this document

Solution:

Make sure that you are compiling in Debug mode not Release mode.

,

No Comments



SetPageWidth