GVillani82
GVillani82

Reputation: 17429

Using ADO .NET convert datatime from sql Server to DateTime in VB .NET

I have created a Web Service in Visual Studio 2005, using VB .NET language. This Web Service interacts with a database on Sql Server through ADO .NET. Using ADO .NET I get one table with this code

    conn = New SqlConnection(Utilities.ConnectionString)
    sqlcmd = New SqlClient.SqlCommand()
    sqlcmd.Connection = conn
    sqlcmd.CommandType = CommandType.Text
    sqlcmd.CommandText = "select * from myTable" 
    da = New SqlClient.SqlDataAdapter()
    da.SelectCommand = sqlcmd
    table = New DataTable()
    da.Fill(table)

where Utilities is a class created by me for managing and return ConnectionString. The table myTable contains more than one field of String type and just one field of datetime named LastChangeDate. I want select all the dates found in the rows of myTable so I use this code:

     Dim d As DateTime 
     For Each row In table.Rows
          d = row.Item("LastChangeDate")
          '
          ' other codes for managing the retuned dates
          '

The problem is that after this operation d contains NULL value.

What can I do for convert correctly the datetime returned from Item method into DateTime in VB .NET

Upvotes: 1

Views: 1612

Answers (1)

shahkalpesh
shahkalpesh

Reputation: 33476

dim isDate as Boolean
dim lastChangeDate as Date

isDate = Date.TryParse(row.Item("LastChangeDate"), lastChangeDate)
If isDate Then
  '** code that should work with the extracted date ...
End If

Upvotes: 2

Related Questions