我如何...转换类型从dbnull到整数是无效的请帮助我 [英] How do i...convertion type from dbnull to integer is not valid pls help me

查看:81
本文介绍了我如何...转换类型从dbnull到整数是无效的请帮助我的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  Dim  con  As   SqlConnection( 数据源= NAAZNEEN-PC;初始目录= testmob;集成安全= false;用户id = sa;密码= q1w2e3r4 /;

公共 conn 作为 ADODB.Connection
公共 rs As New ADODB.Recordset
Public sql < span class =code-keyword> As String
Public qry < span class =code-keyword> As String
Dim image < span class =code-keyword> As String
Dim cmd As SqlCommand
Dim dr As SqlDataReader
Dim da As SqlDataAdapter
Dim ds As DataSet
公共 功能 opendb() As 对象

如果 conn.State = 1 然后 conn.Close()
conn.Open( Provider = SQLOLEDB.1; Persist Security Info = False; User ID = sa; password = q1w2e3r4 /; Initial Catalog = testmob; Data Source = NAAZNEEN-PC
返回 0
结束 功能
Sub loadcat()
sql = select * from tbl_category
If rs.State = 1 然后 rs.Close()
rs.Open(sql,conn)
drpcategory.Items.Clear()
执行 rs .EOF
drpcategory.Items.Add(rs( 1 )。Value)
rs.MoveNext()
循环
结束 Sub
< span class =code-keyword> Sub loadid()
Dim i As 整数
sql = 从tbl_product中选择max(p_id)
如果 rs.State = 1 那么 rs.Close()
rs .Open(sql,conn)
如果 rs.EOF = False 然后
i = rs( 0 )。值
i = i + 1
txtid.Text = i

其他
i = 1
txtid.Text = i
结束 如果

结束 Sub
Sub clear()
drpcategory.SelectedIndex = 0
txtmodel.Text =
txtcolor.Text =
txtdesc.Text =
txtamount.Text =
txtquan.Text =
txtid .Text =
结束 Sub
Sub loadgrid()


qry = 选择p_id作为PRODUCT_ID,p _cat为CATEGORY,p_model为MODEL,p_color为AS COLOR,p_desc为DESCRIPTION,p_amount为AMOUNT,p_quantity为QUANTITY,p_image为图像来自tbl_product
da = SqlDataAdapter(qry,con)
ds = DataSet
da.Fill(ds, tbl_product
GridView1.DataSource = ds
GridView1.DataMember = ds.Tables( 0 )。ToString
GridView1.DataBind()

结束 Sub





我尝试了什么:



这个dbnull转换为整数错误即将到来所以请尽快帮助我....

解决方案

你有检查 Recordset.Field 是否等于 DbNull.Value [ ^ ](在VB.NET中)。怎么样?



 i = IIf(rs( 0 )。值  DbNull.Value, 0 ,rs(< span class =code-digit> 0 )。值)



 i = rs [ 0 ]。值== DbNull.Value?  0 :rs( 0 )。值; 







顺便说一句:我不知道你为什么要使用ADODB.Recordset。

你必须使用 SqlConnection [ ^ ] + SqlCommand [ ^ ] + DataReader [ ^ ]

不要混合 ADO.NET [ ^ ] ADODB [ ^ ]。

ADO和ADO的比较.NET - 维基百科,免费的百科全书 [ ^ ]


使用以下任一选项检查 DBNull 值:



Option1 :使用sqlserver IsNull()功能

  Dim  i 作为 整数 

sql = 从tbl_product中选择isnull(max(p_id),0)

如果 rs.State = 1 那么 rs.Close()
rs.Open(sql,conn)
如果 rs.EOF = False < span class =code-keyword>然后
i = rs( 0 )。值
i = i + 1
txtid.Text = i
结束 如果



Option2 :在vb中使用 DBNull.Value >

  Dim  i  As  整数 
sql = 从tbl_product中选择max(p_id)

如果 rs.State = 1 然后 rs.Close()
rs.Open(sql,conn)

如果 rs.EOF = False 那么
i = 如果((rs( 0 )。Value = DBNull.Value), 0 ,Convert.ToInt32(rs( 0 )。值))
i = i + 1
txtid.Text = i
结束 如果


Dim con As New SqlConnection("Data Source=NAAZNEEN-PC;Initial Catalog=testmob;Integrated Security=false;user id=sa;password=q1w2e3r4/;")

    Public conn As New ADODB.Connection
    Public rs As New ADODB.Recordset
    Public sql As String
    Public qry As String
    Dim image As String
    Dim cmd As SqlCommand
    Dim dr As SqlDataReader
    Dim da As SqlDataAdapter
    Dim ds As DataSet
    Public Function opendb() As Object

        If conn.State = 1 Then conn.Close()
        conn.Open("Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;password=q1w2e3r4/;Initial Catalog=testmob;Data Source=NAAZNEEN-PC")
        Return 0
    End Function
    Sub loadcat()
        sql = "select * from tbl_category"
        If rs.State = 1 Then rs.Close()
        rs.Open(sql, conn)
        drpcategory.Items.Clear()
        Do While Not rs.EOF
            drpcategory.Items.Add(rs(1).Value)
            rs.MoveNext()
        Loop
    End Sub
    Sub loadid()
        Dim i As Integer
        sql = "select max(p_id) from tbl_product"
        If rs.State = 1 Then rs.Close()
        rs.Open(sql, conn)
        If rs.EOF = False Then
            i = rs(0).Value
            i = i + 1
            txtid.Text = i

        Else
            i = 1
            txtid.Text = i
        End If

    End Sub
    Sub clear()
        drpcategory.SelectedIndex = 0
        txtmodel.Text = ""
        txtcolor.Text = ""
        txtdesc.Text = ""
        txtamount.Text = ""
        txtquan.Text = ""
        txtid.Text = ""
    End Sub
    Sub loadgrid()


        qry = "select p_id as PRODUCT_ID,p_cat as CATEGORY,p_model as MODEL,p_color AS COLOUR,p_desc AS DESCRIPTION,p_amount as AMOUNT,p_quantity as QUANTITY,p_image as IMAGE from tbl_product"
        da = New SqlDataAdapter(qry, con)
        ds = New DataSet
        da.Fill(ds, "tbl_product")
        GridView1.DataSource = ds
        GridView1.DataMember = ds.Tables(0).ToString
        GridView1.DataBind()

    End Sub



What I have tried:

in this the dbnull convertion to integer error is coming so pls help me soon....

解决方案

You have to check if Recordset.Field is not equal to DbNull.Value[^] (in VB.NET). How?


i = IIf(rs(0).Value Is DbNull.Value, 0, rs(0).Value)


i = rs[0].Value == DbNull.Value ? 0 : rs(0).Value;




By The Way: i have no idea why do you use ADODB.Recordset.
You have to use SqlConnection[^] + SqlCommand[^] + DataReader[^]
Do not mix ADO.NET[^] with ADODB[^].
Comparison of ADO and ADO.NET - Wikipedia, the free encyclopedia[^]


Use any of following options to check DBNull value:

Option1: Use sqlserver IsNull() function

Dim i As Integer

sql = "select isnull(max(p_id), 0) from tbl_product"

If rs.State = 1 Then rs.Close()
rs.Open(sql, conn)
If rs.EOF = False Then
	i = rs(0).Value
	i = i + 1
	txtid.Text = i
End If


Option2: Use DBNull.Value in vb

Dim i As Integer
sql = "select max(p_id) from tbl_product"

If rs.State = 1 Then rs.Close()
rs.Open(sql, conn)

If rs.EOF = False Then
	i = If((rs(0).Value = DBNull.Value), 0, Convert.ToInt32(rs(0).Value))
	i = i + 1
	txtid.Text = i
End If


这篇关于我如何...转换类型从dbnull到整数是无效的请帮助我的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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