ASP.NET - How to save gridview data into sql using store procedure & LINQ

Asked By bhanupratap singh
11-Feb-12 07:22 AM
I have gridview. Data has been inserted into gridview trhough dynamically created row. Now I want to Save data into table on Button Click Even.
Thanks
  Sreekumar P replied to bhanupratap singh
11-Feb-12 07:51 AM
Hi,

Options

1. This just simple that u can Iterate thru the GridViewRows and get the data.

foreach (GridViewRow gr in girdView1.Rows)
       {
         //if using template colums
         string value = ((Label)gr.FindControl("lblControl")).Text;
         //if using AutoGenerateColums
         string value2 = gr.Cells[0].Text;
       }

2. How u are adding data to the GridView.. ? If using a DataTable den Store that DataTable somewhere over ViewState and get the data from that.

public DataTable GridViewData
     {
       get
       {
         if (ViewState["gridViewData"] == null)
           ViewState["gridViewData"] = new DataTable();
         return (DataTable)ViewState["gridViewData"];
       }
     }



This link will help u how to Save data with LINQ http://msdn.microsoft.com/en-us/library/bb399407.aspx
  dipa ahuja replied to bhanupratap singh
11-Feb-12 07:59 AM
void bind()
{
  foreach (GridViewRow row in GridView1.Rows)
  {
    string name = ((Label)row.FindControl("label1")).Text.ToString();
    string gender = ((DropDownList)row.FindControl("dropdown1")).SelectedValue.ToString();
    string contact = ((Label)row.FindControl("lblcontact")).Text.ToString();
 
    //code to insert
  }
}
  kalpana aparnathi replied to bhanupratap singh
11-Feb-12 12:32 PM
Try this below code

protected void SaveButton(object sender, EventArgs e)
  {
    SqlConnection conn = new SqlConnection(@"your_connection_string");
    conn.Open();
    foreach (GridViewRow gvr in GridView1.Rows)
    {
      string data1 = gvr.Cells[0].Text;    //get data from BoundField in each row
      string data2 = ((Label)gvr.FindControl("Label1")).Text;    //get data from control in TemplateField in each row
      SqlCommand cmd = new SqlCommand("insert_sql", conn);   //generate insert sql using above data
      cmd.ExecuteNonQuery();
    }
    conn.Close();
  }

