如何将数据从Gridview保存到DataBase。 [英] How to save data from Gridview to DataBase.

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

问题描述

在页脚模板(文本框)中,我想保存data.Data保存在DB中,但空值保存到DB。字符串a,b,c,d,f取空。 Kindle提供适当的查询。我的代码是---



protected void RowCommand(对象发送者,GridViewCommandEventArgs e)

{

if (e.CommandName.Equals(Insert))

{

int retVal = 0;

string a =((TextBox)GridView1 .FooterRow.FindControl(TextBox1))。文本;

string b =((TextBox)GridView1.FooterRow.FindControl(TextBox2))。Text;

string c =((TextBox)GridView1.FooterRow.FindControl(TextBox3))。Text;



string d =((TextBox)GridView1.FooterRow.FindControl( TextBox4))。文字;



string f =((TextBox)GridView1.FooterRow.FindControl(TextBox5))。文字;



使用(SqlConnection Sqlcon = new SqlConnection(strcon))

{

using(SqlCommand cmd = new SqlCommand())< br $>
{

Sqlcon.Open();

cmd.Connection = Sqlcon;

cmd.CommandText =insert到Grid_to_DB(名称,年龄,城市,国家,Mobile_No)值('+ a +','+ b +',' + c +','+ d +','+ f +');

cmd.ExecuteNonQuery();

}

}

我的aspx代码是----------------

 <   asp:GridView     ID   =  GridView1    runat   = 服务器    OnRowCommand   =  RowCommand    AutoGenerateColumns   =  False >  <   >  <   asp:BoundField     HeaderText   =  Id    /  >  
< asp:TemplateField HeaderText = < span class =code-keyword>名称 >
< FooterTemplate >
< asp:TextBox ID = TextBox1 runat = 服务器 > < / asp:TextBox >
< / FooterTemplate >
< ItemTemplate >
< asp:标签 ID = Label1 runat = server 文本 =' <% #Eval( 名称%> ' > < / asp:标签 >
< / ItemTemplate >
< / asp:TemplateField >
< asp:TemplateField HeaderText = 年龄 < span class =code-keyword>>
< FooterTemplate >
< asp:TextBox ID = TextBox2 runat = 服务器 > < / asp:TextBox >
< / FooterTemplate >

< ItemTemplate >
< asp:Label ID = Label2 runat = server 文本 =' <% #Eval( 年龄%> ' > < / asp:Label >
< / ItemTemplate >
< / asp:TemplateField < span class =code-keyword>>
< asp:TemplateField HeaderText = 城市 >
< < span class =code-leadattribute> FooterTemplate >
< asp:TextBox ID = TextBox3 runat = server > < / asp:TextBox >
< / FooterTemplate >
< ItemTemplate >
< asp:标签 ID = Label3 runat = server 文本 =' <% #Eval( 城市%> ' > < / asp:标签 >
< / ItemTemplate >
< / asp:TemplateField >
< asp:TemplateField HeaderText = 国家 >
< FooterTemplate >
< asp:TextBox ID = TextBox4 runat = server > < ; / asp:TextBox >
< / FooterTemplate >
< ItemTemplate >
< asp:Label ID = Label4 runat = server 文字 =' <% #Eval( 国家/地区%> ' > < / asp:Label >
< / ItemTemplate >
< / asp:TemplateField < span class =code-keyword>>

< asp:TemplateField HeaderText = Mobile_No >
< < span class =code-leadattribute> FooterTemplate >
< asp:TextBox ID = TextBox5 runat = server > < / asp:TextBox >
< / FooterTemplate >
< ItemTemplate >
< asp:标签 ID = Label5 runat = server 文字 =' <% #Eval( Mobile_No%> ' > < / asp:标签 >
< ; / ItemTemplate >
< / asp:TemplateField >
< asp:TemplateField HeaderText = ABC > ;
< FooterTemplate >
< asp:LinkBut​​ton ID = < span class =code-keyword> LinkBut​​ton1 runat = server CommandName = 插入 > LinkBut​​ton < / asp:LinkBut​​ton >
< / FooterTemplate >
< ItemTemplate >
< asp:标签 ID = Label6 runat = server 文本 = 示例 > < / asp:Label >
< / ItemTemplate >
< / asp:Temp lateField >
< /列 >
< / asp:GridView >

解决方案

嗨Ayush,



请在gridview中添加ShowFooter =true属性并绑定你的网格



if(!Page.IsPostBack)

{

GridView1.DataSource = dt(你的数据源);

GridView1.DataBind();

}



然后你可以在绑定之前访问RowCommandEvent()


中的所有页脚值datsource to grid将表存储在datatable中并执行相同的manupulation以获取页脚结果,然后将值存储在DBB中。



_SG

  public   string  pExecuteQueryList(List< ;串GT; strSQL)
{

SqlTransaction transExecute = null ;
SqlConnection conExecute = new SqlConnection();
SqlCommand cmdExecute = new SqlCommand();
string strReturn = ;
尝试
{
conExecute.ConnectionString = ConfigurationManager.AppSettings.Get( Connectionstring);

cmdExecute.CommandTimeout = 0 ;
if (conExecute.State == ConnectionState.Closed)
conExecute.Open();
transExecute = conExecute.BeginTransaction();
for int intLoopVariable = 0 ; intLoopVariable < strSQL.Count; intLoopVariable ++)
{
cmdExecute = new SqlCommand(strSQL [intLoopVariable],conExecute);
cmdExecute.Transaction = transExecute;
cmdExecute.ExecuteNonQuery();
}
transExecute.Commit();

}
catch (SqlException sqlex)
{
if (transExecute!= null
{
transExecute.Rollback();
strReturn = sqlex.Message.Replace( ' );
}
}
catch (例外情况)
{
if (transExecute!= null
{
transExecute.Rollback();
strReturn = ex.Message.Replace( ' );
}
}
最后
{
if (conExecute.State == ConnectionState.Open)
{
cmdExecute.Dispose();
conExecute.Close();
}
}
return strReturn;
}

 protected void btnsave_Click(object sender,EventArgs e)
{
List< string> lstArray = new List< string>();
string strResult =;
if(gridview1.Rows.Count> 0)
{
for(int i = 0; i< gridview1.Rows.Count; i ++)
{


int id = Convert.ToInt32(gridview1.Rows [i] .Cells [1] .Text);
string备注= gridview1.Rows [i] .Cells [2] .Text.ToString();
//如果控件在单元格中添加,那么你应该使用下面的行
string Remark = gridview1.Rows [i] .FindControl(txtRemark)as TextBox;


lstArray.Add(插入table1(id,备注)+
values(+ id +,+ Remark +')) ;


}

}
strResult = pExecuteQueryList(lstArray);
if(strResult ==)
{
Page.ClientScript.RegisterStartupScript(GetType(),msgbox5,alert('Data Saved');,true);
}
}


In Footer Template (Textbox) i want to save data.Data is saved in DB but null value going to DB. string a,b,c,d,f take null. Kindle give proper Query. My Code is---

protected void RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("Insert"))
{
int retVal = 0;
string a = ((TextBox)GridView1.FooterRow.FindControl("TextBox1")).Text;
string b = ((TextBox)GridView1.FooterRow.FindControl("TextBox2")).Text;
string c = ((TextBox)GridView1.FooterRow.FindControl("TextBox3")).Text;

string d = ((TextBox)GridView1.FooterRow.FindControl("TextBox4")).Text;

string f = ((TextBox)GridView1.FooterRow.FindControl("TextBox5")).Text;

using (SqlConnection Sqlcon = new SqlConnection(strcon))
{
using (SqlCommand cmd = new SqlCommand())
{
Sqlcon.Open();
cmd.Connection = Sqlcon;
cmd.CommandText = "insert into Grid_to_DB (Name,Age,City,Country,Mobile_No) values ('" + a + "','" + b + "','" + c + "','" + d + "','" + f + "')";
cmd.ExecuteNonQuery();
}
}
My aspx code is ----------------

<asp:GridView ID="GridView1" runat="server" OnRowCommand="RowCommand" AutoGenerateColumns="False"><Columns><asp:BoundField HeaderText="Id" />
<asp:TemplateField HeaderText="Name">
<FooterTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Age">
<FooterTemplate>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</FooterTemplate>
 <ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("Age") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City">
<FooterTemplate>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("City") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Country">
<FooterTemplate>
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("Country") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Mobile_No">
<FooterTemplate>
<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Eval("Mobile_No") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ABC">
<FooterTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Insert">LinkButton</asp:LinkButton>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text="Example"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

解决方案

Hi Ayush,

Kindly add ShowFooter="true" property in gridview and bind your grid in

if (!Page.IsPostBack)
{
GridView1.DataSource = dt(your datasource);
GridView1.DataBind();
}

Then you can access all the footer values in RowCommandEvent()


before binding the datsource to grid store the table in datatable and do the same manupulation for getting the footer result and then store the value in DBB.

_SG


public string pExecuteQueryList(List<string> strSQL)
    {

        SqlTransaction transExecute = null;
        SqlConnection conExecute = new SqlConnection();
        SqlCommand cmdExecute = new SqlCommand();
        string strReturn = "";
        try
        {
            conExecute.ConnectionString = ConfigurationManager.AppSettings.Get("Connectionstring");

            cmdExecute.CommandTimeout = 0; 
            if (conExecute.State == ConnectionState.Closed)
                conExecute.Open();
            transExecute = conExecute.BeginTransaction();
            for (int intLoopVariable = 0; intLoopVariable < strSQL.Count; intLoopVariable++)
            {
                cmdExecute = new SqlCommand(strSQL[intLoopVariable], conExecute);
                cmdExecute.Transaction = transExecute;
                cmdExecute.ExecuteNonQuery();
            }
            transExecute.Commit();

        }
        catch (SqlException sqlex)
        {
            if (transExecute != null)
            {
                transExecute.Rollback();
                strReturn = sqlex.Message.Replace("'", "");
            }
        }
        catch (Exception ex)
        {
            if (transExecute != null)
            {
                transExecute.Rollback();
                strReturn = ex.Message.Replace("'", "");
            }
        }
        finally
        {
            if (conExecute.State == ConnectionState.Open)
            {
                cmdExecute.Dispose();
                conExecute.Close();
            }
        }
        return strReturn;
    }

 protected void btnsave_Click(object sender, EventArgs e)
    {
        List<string> lstArray = new List<string>();
        string strResult = "";
        if (gridview1.Rows.Count > 0)
        {
            for (int i = 0; i < gridview1.Rows.Count; i++)
            {


                int id =Convert.ToInt32(gridview1.Rows[i].Cells[1].Text);
                string Remark = gridview1.Rows[i].Cells[2].Text.ToString();
//if control added in cell then you should use below line
string Remark = gridview1.Rows[i].FindControl("txtRemark") as TextBox;


                lstArray.Add("insert into table1(id,Remarks)" +
" values(" + id + ",'" + Remark + "')");


            }

        }
        strResult = pExecuteQueryList(lstArray);
        if(strResult=="")
        {
            Page.ClientScript.RegisterStartupScript(GetType(), "msgbox5", "alert('Data Saved');", true);
        }
    }


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

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