Save data into data table dynamically and then save the data table into database in C#

Posted by Raj.Trivedi under ADO.NET on 1/9/2015 | Points: 10 | Views : 11110 | Status : [Member] [MVP] | Replies : 5
Hello Team,

I am trying to data into data table. The data table is created dynamically, and i want to store multiple rows into data table on add button and then on save button i want to store all the rows in the data table to database at once.

Regard's
Raj.Trivedi
"Sharing is Caring"
Please mark as answer if your Query is resolved



Responses

Posted by: Vuyiswamb on: 1/12/2015 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down
1) Create a Datatable

2)When user add , Add the new Row in the Datatable

3) When you want to save to the DB , Take the same datatable and use a DataAdapter and Commit the Rows in the Database

Thank you for posting at Dotnetfunda
[Administrator]

Raj.Trivedi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Raj.Trivedi on: 1/12/2015 [Member] [MVP] Starter | Points: 25

Up
0
Down
Hello Sir, Can you post a sample code for the same

Regard's
Raj.Trivedi
"Sharing is Caring"
Please mark as answer if your Query is resolved

Raj.Trivedi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vuyiswamb on: 1/13/2015 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down
I want you to know how to do it yourself , and when you get stuck i will help.

now google how to create a Datatable and add Rows on it and save the datatable added into the DB tell me when you get stuck only when you started something.

Thank you for posting at Dotnetfunda
[Administrator]

Raj.Trivedi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Aman2312 on: 9/15/2015 [Member] Starter | Points: 25

Up
0
Down
To create Dynamic DataTable:

DataTable dt = new DataTable();

dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
new DataColumn("Name", typeof(string)),
new DataColumn("Age",typeof(string)) });
dt.Rows.Add(1, "Ankit", "21");
dt.Rows.Add(2, "Anuj", "22");
dt.Rows.Add(3, "Anish", "24");
dt.Rows.Add(4, "Ankush", "22");


Now to Save it to Database:

string sql = "INSERT INTO Student_detail (Name, Age) VALUES (@A, @B)";

using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString))
{
conn.Open();
foreach (DataRow r in dt.Rows)
{
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = sql;
cmd.Parameters.AddWithValue("@A", r["Name"]);
cmd.Parameters.AddWithValue("@B", r["Age"]);
cmd.ExecuteNonQuery();

}
Response.Write("<script> alert('Saved Successfully');</script>");
}



For more details Visit This Article: it will be helpfull for u(with example)

Asp.Net: create dynamic datatable and Save to database using C#
http://programmerskills.blogspot.com/2015/09/aspnet-create-dynamic-datatable-and.html

Raj.Trivedi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rojalin on: 10/14/2015 [Member] Starter | Points: 25

Up
0
Down
Dynamic Gridview with addnew row facility will solve your query.

This link may help you:
https://rojalinsahoo.wordpress.com/2015/08/01/dynamic-gridview/ Dynamic Gridview
http://www.codeproject.com/Tips/993064/Creating-Dynamic-Grid-view-Worked-Like-Excel-Sheet Dynamic Gridview


Raj.Trivedi, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response