Click here to Skip to main content
15,888,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB.NET
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....
Posted
Updated 7-Mar-16 22:23pm
v2
Comments
Maciej Los 8-Mar-16 2:19am    
What line?
Use a debugger to find out what line causes error!
Member 12376662 8-Mar-16 3:19am    
i = rs(0).Value
[no name] 8-Mar-16 2:22am    
In your code snippet, where do you want convert dbnull to integer?
Member 12376662 8-Mar-16 3:19am    
i = rs(0).Value here the error comes
Richard Deeming 8-Mar-16 5:59am    
I hope that's not your real sa password that you've just published to a public forum - particularly as it's so weak.

Also, your application shouldn't be connecting as sa; it should be using an account which has only the privileges required by your application.

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

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

C#
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[^]
 
Share this answer
 
Comments
Sascha Lefèvre 8-Mar-16 4:34am    
+5
Maciej Los 8-Mar-16 5:04am    
Thank you, Sascha.
Use any of following options to check DBNull value:

Option1: Use sqlserver IsNull() function
VB
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
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
 
Share this answer
 
v3
Comments
Maciej Los 8-Mar-16 5:06am    
Good point. A 4 because, you hadn't warned OP that he uses ADODB.Connaction + ADODB.Recordset instead of proper Sql classes.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900