how to add a new row in datatable
how to add a new row in datatable
how to add a new row in datatable
how to add a new row in datatable
how to add a new row in datatable
how to add a new row in datatable how to add a new row in datatable how to add a new row in datatable how to add a new row in datatable how to add a new row in datatable how to add a new row in datatable how to add a new row in datatable how to add a new row in datatable
how to add a new row in datatable how to add a new row in datatable
how to add a new row in datatable
Go Back  Xtreme Visual Basic Talk > > > how to add a new row in datatable


Reply
 
Thread Tools Display Modes
  #1  
Old 02-24-2004, 09:16 AM
yespalaniappan yespalaniappan is offline
Newcomer
 
Join Date: Feb 2004
Posts: 1
Question how to add a new row in datatable


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)


Dim conn As New OleDb.OleDbConnection("Provider=SQLOLEDB.1;UserID=administrator;Passwo rd=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
Reply With Quote
  #2  
Old 02-24-2004, 08:22 PM
LuckyShot LuckyShot is offline
Regular
 
Join Date: Jul 2003
Posts: 50
Default

Try This:



Dim conn As New OleDbConnection("Provider=SQLOLEDB.1;UserID=administrator;Password=pal ani;Trusted_connection=yes;Initial Catalog=palani;Data Source=PALANI")
Dim da As New OleDbDataAdapter()
da.SelectCommand = new OleDbCommand("select * from customerdetails", conn)
Dim ds As New DataSet()
Dim dt As DataTable
Dim dr As DataRow
Dim flag As Integer

da.Fill(ds)
dt = ds.tables(0)
dr = dt.newrow()
dr(0) = CType(txtCustID.Text, Integer)
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)
conn.close()

but if all you want to do is add a record without using the entire dataset then I would change the line

da.SelectCommand = new OleDbCommand("select * from customerdetails", conn)

to

da.SelectCommand = new OleDbCommand("select * from customerdetails where 1=2", conn)

so to not needlessly waist resources by returning a records that aren't needed
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Number of DataRows in a DataTable Spektre .NET Database and Reporting 2 02-26-2004 12:32 PM
Combining Datatable syamin .NET General 0 02-19-2004 08:46 PM
Updating Database throught a DataTable Mikecrosoft .NET Database and Reporting 0 01-28-2004 11:28 AM
adjusting individual column widths of datatable bomber_grrl Database and Reporting 0 05-29-2003 04:34 PM
Query Excel Datatable with SQL dnd Word, PowerPoint, Outlook, and Other Office Products 2 02-25-2003 09:59 AM

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
how to add a new row in datatable
how to add a new row in datatable
how to add a new row in datatable how to add a new row in datatable
how to add a new row in datatable
how to add a new row in datatable
how to add a new row in datatable how to add a new row in datatable how to add a new row in datatable how to add a new row in datatable how to add a new row in datatable how to add a new row in datatable how to add a new row in datatable
how to add a new row in datatable
how to add a new row in datatable
 
how to add a new row in datatable
how to add a new row in datatable
 
-->