Click here to Skip to main content
12,956,278 members (58,966 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: , +
i am writing the code like this...i am getting data to m_dt at last that tabledata will be saved to grid also but it is not saved in database how to save in database pleaseeeeeeeeee

protected void btn_Save_Click(object sender, EventArgs e)
{
DataTable m_dt = new DataTable();
DataRow dr;
m_dt.Columns.Add(new DataColumn("Br_Name"));
m_dt.Columns.Add(new DataColumn("Br_BeddedHospital"));
m_dt.Columns.Add(new DataColumn("Br_Director"));
m_dt.Columns.Add(new DataColumn("Br_Since"));
m_dt.Columns.Add(new DataColumn("Br_Fax"));
m_dt.Columns.Add(new DataColumn("Br_Address"));
m_dt.Columns.Add(new DataColumn("Br_Phone_No1"));
m_dt.Columns.Add(new DataColumn("Br_E_Mail_Id"));


foreach (GridViewRow gvr in grv_Child.Rows)
{
dr = m_dt.NewRow();
dr["Br_Name"] = ((Label)gvr.Cells[1].FindControl("Label1")).Text;
dr["Br_BeddedHospital"] = ((Label)gvr.Cells[2].FindControl("Label2")).Text;
dr["Br_Director"] = ((Label)gvr.Cells[3].FindControl("Label3")).Text;
dr["Br_Since"] = ((Label)gvr.Cells[4].FindControl("Label4")).Text;
dr["Br_Fax"] = ((Label)gvr.Cells[5].FindControl("Label5")).Text;
dr["Br_Address"] = ((Label)gvr.Cells[6].FindControl("Label6")).Text;
dr["Br_Phone_No1"] = ((Label)gvr.Cells[7].FindControl("Label7")).Text;
dr["Br_E_Mail_Id"] = ((Label)gvr.Cells[8].FindControl("Label8")).Text;
m_dt.Rows.Add(dr);
}

grv_BranchDetails.DataSource = m_dt;
grv_BranchDetails.DataBind();


}
Posted 18-Feb-13 0:20am
Comments
SateeshVankayala 18-Feb-13 6:31am
   
Where is answer sir..............
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

edit:

protected void btn_Save_Click(object sender, EventArgs e)
{
DataTable m_dt = new DataTable();
DataRow dr;
m_dt.Columns.Add(new DataColumn("Br_Name"));
m_dt.Columns.Add(new DataColumn("Br_BeddedHospital"));
m_dt.Columns.Add(new DataColumn("Br_Director"));
m_dt.Columns.Add(new DataColumn("Br_Since"));
m_dt.Columns.Add(new DataColumn("Br_Fax"));
m_dt.Columns.Add(new DataColumn("Br_Address"));
m_dt.Columns.Add(new DataColumn("Br_Phone_No1"));
m_dt.Columns.Add(new DataColumn("Br_E_Mail_Id"));
 
 
foreach (GridViewRow gvr in grv_Child.Rows)
{
dr = m_dt.NewRow();
dr["Br_Name"] = ((Label)gvr.Cells[1].FindControl("Label1")).Text;
dr["Br_BeddedHospital"] = ((Label)gvr.Cells[2].FindControl("Label2")).Text;
dr["Br_Director"] = ((Label)gvr.Cells[3].FindControl("Label3")).Text;
dr["Br_Since"] = ((Label)gvr.Cells[4].FindControl("Label4")).Text;
dr["Br_Fax"] = ((Label)gvr.Cells[5].FindControl("Label5")).Text;
dr["Br_Address"] = ((Label)gvr.Cells[6].FindControl("Label6")).Text;
dr["Br_Phone_No1"] = ((Label)gvr.Cells[7].FindControl("Label7")).Text;
dr["Br_E_Mail_Id"] = ((Label)gvr.Cells[8].FindControl("Label8")).Text;
m_dt.Rows.Add(dr);
}
 
grv_BranchDetails.DataSource = m_dt;
grv_BranchDetails.DataBind();
 
 
} 
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

You have to make connection to the database you've created and write command for SQL Server.

Like;

protected void btn_Save_Click(object sender, EventArgs e)
{
 
DataTable m_dt = new DataTable();
DataRow dr;
m_dt.Columns.Add(new DataColumn("Br_Name"));
m_dt.Columns.Add(new DataColumn("Br_BeddedHospital"));
m_dt.Columns.Add(new DataColumn("Br_Director"));
m_dt.Columns.Add(new DataColumn("Br_Since"));
m_dt.Columns.Add(new DataColumn("Br_Fax"));
m_dt.Columns.Add(new DataColumn("Br_Address"));
m_dt.Columns.Add(new DataColumn("Br_Phone_No1"));
m_dt.Columns.Add(new DataColumn("Br_E_Mail_Id"));
 
 
foreach (GridViewRow gvr in grv_Child.Rows)
{
dr = m_dt.NewRow();
dr["Br_Name"] = ((Label)gvr.Cells[1].FindControl("Label1")).Text;
dr["Br_BeddedHospital"] = ((Label)gvr.Cells[2].FindControl("Label2")).Text;
dr["Br_Director"] = ((Label)gvr.Cells[3].FindControl("Label3")).Text;
dr["Br_Since"] = ((Label)gvr.Cells[4].FindControl("Label4")).Text;
dr["Br_Fax"] = ((Label)gvr.Cells[5].FindControl("Label5")).Text;
dr["Br_Address"] = ((Label)gvr.Cells[6].FindControl("Label6")).Text;
dr["Br_Phone_No1"] = ((Label)gvr.Cells[7].FindControl("Label7")).Text;
dr["Br_E_Mail_Id"] = ((Label)gvr.Cells[8].FindControl("Label8")).Text;
m_dt.Rows.Add(dr);
}
 
grv_BranchDetails.DataSource = m_dt;
grv_BranchDetails.DataBind();
 

SqlConnection sconn = new SqlConnection();
 
sconn.ConnectionString = "your connection string here";
 
SqlCommand ins = new SqlCommand();
 
ins.Connection = sconn;
 
ins.CommandText = "update yourtable set Br_Name=@Br_Name, Br_BeddedHospital=@Br_BeddedHospital, Br_Director=@Br_Director, Br_Since=@Br_Since, Br_Fax=@Br_Fax, Br_Address=@Br_Address, Br_Phone_No1=@Br_Phone_No1, Br_E_Mail_Id=@Br_E_Mail_Id"; 
 
ins.Parameters.AddWithValue("@Br_Name",yourgrid.Value.ToString());
ins.Parameters.AddWithValue("@Br_BeddedHospital",yourgrid.Value.ToString();
ins.Parameters.AddWithValue("@Br_Director",yourgrid.Value.ToString();
ins.Parameters.AddWithValue("@Br_Since",yourgrid.Value.ToString();
ins.Parameters.AddWithValue("@Br_Fax",yourgrid.Value.ToString();
ins.Parameters.AddWithValue("@Br_Address",yourgrid.Value.ToString();
ins.Parameters.AddWithValue("@Br_Phone_No1",yourgrid.Value.ToString();
ins.Parameters.AddWithValue("@Br_E_Mail_Id",yourgrid.Value.ToString();
 
sconn.Open();
                   
ins.ExecuteNonQuery();
                    
sconn.Close();
}


By the way your SQL databases table names must be as same as you define at command text like; Br_Name, Br_BeddedHospital, Br_Director, Br_Since, Br_Fax, Br_Address,Br_Phone_No1, Br_E_Mail_Id.

I hope this will help.
  Permalink  
v2
Comments
SateeshVankayala 18-Feb-13 7:10am
   
here your grid is grid viewname?????
and
what is value??????
tolgasenol 18-Feb-13 7:21am
   
sorry?
SateeshVankayala 18-Feb-13 7:22am
   
please tell me the answer...........
tolgasenol 18-Feb-13 7:35am
   
There should be a SQL Database table created. Right?
You have to insert or update this SQL Database with SQL command.

Check this out;
http://msdn.microsoft.com/en-US/library/8hwekas8%28v=vs.80%29.aspx
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

Hi Dear you need to save datatable in DataBase you must follow concept of SQLBulkCopy, here is URL for your refrence.

http://www.4guysfromrolla.com/articles/102109-1.aspx</a>[<a href="" target="_blank"></a>]
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 6,869
CHill60 3,550
Maciej Los 3,243
ppolymorphe 2,070
Jochen Arndt 1,973


Advertise | Privacy | Mobile
Web01 | 2.8.170525.1 | Last Updated 18 Feb 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100