Hello all, thanks for reading my post. I have the following code and every so often I get a conversion error: DBNull cannot be converted to long. I am using the following code:
Protected Sub Calendar332_SelectionChanged(sender As Object, e As EventArgs)
Dim sqlstring As String = "SELECT firstname, lastname, startdate, starttime, endtime, COALESCE(hours,0) AS hours FROM hoursworked WHERE startdate >= '" & Calendar332.SelectedDate & "' AND startdate <= '" & Calendar333.SelectedDate & "' ORDER BY startdate"
Dim strSQLconnection As String = "Data Source=SQL Server;SERVER=123456; Initial Catalog=ABC; UID=abc123;PWD=123;"
Dim sqlConnection As New Data.SqlClient.SqlConnection(strSQLconnection)
Dim sqlCommand As New Data.SqlClient.SqlCommand(sqlstring, sqlConnection)
sqlConnection.Open()
Dim reader As Data.SqlClient.SqlDataReader = sqlCommand.ExecuteReader()
GridView7.Visible = True
GridView7.DataSource = reader
GridView7.DataBind()
reader.Close()
Dim totalhours
Dim sqlstring1 As String = "SELECT firstname, lastname, SUM(hours) AS totalhours FROM hoursworked WHERE startdate >= '" & Calendar332.SelectedDate & "' AND startdate <= '" & Calendar333.SelectedDate & "' group by firstname, lastname"
Dim sqlConnection1 As New Data.SqlClient.SqlConnection(strSQLconnection)
Dim sqlCommand1 As New Data.SqlClient.SqlCommand(sqlstring1, sqlConnection1)
sqlConnection1.Open()
Dim reader1 As Data.SqlClient.SqlDataReader = sqlCommand1.ExecuteReader()
If totalhours Is DBNull.Value Then
totalhours = 0
End If
GridView8.Visible = True
GridView8.DataSource = reader1
GridView8.DataBind()
reader1.Close()
End Sub
In the aspx part I have:
<ItemTemplate>
<%# Format((CLng(DataBinder.Eval(Container.DataItem, "totalhours")) - (CLng(DataBinder.Eval(Container.DataItem, "totalhours")) Mod 60)) / 60, "0")
%>
:
<%# Format(CLng(DataBinder.Eval(Container.DataItem, "totalhours")) Mod 60, "00")%>
</ItemTemplate>
After I used COALESCE in the first select statement the error went away. But, when I act as an end user and start clicking on stuff randomly the error returned. I was hoping my if statement would do the trick, but no such luck. Any ideas or suggestions would be greatly appreciated.
Thank you very much,
D-Bar