Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.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/6/ant/2.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_Gridview - Fatal编程技术网

C# 如何避免将不需要的值插入数据库?

C# 如何避免将不需要的值插入数据库?,c#,asp.net,gridview,C#,Asp.net,Gridview,我只想插入选定的值,但在保存该值时,我会在数据库中插入所有值 在上述数据库的subject列中,我只想要有教员的科目。但在我的数据库中,所有科目都在插入 我正在使用boundfield作为GridView中的主题列。到目前为止我的代码` <asp:GridView ID="Gvassignsubject" runat="server" AutoGenerateColumns="False" OnRowDataBound="Gvassignsubject_RowDataBound">

我只想插入选定的值,但在保存该值时,我会在数据库中插入所有值

在上述数据库的
subject
列中,我只想要有教员的科目。但在我的数据库中,所有科目都在插入

我正在使用
boundfield
作为
GridView
中的主题列。到目前为止我的代码`

<asp:GridView ID="Gvassignsubject" runat="server" AutoGenerateColumns="False" OnRowDataBound="Gvassignsubject_RowDataBound">
        <Columns>
            <asp:BoundField DataField="Subject" HeaderText="subject" SortExpression="subject" />
            <asp:TemplateField HeaderText ="Faculty">
                <ItemTemplate>
                    <asp:Label ID ="lblfaculty" runat="server" Text='<%%# Eval("facultyname") %>>' Visible="false"/>
                    <asp:DropDownList ID="ddlfaculty" runat="server"></asp:DropDownList>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Set Hour">
                <ItemTemplate>
                     <asp:TextBox ID="txthour" runat="server"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
             </Columns>
    </asp:GridView>`


 protected void btnsubmit_Click(object sender, EventArgs e)
    {
        using (SqlConnection con2 = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
        {
            foreach (GridViewRow r in Gvassignsubject.Rows)
            {
                for (int i = 0; i < Gvassignsubject.Rows.Count; i++)
                {
                    string batch = ddlbatches.SelectedValue;
                    string subject = r.Cells[0].Text;
                    DropDownList ddlfaculty = (DropDownList)Gvassignsubject.Rows[i].FindControl("ddlfaculty");
                    TextBox txthour = (TextBox)Gvassignsubject.Rows[i].FindControl("txthour");
                    string facultyname = ddlfaculty.SelectedValue;
                    string sethour = txthour.Text;
                    con2.Open();
                    SqlCommand cmd = new SqlCommand("insert into assign (batch,facultyname,sethour,subject)values(@batch,@facultyname,@sethour,@subject)", con2);
                    cmd.Parameters.AddWithValue("@batch", batch);
                    cmd.Parameters.AddWithValue("@subject", subject);
                    cmd.Parameters.AddWithValue("@facultyname", facultyname);
                    cmd.Parameters.AddWithValue("@sethour", sethour);
                    cmd.Connection = con2;
                    cmd.ExecuteNonQuery();
                    con2.Close();
                }
            }
        }
    }

`
受保护的void btnsupmit\u单击(对象发送者,事件参数e)
{
使用(SqlConnection con2=新的SqlConnection(ConfigurationManager.ConnectionString[“ConnectionString”].ConnectionString))
{
foreach(Gvassignsubject.Rows中的GridViewRow r)
{
对于(int i=0;i
它不考虑添加所有行,因为您没有标准

    for (int i = 0; i < Gvassignsubject.Rows.Count; i++)
    {

        DropDownList ddlfaculty = (DropDownList)Gvassignsubject.Rows[i].FindControl("ddlfaculty");
        string facultyname = ddlfaculty.SelectedValue;

        if (!string.Equals(ddlfaculty.SelectedValue, "Please Select"))
        {
            string batch = ddlbatches.SelectedValue;
            string subject = r.Cells[0].Text;
            TextBox txthour = (TextBox)Gvassignsubject.Rows[i].FindControl("txthour");
            string sethour = txthour.Text;
            con2.Open();
            SqlCommand cmd = new SqlCommand("insert into assign (batch,facultyname,sethour,subject)values(@batch,@facultyname,@sethour,@subject)", con2);
            cmd.Parameters.AddWithValue("@batch", batch);
            cmd.Parameters.AddWithValue("@subject", subject);
            cmd.Parameters.AddWithValue("@facultyname", facultyname);
            cmd.Parameters.AddWithValue("@sethour", sethour);
            cmd.Connection = con2;
            cmd.ExecuteNonQuery();
            con2.Close();
        }
    }
for(int i=0;i
我认为您必须将默认选择列表项值设置为
null
,并在
for
循环中检查它:

for(int i=0;i}

您可以在“主题”字段中设置一个条件来检查值是否存在。您可以按如下方式执行此操作:

if(facultyname!=“和&facultyname!=“请选择”)
{
SqlCommand cmd=new-SqlCommand(“插入到赋值(批处理、facultyname、sethour、主题)值(@batch、@facultyname、@sethour、@subject)”,con2);
cmd.Parameters.AddWithValue(“@batch”,batch);
cmd.Parameters.AddWithValue(“@subject”,subject);
cmd.Parameters.AddWithValue(“@facultyname”,facultyname);
cmd.Parameters.AddWithValue(“@sethour”,sethour);
cmd.Connection=con2;
cmd.ExecuteNonQuery();
}

这是考虑到DDL功能下降的情况 具有与其文本相同的值。

请尝试此操作

if(ddlfaculty.selectedindex !=-1 )
{
do the insert function
}

请检查ddlfuncil.SelectedValue,就像(!(ddlfuncil.SelectedValue.ToString()=“请选择”)在将其分配给facultyname并进行插入之前。这不起作用,结果相同。foreach是否有其他替代方法?请您建议facultyname的值是多少,其中`string facultyname=ddlfaculty.SelectedValue;`当它在数据库中没有值时?facultyname是从dropdownlist(ddlfaculty)中选择的@KevinAndrid在显示“请选择”时,您能否建议DDLFuncil.SelectedValue的值在datagrid上?我尝试了这个。但输出是相同的。它显示数据库中的所有主题。foreach是否有其他选项?不应该是这样,因为条件会阻止添加带有默认下拉列表值的行,您是否在调试器中逐步检查了您的代码,以查看它是否正在执行所需的操作?您能说出什么是
ddlf吗aculty.SelectedValue
循环中不希望插入到DB的行的值?