null值从数据库返回如何在asp.net vb中处理它 [英] null value return from database how to handle it in asp.net vb

查看:88
本文介绍了null值从数据库返回如何在asp.net vb中处理它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里使用这样的代码

 Dim qry =  从tbl_deal中选择max(ID),其中officeid ='& ddl1.SelectedValue.Trim&  ' 
dun cmd1 as sqlCommand = New SqlCommand(qry,conn)
conn.open()
dim read As SqlDataReader = cmd1.ExecuteReader()
read.Read()
Dim a = 0
if (read.HasValue)然后
a = read( 0
endif
a = a + 1



但此代码在nullvalue中给出错误1.谢谢

问候,

Umesh Daiya

解决方案

请使用以下查询:

 选择  COALESCE (max(ID), 0 来自 tbl_deal 其中 officeid = @ officeid 



DbNull 解决你的问题。


给convert.tostring回覆ad(0)如果它的null然后是0并且如果它不为null那么你的函数a = a + 1并且如果不为null则将读取转换为int ............ convert.tostring也可以处理null值它不会抛出错误


 如果 读取( 0 )= DBNull.Value 然后 
a =读取( 0
end if


Here i use code like this

Dim qry="select max(ID) from tbl_deal where officeid='" & ddl1.SelectedValue.Trim & "'"
dun cmd1 as sqlCommand=New SqlCommand(qry,conn)
conn.open()
dim read As SqlDataReader=cmd1.ExecuteReader()
read.Read()
Dim a=0
if(read.HasValue) Then
a=read(0)
endif
a=a+1


but this code given error in nullvalue its not 1.Thanks
Regards,
Umesh Daiya

解决方案

Please, use below query:

select COALESCE(max(ID),0) from tbl_deal where officeid=@officeid


That resolves your problem with DbNull.


give convert.tostring to read(0) if its null then 0 and if its not null then your function a=a+1 and convert read to int if not null ............ convert.tostring can handle null values also it will not throw error


If not read(0)= DBNull.Value Then
a=read(0)
end if 


这篇关于null值从数据库返回如何在asp.net vb中处理它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