如何使用GridView在数据库中插入值,ASP.NET中每行都有Textbox [英] How to insert values in database using GridView which have Textboxes in each row in ASP.NET

查看:76
本文介绍了如何使用GridView在数据库中插入值,ASP.NET中每行都有Textbox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个网站。我想使用GridView在数据库中插入值,每行都有文本框。当我点击按钮然后文本框

值应该转到数据库。

那怎么可能?



请告诉我?



我正在使用此代码。在构建解决方案之后没有错误但是它无法正常工作。那么错误在哪里以及我应该做什么?



.aspx代码 -

 <   div  >  
< asp:GridView ID = GridView1 runat = server AutoGenerateColumns = False

DataSourceID = SqlDataSource2 >

< >
< asp:BoundField DataField = admno HeaderText = admno

< span class =code-attribute> SortExpression = admno / >
< asp:BoundField DataField = Employeename HeaderText = Employeename

SortExpression = Employeename / >
< asp:BoundField DataField = EmployeeID HeaderText = EmployeeID

< span class =code-attribute> SortExpression = EmployeeID / >
< asp:BoundField DataField = 名称 HeaderText = 名称

SortExpression = 名称 / >

< asp:TemplateField HeaderText = 银泰 >
< ItemTemplate >
< asp:TextBox ID < span class =code-keyword> =
TextBox1 runat = server > < / asp:TextBox >
< / ItemTemplate >
< ItemStyle Horizo​​ntalAlign = 中心 / >
< / asp:TemplateField >
< asp:TemplateField HeaderText = 停机时间 > ;
< ItemTemplate >
< asp:TextBox < span class =code-attribute> ID = TextBox2 runat = 服务器 > < / asp:TextBox >
< / ItemTemplate >
< ItemStyle Horizo​​ntalAlign = 中心 / >
< / asp:TemplateField >

< /列 > ;


< / asp:GridView >
< asp:Button ID = < span class =code-keyword> BtnSave runat = server 文字 = 保存 onclick = BtnSave_Click

< span class =code-attribute>
宽度 = 124px / >
< asp:SqlDataSource ID = SqlDataSource2 runat = server ConnectionString = <% $ ConnectionStrings:cnn %>



< span class =code-attribute> SelectCommand = SELECT [admno],[Sitname],[Employeename] ,[EmployeeID],[指定] FROM [SiteAttendence1] >
< / asp:SqlDataSource >







背后的代码是: -

  protected   void  BtnSave_Click(对象发​​件人,EventArgs e)
{
string 备注;
SqlConnection cnn = new SqlConnection(connectionString);
SqlTransaction trans = null ;
尝试
{
cnn.Open();
trans = cnn.BeginTransaction();
SqlCommand cmd = new SqlCommand( insert到SiteAttendence1值(@ admno,@ Intime),cnn);
cmd.Transaction = trans;
cmd.Parameters.Add( @ admno,SqlDbType.Int);
cmd.Parameters.Add( @ Intime,SqlDbType.VarChar, 50 );


foreach (GridViewRow r GridView1.Rows)
{
remarks = ;
TextBox t =(TextBox)r.FindControl( Textbox1);
remarks = t.Text;
cmd.Parameters [ @ admno]。值= r.Cells [ 0 ]文本。
cmd.Parameters [ @ Intime]。值=备注;
cmd.ExecuteNonQuery();
}
trans.Commit();

}
catch (例外情况)
{
trans.Rollback();

}
最后
{
cnn.Close();
}
}

解决方案

ConnectionStrings:cnn %>

< span class =code-attribute>

< span class =code-attribute> SelectCommand = SELECT [admno],[Sitname],[Employeename],[EmployeeID],[Designation] FROM [SiteAttendence1] >
< / asp:SqlDataSource >







背后的代码是: -

  protected   void  BtnSave_Click( object  sender,EventArgs e)
{
字符串备注;
SqlConnection cnn = new SqlConnection(connectionString);
SqlTransaction trans = null ;
尝试
{
cnn.Open();
trans = cnn.BeginTransaction();
SqlCommand cmd = new SqlCommand( insert到SiteAttendence1值(@ admno,@ Intime),cnn);
cmd.Transaction = trans;
cmd.Parameters.Add( @ admno,SqlDbType.Int);
cmd.Parameters.Add( @ Intime,SqlDbType.VarChar, 50 );


foreach (GridViewRow r GridView1.Rows)
{
remarks = ;
TextBox t =(TextBox)r.FindControl( Textbox1);
remarks = t.Text;
cmd.Parameters [ @ admno]。值= r.Cells [ 0 ]文本。
cmd.Parameters [ @ Intime]。值=备注;
cmd.ExecuteNonQuery();
}
trans.Commit();

}
catch (例外情况)
{
trans.Rollback();

}
最后
{
cnn.Close();
}
}


您是否正在插入新行?或更新旧行?

我注意到你有控制绑定到数据源,我认为这是填充初始字段。



如果要更新插入,则需要更新


