gpt4 book ai didi

c# - 为什么第二个列值没有插入到我的数据库中?

转载 作者:太空宇宙 更新时间:2023-11-03 12:03:05 25 4
gpt4 key购买 nike

我的数据库中有一个表 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>

最佳答案

这可能对你有用

使用隐藏字段

<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();

}
}
}

关于c# - 为什么第二个列值没有插入到我的数据库中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56254075/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com