本文介绍了转换错误DBNull无法转换为long的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 大家好,感谢您阅读我的帖子。我有以下代码,每隔一段时间我就会收到一个转换错误:DBNull无法转换为long。我使用以下代码: 受保护 Sub Calendar332_SelectionChanged(发件人作为 对象,e As EventArgs) ' 声明将查询数据库的SQL语句 Dim sqlstring As String = SELECT firstname,lastname,startdate,starttime,endtime,COALESCE(hours,0)AS hours FROM小时工作WHERE startdate> ='& Calendar332.SelectedDate& 'AND startdate< ='& Calendar333.SelectedDate& 'ORDER BY startdate Dim strSQLconnection 作为 字符串 = 数据源= SQL Server; SERVER = 123456;初始目录= ABC; UID = abc123; PWD = 123; Dim sqlConnection 作为 新 Data.SqlClient.SqlConnection(strSQLconnection) Dim sqlCommand As New Data.SqlClient.SqlCommand(sqlstring,sqlConnection) sqlConnection.Open() Dim reader As Data.SqlClient.SqlDataReader = sqlCommand.ExecuteReader() Gr idView7.Visible = True GridView7.DataSource = reader GridView7.DataBind() reader.Close() Dim totalhours Dim sqlstring1 As 字符串 = SELECT firstname,lastname,SUM(hours )AS totalhours FROM hoursworked WHERE startdate> ='& Calendar332.SelectedDate& 'AND startdate< ='& Calendar333.SelectedDate& 'group by firstname,lastname Dim sqlConnection1 As 新 Data.SqlClient.SqlConnection(strSQLconnection) Dim sqlCommand1 作为 新 Data.SqlClient.SqlCommand( sqlstring1,sqlConnection1) ' Dim sqlCommand1 As New Data.SqlClient.SqlCommand(sqlstring1,sqlConnection) sqlConnection1.Open() Dim reader1 As Data.SqlClient.SqlDataReader = sqlCommand1.ExecuteReader() 如果 totalhours DBNull.Value 然后 totalhours = 0 结束 如果 GridView8.Visible = True GridView8.DataSource = reader1 GridView8.DataBind() reader1.Close() 结束 Sub 在aspx部分我有: < 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 > 我在第一个select语句中使用了COALESCE后错误消失了远。但是,当我作为最终用户并开始随机点击内容时,错误返回。我希望我的if语句可以解决问题,但没有这样的运气。任何想法或建议将不胜感激。 非常感谢, D-Bar 解决方案 Is运算符确定两个对象引用是否引用同一个对象。但是,它不执行值比较 我认为你想要一个等号( = ),而不是是,但我不是VB专家。 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) 'declare SQL statement that will query the database 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) ' Dim sqlCommand1 As New Data.SqlClient.SqlCommand(sqlstring1, sqlConnection) 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 SubIn 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 解决方案 "The Is operator determines if two object references refer to the same object. However, it does not perform value comparisons"I think you want an equal sign (=), rather than is, but I'm no VB expert. 这篇关于转换错误DBNull无法转换为long的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-03 05:38