i have a problem when there is no data entered in the database and the data is entered for the first time. then it is giving an error even though the data is get saved. here is the code :

Protected Sub submit1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles submit1.Click
       Dim connstr As String
        Dim gp_no, year, sno, equip_no, qty, total_packets As Integer
        Dim description, make As String
        connstr = ConfigurationManager.ConnectionStrings("gate").ConnectionString()
        Dim conn As New SqlConnection(connstr)
        Dim dt As Date
        dt = Date.Today
        conn.Open()
        gp_no = serial.Text
        years.Text = DateTime.Now.Year
        sno = slno.Text
        description = descp.Text
        make = making.Text
        equip_no = equipno.Text
        qty = quantity.Text
        total_packets = totalpackets.Text
        Dim insert As New SqlClient.SqlCommand("insert into gatein_item (gp_no,year,sno,description ,make ,equip_no ,qty ,total_packets) Values('" & gp_no & "' ," & dt.Year & ",'" & sno & "' ,'" & description & "', '" & make & "','" & equip_no & "','" & qty & "','" & total_packets & "')", conn)
       insert.ExecuteNonQuery()
        conn.Close()
        MsgBox("record saved")
        Dim dn As SqlDataAdapter
        dn = New SqlDataAdapter("select sno, description, make, equip_sno,qty,total_packets from gatein_item", conn)
        Dim ds As New DataSet()
        Dim dr As DataRow
        GridView1.DataSource = ds.Tables()
dr = ds.Tables(0).NewRow()
        dr(1) = slno.Text
        dr(2) = descp.Text
        dr(3) = making.Text
        dr(4) = equipno.Text
        dr(5) = quantity.Text
        dr(6) = totalpackets.Text
        ds.Tables(0).Rows.Add(dr)
        Dim cb As SqlCommandBuilder
        cb = New SqlCommandBuilder(dn)
        dn.Update(ds)
        MsgBox("data")
    End Sub

and the exception is pointing towards the highlited area

Fill() method?

...
 dn.Fill(ds)
 ...
 dr = ds.Tables(0).NewRow()

Hi

First u need to import the System.data.sqlclient namespace.

Write the follwing code in Form_Load

Dim cn as new SqlConnection("connectionstring")
Dim da as new SqlDataAdapter("select custname from customers where custname like 's%'", cn)
Dim ds as new DataSet
da.fill(ds,"customers")
DataGrid1.datasource=ds.tables("customers")

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.