如何将GridView文本框值保存到数据库? [英] How to save gridview textbox values into database?

查看:63
本文介绍了如何将GridView文本框值保存到数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将gridview文本框值保存到数据库中?

How to save gridview textbox values into database?

推荐答案

页面.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



BLL

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


DAL


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


这篇关于如何将GridView文本框值保存到数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