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

时间:2015-05-11 06:36:51

标签: c# asp.net gridview

我想只插入所选的值,但是当我保存其插入数据库中的所有值时。enter image description here enter image description here

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

4 个答案:

答案 0 :(得分:0)

它添加所有行,因为你没有标准;

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

答案 1 :(得分:0)

我认为您必须将默认选择列表项值设置为null并在for循环中检查它:

for (int i = 0; i < Gvassignsubject.Rows.Count; i++) { if(!string.IsNullOrEmpty(ddlfaculty.SelectedValue)) { ... } }

答案 2 :(得分:0)

您可以在主题字段中放置一个检查值是否存在的条件。你可以这样做:

if(facultyname !="" && facultyname !="Please Select") { 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(); }

这是考虑ddlfaculty下拉的条件                   具有与其文本相同的值。

答案 3 :(得分:0)

试试这个

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