为什么第二个列值未插入数据库中?

时间:2019-05-22 09:53:14

标签: c# asp.net

我的数据库中有一个表PrescTest(idPrescTest,nameTest)

应该从Gridview中插入第二列值(“ NameTest”),但是为什么要在表中插入(“ idTest”)?

我尝试更改c#中的列数,但仍然没有改变

CREATE TABLE [dbo].[PrescTest](
[idPrescTest] [int] IDENTITY(1,1) NOT NULL,
[nameTest] [nvarchar](50) NOT NULL,
CONSTRAINT [PK_PrescEx] PRIMARY KEY CLUSTERED 
(
[idPrescTest] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO



 <asp:GridView ID="GridView1" runat="server" DataKeyNames="idTest" Width="1121px" CellPadding="0" ForeColor="#333333" GridLines="None" AutoGenerateColumns="False" >
            <AlternatingRowStyle BackColor="White" />
            <Columns>
                <asp:CheckBoxField />

                <asp:TemplateField>

                    <ItemTemplate>
                        <asp:CheckBox ID="CheckBox1" runat="server" />
                    </ItemTemplate>

                </asp:TemplateField>

            <asp:BoundField HeaderText="idTest" DataField="idTest" />
            <asp:BoundField HeaderText="nameTest" DataField="nameTest" />
            <asp:BoundField HeaderText="TestCost" DataField="TestCost" />
            <asp:BoundField HeaderText="TestReduction" DataField="TestReduction" />

            </Columns>
</asp:GridView>





protected void Button1_Click1(object sender, EventArgs e)
    {
        foreach (GridViewRow row in GridView1.Rows)
        {
            if ((row.FindControl("CheckBox1") as CheckBox).Checked)
            {
                SqlCommand cmd = new SqlCommand("insert into PrescTest (nameTest) values (@Test)", conn);
                cmd.Parameters.Add("Test", SqlDbType.NVarChar, 50).Value = row.Cells[2].Text;

                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();

            }
        }
    }

好的,所以我找到了一个解决方案,但我不确定为什么会起作用...

我只将这段代码放在BoundFields后面

<asp:TemplateField>

                <ItemTemplate>
                    <asp:CheckBox ID="CheckBox1" runat="server" />
                </ItemTemplate>

            </asp:TemplateField>

1 个答案:

答案 0 :(得分:1)

这可能对您有用

使用HiddenField

<asp:GridView ID="GridView1" runat="server" DataKeyNames="idTest" 
     Width="1121px" CellPadding="0" ForeColor="#333333" GridLines="None" AutoGenerateColumns="False">
     <AlternatingRowStyle BackColor="White" />
     <Columns>
         <asp:CheckBoxField />
         <asp:TemplateField>
             <ItemTemplate>
                 <asp:CheckBox ID="CheckBox1" runat="server" />
                 <asp:HiddenField ID="hdnnameTest" runat="server" Value='<%# Eval("nameTest")%>' />
             </ItemTemplate>
         </asp:TemplateField>
         <asp:BoundField HeaderText="idTest" DataField="idTest" />
         <asp:BoundField HeaderText="nameTest" DataField="nameTest" />
         <asp:BoundField HeaderText="TestCost" DataField="TestCost" />
         <asp:BoundField HeaderText="TestReduction" DataField="TestReduction" />
    </Columns>
</asp:GridView>

在文件后面的代码中

protected void Button1_Click1(object sender, EventArgs e)
{
    foreach (GridViewRow row in GridView1.Rows)
    {
        if ((row.FindControl("CheckBox1") as CheckBox).Checked)
        {
            SqlCommand cmd = new SqlCommand("insert into PrescTest (nameTest) values (@Test)", conn);
            cmd.Parameters.Add("Test", SqlDbType.NVarChar, 50).Value = ((HiddenField)row.FindControl("hdnnameTest")).Value;

            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();

        }
    }
}