Click here to Skip to main content
15,870,165 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have to save data into data table by using c# coding.
I have displayed data into grid view by given below coding.
I want to modify the given code to save those data into data table and same as display on grid view.
Thank You.

C#
protected void Button1_Click(object sender, EventArgs e)
    {
dt1.Columns.Add("SiteName", typeof(string));
        dt1.Columns.Add("State", typeof(string));
        dt1.Columns.Add("Cluster", typeof(string));

        for (int i = 0; i < 10; i++)
        {
            dt1.Rows.Add("A", "B", "C");
            gvData.DataSource = dt1;
            gvData.DataBind();
        }
     }
Posted
Updated 11-Sep-14 22:47pm
v2

See the example, Its having full operation with gridview . Adding, Updating , Deleting records

http://www.dotnetgallery.com/kb/resource10-How-to-perform-insert-update-delete-and-select-rows-in-ASPNET-gridview-c.aspx[^]
 
Share this answer
 
C#
  DataTable dt1 = new DataTable();

dt1.Columns.Add("SiteName", typeof(string));
dt1.Columns.Add("State", typeof(string));
dt1.Columns.Add("Cluster", typeof(string));
for (int i = 0; i < 10; i++)
{
dt1.Rows.Add("A", "B", "C");
}


C#
gvData.DataSource = dt1;
gvData.DataBind();


Updated


C#
using System.Data.SqlClient;


  for (int n = 0; n < dt1.Rows.Count; n++)
{
try
{
SqlConnection con = new SqlConnection("Your Database Connection String");
string col1 = Convert.ToString(dt1.Rows[n]["SiteName"]);
string col2 = Convert.ToString(dt1.Rows[n]["State"]);
string col3 = Convert.ToString(dt1.Rows[n]["Cluster"]);
string strQry = "insert into table(col1,col2,col1) values('" + col1 + "','" + col2 + "','" + col3 + "')";
                
con.Open();
SqlCommand cmd = new SqlCommand(strQry, con);
cmd.ExecuteNonQuery();
}
catch(exception e1)
{
}
finally
{
con.Close();
}

 }
 
Share this answer
 
v2
Comments
Member 11042100 12-Sep-14 5:35am    
@ajmot: this one working , but it display the values into gridview. I need to save those data into database. Can you?
go through this[^]
 
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