Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么第二列值没有插入到我的数据库中?_C#_Asp.net - Fatal编程技术网

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

C# 为什么第二列值没有插入到我的数据库中?,c#,asp.net,C#,Asp.net,我的数据库中有一个表PRESTEST(IDPRESTEST,nameTest) 从Gridview中,它应该在其中插入第二列值(“NameTest”),但它却在表中插入(“idTest”),为什么 我试图改变c#中的列计数,但仍然没有任何改变 CREATE TABLE [dbo].[PrescTest]( [idPrescTest] [int] IDENTITY(1,1) NOT NULL, [nameTest] [nvarchar](50) NOT NULL, CONSTRAINT [PK_P

我的数据库中有一个表PRESTEST(IDPRESTEST,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();

            }
        }
    }
创建表[dbo]。[prestest](
[IDPRESSTEST][int]标识(1,1)不为空,
[nameTest][nvarchar](50)不为空,
约束[PK_PRECEX]主键群集
(
[idPrescTest]ASC
)在[主]上打开(PAD\u INDEX=OFF,STATISTICS\u norecocomputer=OFF,IGNORE\u DUP\u KEY=OFF,ALLOW\u ROW\u LOCKS=ON,ALLOW\u PAGE\u LOCKS=ON)
)在[小学]
去
受保护的无效按钮1\u单击1(对象发送者,事件参数e)
{
foreach(GridView1.Rows中的GridViewRow行)
{
if((作为复选框的行FindControl(“CheckBox1”)).Checked)
{
SqlCommand cmd=new-SqlCommand(“插入到prestest(nameTest)值(@Test)”,conn);
cmd.Parameters.Add(“Test”,SqlDbType.NVarChar,50).Value=row.Cells[2].Text;
conn.Open();
cmd.ExecuteNonQuery();
康涅狄格州关闭();
}
}
}
好吧,我找到了一个解决方案,但我不确定为什么它会起作用

我只将此代码放在边界字段之后

<asp:TemplateField>

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

            </asp:TemplateField>

这可能适合您

使用
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>

cmd.Parameters.Add(“Test”,SqlDbType.NVarChar,50).Value=row.Cells[2].Text;我认为列计算中的问题如果你能列出完整的表信息,如单个列的数据类型和默认值等,那就太好了@Tayyab好的,我现在把表信息放进去。
cmd.Parameters.Add(“Test”)…
应该是
cmd.Parameters.Add(@Test”..
不确定这是问题所在,但值得一试。@ZoharPeled No我试过:(仍在数据库中插入idTest:(您得到了什么值((HiddenField)row.FindControl(“hdnnameTest”)。调试程序时的值?
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();

        }
    }
}