Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#2.0 ASP>NET
How to save gridview textbox values into database?
Posted 23-Nov-12 1:22am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

page.aspx
<asp:TemplateField HeaderText="Maxmarks">
                               <ItemTemplate>
                                   <asp:TextBox ID="TextBox2" runat="server" Text='<%=(TextBox1.Text) %>'></asp:TextBox>
                               </ItemTemplate>
                           </asp:TemplateField>
 
page.aspx.cs
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
protected int insert(string value)
{
  dal ob=new dal();
  int i=ob.insertion(value);
return i;
}
DAL
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
create procedure procedurename
@TextFieldvalue varchar(50);
as
begin
Insert into tablename (name)
 Values(@TextFieldvalue)
end
  Permalink  
v3

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

  Print Answers RSS
0 OriginalGriff 190
1 Jochen Arndt 165
2 PIEBALDconsult 150
3 BillWoodruff 148
4 DamithSL 140
0 OriginalGriff 5,695
1 DamithSL 4,591
2 Maciej Los 4,012
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,190


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 23 Nov 2012
Copyright © CodeProject, 1999-2014
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