Create New Account
help
center" bgcolor = "#CC3300"> Email_address < / td> <td style = "width: 112px;" align = "center" bgcolor = "#CC3300"> Organisation <asp:DropDownList ID = "ddlorganisation" DataSourceID = "dsPopulateorganisation" AutoPostBack = "true" DataValueField = "Organisation" runat = "server" Width = "100px" Font-Size = "11px" AppendDataBoundItems = "true"> <asp:ListItem Text = "All" Value = "%"> < / asp:ListItem> < / asp:DropDownList> < / td> <td style = "width: 120px;" align = "center" bgcolor = "#CC3300"> Org_type <asp DropDownList ID = "ddlorgtype" DataSourceID = "dsPopulateorgtype" AutoPostBack = "true" DataValueField = "Org_type" runat = "server" Width = "100px" Font-Size = "11px" AppendDataBoundItems = "true"> <asp:ListItem Text = "All" Value = "%"> < / asp:ListItem> < / asp:DropDownList> < / td> <td style = "width: 115px;" align = "center" bgcolor = "#CC3300"> Client_PartnerId <asp:DropDownList ID = "ddlclientpartnerid" DataSourceID = "dsPopulateclientpartnerid" AutoPostBack = "true" DataValueField = "Client_PartnerId" runat = "server" Width = "100px" Font-Size = "11px AppendDataBoundItems = "true"> <asp:ListItem Text = "All" Value = "%"> < / asp:ListItem> < / asp:DropDownList> < / td> <td style = "width: 115px;" align = "center" bgcolor = "#CC3300"> Work_type <asp:DropDownList ID = "ddlworktype" DataSourceID = "dsPopulateworktype" AutoPostBack = "true" DataValueField = "Work_type" runat = "server" Width = "100px" Font-Size = "11px AppendDataBoundItems = "true"> <asp:ListItem Text = "All" Value = "%"> < / asp:ListItem> < / asp:DropDownList> < / td> <td style = "width: 118px;" align = "center" bgcolor = "#CC3300"> MK_mailinglist <asp:DropDownList ID = "ddlmkmailinglist" DataSourceID
gridview I have to insert data from gridview.I have two gridview with textboxes and dropdownlist. I have added textbox, dropdownlist item template inside the gridview now i have to insert data from this textbox, dropdownlist to database on a button click.but this button lies outside the gridview .can anyone aspdotnet-suresh.com / 2011 / 02 / how-to-inserteditupdate-and-delete-data.html void Insert() { foreach ( GridViewRow row in GridView1.Rows) { string name = (( Label )row.FindControl( "label1" )).Text.ToString(); string gender = (( DropDownList )row.FindControl( "dropdown1" )).SelectedValue.ToString(); string contact = (( Label )row.FindControl( "lblcontact" )).Text.ToString(); / / code to object sender, GridViewUpdateEventArgs e) { if (((LinkButton)GridView1.Rows[0].Cells[0].Controls[0]).Text = = "Insert" ) { SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[ "exam_moduleConnectionString" ].ConnectionString); SqlCommand cmd = new SqlCommand(); cmd.CommandText = "INSERT INTO quest_categories(cat_name) VALUES TextBox)GridView1.Rows[0].Cells[2].Controls[0]).Text; cmd.Connection = con; con.Open(); cmd.ExecuteNonQuery(); con.Close(); } else { SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[ "exam_moduleConnectionString" ].ConnectionString); SqlCommand cmd = new SqlCommand
empid" onrowcancelingedit = "GridView2_RowCancelingEdit" onrowdeleting = "GridView2_RowDeleting" onrowediting = "GridView2_RowEditing" onrowupdating = "GridView2_RowUpdating" onselectedindexchanged = "Gridiew2_SelectedIndexChanged" onrowcommand = "GridView2_RowCommand" > <Columns> <asp:TemplateField HeaderText = "Select"> <ItemTemplate> <asp:CheckBox ID = "CheckBox1" runat = "server" / > < / ItemTemplate> < / asp:TemplateField> <asp:BoundField DataField = "empid" HeaderText = "empid" / > <asp:BoundField DataField = "empname" HeaderText = "empname" / > <asp:TemplateField HeaderText = "dept"> <ItemTemplate> <%# Eval("dep") %> < / ItemTemplate> <EditItemTemplate> <asp DropDownList runat = "server" DataSourceID = "SqlDataSource1" ID = "DropDeptId" DataTextField = "Deptid" DataValueField = "DeptId" / > < / EditItemTemplate> < / asp:TemplateField> <asp:CommandField ShowEditButton = "true" ShowDeleteButton = "true" / > < / Columns> < / asp:GridView> .cs code- Function to bind Grid private void getdata() { SqlConnection con = new SqlConnection("Data Source = 10.0.2.8;Initial Catalog = JitendraDB;User ID = sa;password = change_123"); SqlDataAdapter RowIndex].Cells[1].Text; / / FOR GETTING ENPID string deleteQuery = "delete from emp where empid = ' " + strEmpId + " ' "; SqlConnection cn = new SqlConnection("CONNECTION STRING"); cn.Open(); SqlCommand cmd = new SqlCommand(deleteQuery, cn); cmd.ExecuteNonQuery
column as follows < asp : GridView ID = "GridView1" runat = "server" AutoGenerateColumns = "False" onselectedindexchanged = "GridView1_SelectedIndexChanged"> < Columns > < asp : TemplateField HeaderText = "SceneNo" SortExpression = "SceneNO"> < ItemTemplate > < asp : Label ID = "LblGridSceneNo" runat = "server" Text = ' <% # Bind("UserId") %> '> < / asp : Label > < / ItemTemplate > < / asp : TemplateField > < asp : TemplateField HeaderText = "ShotNo" SortExpression = "ShotNo"> < ItemTemplate > < asp : Label ID = "LblGridShotNo" runat = "server" Text = ' <% # Bind("Passwords") %> '> < / asp Label > < / ItemTemplate > < / asp : TemplateField > < asp : TemplateField HeaderText = "Date Sent" SortExpression = "DateSent"> < ItemTemplate > < asp : TextBox ID = "TextBox1" runat = "server" > < / asp : TextBox > < / ItemTemplate > < / asp : TemplateField > < / Columns > < / asp : GridView > i will enter the data in the textbox after running it. i method: / / A method that Inserts the records to the database private void InsertRecords( StringCollection sc) { SqlConnection conn = new SqlConnection (GetConnectionString()); StringBuilder sb = new StringBuilder ( string .Empty); string [] splitItems = null ; foreach ( string item in sc
how to bind dropdownlist in gridview in asp.net hi, can any one tell me how to bind dropdownlist in gridview in asp.net HI try this C# Code for ASP.Net GridView Control database connectivity as well as functionality to display the data in GridView and selectedValue of DropDownList control placed in it. C# Code for ASP.Net GridView DropDownList DataSource / / Here new SQL query is pass to the SQL command object to get the SqlCommand("select * from tbl_order_status", mySQLconnection); mySqlAdapter = new SqlDataAdapter(mySqlCommand); myDataSet = new DataSet(); mySqlAdapter.Fill(myDataSet); / / DropDownList Control Object Created to bind the data dynamically with each nested DropDownlist control placed inside the template column of the GridView Control. DropDownList drdList; / / foreach loop is used to loop through each row of GridView Control. foreach GridViewRow grdRow in GridView1.Rows) { / / Nested DropDownList Control reference is passed to the DrdList object. This