Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

Can anyone tell me how to update a datatable to a dataset..?
C#
dt = ds.Tables["Resource"];
        DataRow row = dt.NewRow();
        row["res_Name"] = name;
        row["res_Password"] = pass;
        row["role_name"] = rol;
        dt.Rows.Add(row);


I want to update this dt(datatable) to ds(dataset) here.
I dont want to update database.

Next, in save button click event, I want to update dataset to database.

Please help me.

Thanks.
Posted
Updated 15-Nov-10 22:26pm
v2
Comments
Dalek Dave 16-Nov-10 4:26am    
Edited for Grammar, Spelling and Readability.

1 solution

You can remove the old table
ds.Tables.Remove(dt);

or by name:
ds.Tables.Remove("Resource");

You can later add the table
ds.Tables.Add(dt);

or by name:
ds.Tables.Add("Resource");


Now you can loop the rows of Datatable/Dataset you can generate query or XML & then you can pass it to your stored procedure, then you can save it. So try...if you have any doubt then let us know
 
Share this answer
 
Comments
shwetha_m6 16-Nov-10 4:28am    
Hi, thanks very much... as u said i removed table from ds after assingning it to dt. and i tried to add it again to ds but its not showing previously updated row as i am trying to add 2 rows one after another, it'l show only a last row which is added..!
thatraja 16-Nov-10 4:32am    
better you show the code on your question so that we can help you quickly
shwetha_m6 16-Nov-10 6:00am    
This is the exact code which m using..

public DataSet Getdataset()
{
try
{
con.Open();
ds.Clear();
com = new SqlCommand("select * from Role_Master", con);
ra = com.ExecuteNonQuery();
da = new SqlDataAdapter(com);
da.Fill(ds, "Role");
com = new SqlCommand("select * from Add_Resource", con);
ra = com.ExecuteNonQuery();
da = new SqlDataAdapter(com);
da.Fill(ds, "Resource");
//GridView1.DataSource = ds.Tables[1];
//GridView1.DataBind();
con.Close();
return ds;
}
catch (Exception e)
{
con.Close();
return ds;
}
}

public DataTable Addusers(String name, String pass, String rol)
{
//dt.Clear();
DataSet ds3 = new DataSet();
ds3 = Getdataset();
// Create a data table object and add a new row
dt = ds3.Tables["Resource"];
ds3.Tables.Remove(dt);
DataRow row = dt.NewRow();
row["res_Name"] = name;
row["res_Password"] = pass;
row["role_name"] = rol;
dt.Rows.Add(row);
ds3.Tables.Add(dt);
//// Update data adapter
//da = new SqlDataAdapter("Select * from dt", con);
//da.Update(ds, "Resource");
//dt.Clear();
dt = ds3.Tables["Resource"];
return dt;
}
thatraja 16-Nov-10 6:23am    
since dt as still reference of ds3.Tables["Resource"] so you may create new table like dt1 & add rows to that then you can add the table dt1 to dataset ds3

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