是的,有错误。当您将数据插入数据库时​​,您用于插入的插入命令不正确,因为表和您的插入命令列的列不匹配。

例如:

 SqlCommand cmd =  new  SqlCommand( 插入SiteAttendence1([admno],[Intime])值(@ admno,@ Intime),cnn); 





希望它对你有所帮助。

如果有帮助你不要忘记投票。 :)


I am developing a website. I want to insert the values in database with the use of GridView which have textboxes in each row.When I click on the button then Textboxes
values should go to the database.
So How it is possible?

Please tell me?

I am using this code.it has no error after "build" the solution but It does not work properly.So where is the error and what I shoud do?

The .aspx code-

<div>
           <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"

               DataSourceID="SqlDataSource2">

               <Columns>
               <asp:BoundField DataField="admno" HeaderText="admno"

                       SortExpression="admno" />
                   <asp:BoundField DataField="Employeename" HeaderText="Employeename"

                       SortExpression="Employeename" />
                   <asp:BoundField DataField="EmployeeID" HeaderText="EmployeeID"

                       SortExpression="EmployeeID" />
                   <asp:BoundField DataField="Designation" HeaderText="Designation"

                       SortExpression="Designation" />

                   <asp:TemplateField HeaderText="Intime">
                  <ItemTemplate>
                      <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                  </ItemTemplate>
                  <ItemStyle HorizontalAlign="Center" />
              </asp:TemplateField>
              <asp:TemplateField HeaderText="Outtime">
                  <ItemTemplate>
                      <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                  </ItemTemplate>
                  <ItemStyle HorizontalAlign="Center" />
              </asp:TemplateField>

               </Columns>


           </asp:GridView>
           <asp:Button ID="BtnSave" runat="server" Text="Save" onclick="BtnSave_Click"

               Width="124px" />
           <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:cnn %>"



             SelectCommand="SELECT [admno], [Sitname], [Employeename], [EmployeeID], [Designation] FROM [SiteAttendence1]">
           </asp:SqlDataSource>




The code behind is:-

protected void BtnSave_Click(object sender, EventArgs e)
   {
       string remarks;
       SqlConnection cnn = new SqlConnection(connectionString);
       SqlTransaction trans = null;
       try
       {
           cnn.Open();
           trans = cnn.BeginTransaction();
           SqlCommand cmd = new SqlCommand("insert into SiteAttendence1 values(@admno,@Intime)", cnn);
           cmd.Transaction = trans;
           cmd.Parameters.Add("@admno", SqlDbType.Int);
           cmd.Parameters.Add("@Intime", SqlDbType.VarChar, 50);


           foreach (GridViewRow r in GridView1.Rows)
           {
               remarks = "";
               TextBox t = (TextBox)r.FindControl("Textbox1");
               remarks = t.Text;
               cmd.Parameters["@admno"].Value = r.Cells[0].Text;
               cmd.Parameters["@Intime"].Value = remarks;
               cmd.ExecuteNonQuery();
           }
           trans.Commit();

       }
       catch (Exception ex)
       {
           trans.Rollback();

       }
       finally
       {
           cnn.Close();
       }
   }

解决方案

ConnectionStrings:cnn %>" SelectCommand="SELECT [admno], [Sitname], [Employeename], [EmployeeID], [Designation] FROM [SiteAttendence1]"> </asp:SqlDataSource>




The code behind is:-

protected void BtnSave_Click(object sender, EventArgs e)
   {
       string remarks;
       SqlConnection cnn = new SqlConnection(connectionString);
       SqlTransaction trans = null;
       try
       {
           cnn.Open();
           trans = cnn.BeginTransaction();
           SqlCommand cmd = new SqlCommand("insert into SiteAttendence1 values(@admno,@Intime)", cnn);
           cmd.Transaction = trans;
           cmd.Parameters.Add("@admno", SqlDbType.Int);
           cmd.Parameters.Add("@Intime", SqlDbType.VarChar, 50);


           foreach (GridViewRow r in GridView1.Rows)
           {
               remarks = "";
               TextBox t = (TextBox)r.FindControl("Textbox1");
               remarks = t.Text;
               cmd.Parameters["@admno"].Value = r.Cells[0].Text;
               cmd.Parameters["@Intime"].Value = remarks;
               cmd.ExecuteNonQuery();
           }
           trans.Commit();

       }
       catch (Exception ex)
       {
           trans.Rollback();

       }
       finally
       {
           cnn.Close();
       }
   }


Are you inserting new rows? or updating old rows?
I notice you have the controls bound to a data source, I assume that is to populate the initial fields.

If you are updating the your insert into will need to be update where


Yes, there is error. When you are inserting data into database, the insert command that you are using for insertion is not correct because there is mismatch in columns of table and your insert command column.
eg:

SqlCommand cmd = new SqlCommand("insert into SiteAttendence1 ([admno],[Intime]) values(@admno,@Intime)", cnn);



Hope it'll help you.
don't forget to vote if it helped you. :)


这篇关于如何使用GridView在数据库中插入值,ASP.NET中每行都有Textbox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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