Click here to Skip to main content
15,922,696 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to save gridview textbox values into database?
Posted

1 solution

page.aspx
XML
<asp:TemplateField HeaderText="Maxmarks">
                               <ItemTemplate>
                                   <asp:TextBox ID="TextBox2" runat="server" Text='<%=(TextBox1.Text) %>'></asp:TextBox>
                               </ItemTemplate>
                           </asp:TemplateField>

page.aspx.cs

C#
protected void btnEnter_Click(object sender, EventArgs e)
      {

        string value=string.Empty;
          foreach (GridViewRow row in grvTest.Rows)
          {
              TextBox txt = (TextBox)row.FindControl("txtTextBoxInGrid");
              value.text=txt.text;
               int i=save(value);
               if(i>0)
                {
                 //alert to show successful
                 ClientScript.RegisterClientScriptBlock(GetType(), "sas", "<script> alert('Inserted successfully');</script>", false);
                }
               else
               {
                //alert to show unsuccessful
ClientScript.RegisterClientScriptBlock(GetType(), "sas", "<script> alert('Insertion Unsuccessfully');</script>", false);
               }
          }
      }
protected int save(string value)
{
bll obj=new bll();
int i=obj.insert(value);
return i;
}


BLL
C#
protected int insert(string value)
{
  dal ob=new dal();
  int i=ob.insertion(value);
return i;
}

DAL
C#
protected int insertion(string value)
{
//connection to the database;
SqlConnection con=new SqlConnection("datasource;inital catalog;user id;password");
SqlCommand cmd= SqlCommand("Stored Procedure name",con);
cmd.CommandType=CommandType.StoredProcedure;
cmd.Parameters.add("@TextFieldvalue", SqlDbType.VarChar);
con.Open();
int i= cmd.ExecuteNonQuery();
sqlConn.Close();
return i;
}

stored procedure
SQL
create procedure procedurename
@TextFieldvalue varchar(50);
as
begin
Insert into tablename (name)
 Values(@TextFieldvalue)
end
 
Share this answer
 
v3

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