How to add a new row in datatable

yespalaniappan

New member
Joined
Feb 27, 2004
Messages
2
Location
chennai
Hi:

How do I add the newrow to the existed datatable in
Ado.net? Below is my coding that will give me the error
message An unhandled exception of type System.IndexOutOfRangeException occurred in system.data.dll .Additional information: Cannot find column 0. say, when I
try to click at btnSave_Click to add new row to data
base.(i can also add the namespace ---- imports system.Data.Oledb and try it,it raise the same error)
Code:
Dim conn As New OleDb.OleDbConnection("Provider=SQLOLEDB.1;UserID=administrator;Password=palani;Trusted_connection=yes;Initial Catalog=palani;Data Source=PALANI")
Dim da As New OleDb.OleDbDataAdapter()
Dim selcust As New OleDb.OleDbCommand("select * from customerdetails", conn)
Dim ds As New DataSet()
Dim dt As DataTable
Dim dr As DataRow
Dim flag As Integer

Private Sub cmdSAVE_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSAVE.Click
If flag = 1 Then
conn.Open()
dt = ds.Tables.Add("CustomerDetails")
dr = dt.NewRow()
dr(0) = CType(txtCustID.Text, Integer)----------------Here ERROR is Raised.
dr(1) = txtName.Text
dr(2) = txtAddress1.Text
dr(3) = txtAddress2.Text
dr(4) = txtCity.Text
dr(5) = CType(txtPhoneNO.Text, Integer)
dr(6) = CType(txtCellNo.Text, Integer)
dr(7) = txtMailID.Text
dt.Rows.Add(dr)
da.Update(ds, "customerdetails")
da.Fill(ds)
conn.Close()
End If
End Sub
:confused:
 
Last edited by a moderator:
Code:
Dim r as DataRow

conn.Open() is not necessary, da automatically opens and
closes the connection when used with Update() command!

r = dt.NewRow()
r(0) = Convert.ToInt32(txtCustID.Text)
r(1) = txtName.Text
r(2) = txtAddress1.Text
r(3) = txtAddress2.Text
r(4) = txtCity.Text
r(5) = Convert.ToIn32(txtPhoneNO.Text)
r(6) = Convert.ToInt32(txtCellNo.Text)
r(7) = txtMailID.Text
dt.Rows.Add(r)
da.Update(ds, "customerdetails")
da.Fill(dt)
conn.Close() is not nessary, da automatically opens and closes
the connection when used with Update() command!

You still need your da.InsertCommand query and parameters for this to work, if your going to use conn.Open() and conn.Close() you may want to just use da.ExecuteNonQuery() instead.
 
okay bri189a,

Thanks for your help in the same.now i want to enter values through textboxes to the update in datatable.(i.e) i dont want to enter the value directly ex( arvind for name of the customer) in the insert command.how can i pass the values through the insertcommand.i need your kindly help in the same.

how to pass parameters to the insertcommand.please expalin with code.i expect your favourable reply.


i dont know whethre it is correct or not,

da.insertcommand("insert into customerdetails values(txtcustid,txtname and so on",conn)

regards,

yespalaniappan.
 
I dont understand what your asking...are you asking how to set up the Insert command itself?

Code:
da.InsertCommand = New OleDbCommand("INSERT INTO CustomerDetails (Field1, Field2, Field3) VALUES (@val1, @val2, @val3)")
da.InsertCommand.Parameters.Add("@val1", OleDbType.Char, 0 (or whatever), "Field1)
Same for @val2 and @val3

Hope that helps.
 
Back
Top