Private Sub frmCust_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       
               dbSource = "Data Source=xx-B4C521B850\SQLEXPRESS;Initial Catalog=TT;Integrated Security=True"

        con.ConnectionString = dbSource
        con.Open()
        da.Dispose()
        sql = "Select * from Cust"
        da = New SqlClient.SqlDataAdapter(sql, con)

        da.Fill(ds, "Cust")
        da.Update(ds, "Cust")
        con.Close()
        MaxRows = ds.Tables("Cust").Rows.Count
        inc = -1
    End Sub

if after executing upper code the maxrows value is 10 after executing following code it becomes 21 how!!!!!!!!

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
       
                    con.Open()
            sql = "INSERT INTO Cust (Cust_Fn,Cust_Ln,Cust_Address,Cust_Mob,Cust_Land,Cust_CLand) VALUES ('" & txtFn.Text & "','" & txtLn.Text & "','" & txtAddress.Text & "','" & txtMob.Text & "','" & txtLand.Text & "','" & chkLCo.CheckState & "')"
            cmd = New SqlClient.SqlCommand(sql, con)
            cmd.ExecuteNonQuery()

            da.Update(ds, "Cust")
            MsgBox("New Record added to the Database")
da.Fill(ds, "Cust")
            con.Close()
            MaxRows1 = ds.Tables("Cust").Rows.Count
            inc = -1
        End If
    End Sub

PLS HELP

Dani AI

Generated

Short answer for : was right that you’re counting after new records were added, but the surprising jump from 10 to 21 comes from how you refresh the in-memory table. Calling the data adapter’s Fill again on the same DataTable without clearing it will merge/append the rows fetched from the database. If the table in memory has no primary key defined, Fill cannot match rows and simply appends them — so 10 original rows + 11 rows fetched from DB = 21.

Why that happens (quick example): the DB went from 10 -> 11 after your INSERT. Then Fill merged the 11 DB rows into the existing 10 rows in the DataSet, producing 21. If the DataTable has a proper primary key, Fill will match by key and update existing rows instead of duplicating them.

Practical fixes and best practices:

  • Refresh by clearing before refilling:

    Dim tbl As DataTable = myDataSet.Tables("Cust")
    tbl.Clear()
    adapter.Fill(tbl)
  • Or load the schema with keys so Fill can merge correctly:

    adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey
    Dim tbl As New DataTable()
    adapter.Fill(tbl)
  • Prefer one consistent pattern: either

    • work with the DataSet/DataTable and call adapter.Update to persist changes, or
    • execute SQL commands directly and then replace (clear + fill) the DataTable to reflect DB state. Calling adapter.Update after executing a direct INSERT does not refresh the DataSet — Update pushes changes from the DataSet to the DB, it does not pull DB changes.

Other notes: use parameterized commands (avoid string concatenation), wrap connections/commands in Using blocks, and handle exceptions. For your form-load question, extract the load logic into a shared method (e.g., LoadCustData()) and call that from both the Load event and any button click — don’t rely on calling the Load event handler directly.

Recommended Answers

All 3 Replies

Simply because you are taking the count after adding more records.

Thanks for ur help!!

One more question is it possible to call form load event on button click

Following is the vb code i want same function in vb.net

Private Sub cmdaddcancel_Click()
       Form_Load
  End Sub

in vb we simply call Form_Load is it possible in vb.net

To call form2 you must to make an object of form2 :

Dim Myform as New Form2
    Myform.show

NOTE:---Please start a new thread for a new question.

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.