Click here to Skip to main content
15,896,912 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hey I have Exception in this code , m new to Sqlserver , can any one help me and explain me the following code and correct it and explain the exception thanks ...


SqlDataAdapter da = new SqlDataAdapter("Select * from mytable", SqlConnection Object);
DataSet ds = new DataSet();
da.Fill(ds);

DataRow row1 = ds.Tables[0].NewRow();

row1["ID"] = 3;
row1["Name"] = "Saaaaadii";

ds.Tables[0].Rows.Add(row1);

da.Update(ds);



Exception occurrs at : da.Update(ds);

Update requires a valid InsertCommand when passed DataRow collection with new rows.
Posted

Hi,

Check this link below...

Inserting relational data using DataSet and DataAdapter[^]

Regards,
RK
 
Share this answer
 
Hi All
Run this code

SqlDataAdapter da = new SqlDataAdapter("select * from item ", con);
con.Open();
try
{
DataSet ds = new DataSet();
da.Fill(ds);
SqlCommandBuilder cb = new SqlCommandBuilder(da);




DataRow row1 = ds.Tables[0].NewRow();

row1[0] = txtaccno.Text;
row1[1] = txtname.Text;
row1[2] = txtsalary.Text;
ds.Tables[0].Rows.Add(row1);
da.Update(ds);
gvShow.DataSource = ds;
gvShow.DataBind();

Regards,
Pawan
 
Share this answer
 
Few thing you need to do
1) Is the table MYTable has a primary key if not first create it,
2) you need to use a commandbuilder [^]

hope you got it
 
Share this answer
 

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