Unable to cast object of type 'System.DateTime' to type 'System.String'.
This is the code that I am using:
Dim sqlStr As String = "SELECT T1.Col, T1.Col, T2.Col, T2.Col, T1.Col, CONVERT(CHAR(12), T1.Date1, 107) AS Date, T1.Date2 AS Date2, T1.Col, T1.Col FROM Table T1 LEFT OUTER JOIN Table T2 ON T1.Col = T2.Col WHERE T1.Col = '" & HttpContext.Current.User.Identity.Name & "' ORDER BY T1.Col DESC"
Dim dt As New DataTable
Dim dataAdapter As New SqlClient.SqlDataAdapter(sqlStr, connstr)
dataAdapter.Fill(dt)
dataAdapter.Dispose()
Dim Keyconnstr As String = ConfigurationManager.ConnectionStrings("String").ToString()
Dim sqlStrKey As String = "SELECT MessageID, Algorithm FROM Algorithm WHERE UserName = '" & HttpContext.Current.User.Identity.Name & "'"
Dim dtKey As New DataTable
Dim dataAdapterKey As New SqlClient.SqlDataAdapter(sqlStrKey, Keyconnstr)
dataAdapterKey.Fill(dtKey)
dataAdapterKey.Dispose()
Dim query = From t3 In dt.AsEnumerable() Join t4 In dtKey.AsEnumerable() On t3.Field(Of String)("Col") Equals _
t4.Field(Of String)("Col") Select New With { _
.Col = t3.Field(Of String)("Col"), _
.Col = t3.Field(Of String)("Col"), _
.Col = t3.Field(Of String)("Col"), _
.Col = t3.Field(Of String)("Col"), _
.Col = t3.Field(Of String)("Col"), _
.Date = t3.Field(Of String)("Date"), _
.Date2 = t3.Field(Of String)("Date2"), _
.Col = t3.Field(Of String)("Col"), _
.Col = t3.Field(Of String)("Col"), _
.Col = t4.Field(Of String)("Col")}
MyGrid.DataSource = query.ToList()
Does anyone know how I could fix this?
It appears to olny be triggering this error for the Date2 column. Everthing seems to work fine for the Date column.
mattcase
Member
374 Points
518 Posts
Date Error
Feb 28, 2012 06:02 AM|LINK
Hi,
I am receiving this error:
Unable to cast object of type 'System.DateTime' to type 'System.String'.
This is the code that I am using:
Dim sqlStr As String = "SELECT T1.Col, T1.Col, T2.Col, T2.Col, T1.Col, CONVERT(CHAR(12), T1.Date1, 107) AS Date, T1.Date2 AS Date2, T1.Col, T1.Col FROM Table T1 LEFT OUTER JOIN Table T2 ON T1.Col = T2.Col WHERE T1.Col = '" & HttpContext.Current.User.Identity.Name & "' ORDER BY T1.Col DESC" Dim dt As New DataTable Dim dataAdapter As New SqlClient.SqlDataAdapter(sqlStr, connstr) dataAdapter.Fill(dt) dataAdapter.Dispose() Dim Keyconnstr As String = ConfigurationManager.ConnectionStrings("String").ToString() Dim sqlStrKey As String = "SELECT MessageID, Algorithm FROM Algorithm WHERE UserName = '" & HttpContext.Current.User.Identity.Name & "'" Dim dtKey As New DataTable Dim dataAdapterKey As New SqlClient.SqlDataAdapter(sqlStrKey, Keyconnstr) dataAdapterKey.Fill(dtKey) dataAdapterKey.Dispose() Dim query = From t3 In dt.AsEnumerable() Join t4 In dtKey.AsEnumerable() On t3.Field(Of String)("Col") Equals _ t4.Field(Of String)("Col") Select New With { _ .Col = t3.Field(Of String)("Col"), _ .Col = t3.Field(Of String)("Col"), _ .Col = t3.Field(Of String)("Col"), _ .Col = t3.Field(Of String)("Col"), _ .Col = t3.Field(Of String)("Col"), _ .Date = t3.Field(Of String)("Date"), _ .Date2 = t3.Field(Of String)("Date2"), _ .Col = t3.Field(Of String)("Col"), _ .Col = t3.Field(Of String)("Col"), _ .Col = t4.Field(Of String)("Col")} MyGrid.DataSource = query.ToList()Does anyone know how I could fix this?
It appears to olny be triggering this error for the Date2 column. Everthing seems to work fine for the Date column.
Thanks!
yrb.yogi
Star
14460 Points
2402 Posts
Re: Date Error
Feb 28, 2012 06:09 AM|LINK
why not assign the result directly without using cast.
you are getting error because your properties support datetime datatype and you are assinging the string value to datetime datetype properties.
if you will need to cast the data, than cast with datetime, your error will go out..
.Net All About
mattcase
Member
374 Points
518 Posts
Re: Date Error
Feb 28, 2012 06:32 PM|LINK
Thanks for your reply. Could you possibly give me some guidance on how to do what you are recommending?
Thanks.
mattcase
Member
374 Points
518 Posts
Re: Date Error
Feb 28, 2012 10:29 PM|LINK
Got It!