从gridview复选框填充sql数据库

时间:2014-04-01 16:11:39

标签: c# asp.net sql gridview checkbox

我正在尝试在gridview部分的末尾添加一个复选框,一旦检查它就会使用" 1"更新sql数据库。或" 0"。是的,它是用列名posFill完成的。

这是代码......

protected void gvPerson_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["sqlconnection"].ToString()))
        {

            SqlCommand cmd = new SqlCommand();


            cmd.Connection = conn;


            cmd.CommandText = "UPDATE requests SET date = @date, submitted_by = @submitted_by, position = @position, district = @district, base_store = @base_store, travel_to = @travel_to, open_til6 = @open_til6, email_add = @email_add, comments = @comments, posFill = @posFill, interviewDate = @interviewDate  WHERE _id = @_id";


            cmd.CommandType = CommandType.Text;

            // Get the PersonID of the selected row.
            string strID = gvPerson.Rows[e.RowIndex].Cells[2].Text;
            string strPosition = ((TextBox)gvPerson.Rows[e.RowIndex].FindControl("TextBox1")).Text;
            string strEmail_add = ((TextBox)gvPerson.Rows[e.RowIndex].FindControl("TextBox2")).Text;
            string strDate = ((TextBox)gvPerson.Rows[e.RowIndex].FindControl("TextBox3")).Text;
            string strSubBy = ((TextBox)gvPerson.Rows[e.RowIndex].FindControl("TextBox4")).Text;
            string strDist = ((TextBox)gvPerson.Rows[e.RowIndex].FindControl("TextBox5")).Text;
            string strBase = ((TextBox)gvPerson.Rows[e.RowIndex].FindControl("TextBox6")).Text;
            string strTravel = ((TextBox)gvPerson.Rows[e.RowIndex].FindControl("TextBox7")).Text;
            string strOpen = ((TextBox)gvPerson.Rows[e.RowIndex].FindControl("TextBox8")).Text;
            string strComments = ((TextBox)gvPerson.Rows[e.RowIndex].FindControl("TextBox9")).Text;
            //string strFilled = ((TextBox)gvPerson.Rows[e.RowIndex].FindControl("TextBox10")).Text;
            string strIntDate = ((TextBox)gvPerson.Rows[e.RowIndex].FindControl("TextBox11")).Text;
            string strLblFilled = ((Label)gvPerson.Rows[e.RowIndex].FindControl("lblFilled")).Text;

            // Append the parameters.
            cmd.Parameters.Add("@_id", SqlDbType.Int).Value = strID;
            cmd.Parameters.Add("@position", SqlDbType.NVarChar, 50).Value = strPosition;
            cmd.Parameters.Add("@email_add", SqlDbType.NVarChar, 50).Value = strEmail_add;
            cmd.Parameters.Add("@date", SqlDbType.Date).Value = strDate;
            cmd.Parameters.Add("@submitted_by", SqlDbType.NVarChar, 50).Value = strSubBy;
            cmd.Parameters.Add("@district", SqlDbType.NVarChar, 50).Value = strDist;
            cmd.Parameters.Add("@base_store", SqlDbType.NVarChar, 50).Value = strBase;
            cmd.Parameters.Add("@travel_to", SqlDbType.NVarChar, 50).Value = strTravel;
            cmd.Parameters.Add("@open_til6", SqlDbType.NVarChar, 50).Value = strOpen;
            cmd.Parameters.Add("@comments", SqlDbType.NVarChar, 50).Value = strComments;
            //cmd.Parameters.Add("@posFilled", SqlDbType.NVarChar, 50).Value = strFilled;
            cmd.Parameters.Add("@interviewDate", SqlDbType.Date).Value = strIntDate;
            cmd.Parameters.Add("@posFill", SqlDbType.Bit).Value = strLblFilled;

            // Open the connection.
            conn.Open();

            // Execute the command.
            cmd.ExecuteNonQuery();
        }

        // Exit edit mode.
        gvPerson.EditIndex = -1;

        // Rebind the GridView control to show data after updating.
        BindGridView();

        // Show the Add button.
        lbtnAdd.Visible = true;
    }

这是复选框的功能......

protected void posFilled_CheckChanged(object sender, System.EventArgs e)
    {
        if (chkFilled.Checked == true)
        { lblFilled.Text = "1"; }
        else
        { lblFilled.Text = "0"; }


    }

复选框位于gridview上并正常运行,但这里也是代码。

<asp:TemplateField HeaderText="Filled" SortExpression="posFill">
                <EditItemTemplate>
                    <asp:CheckBox ID="chkFilled" runat="server" AutoPostBack="true" />
                    <asp:Label ID="lblFilled" runat="server" Visible="true"></asp:Label>
                    <%--<asp:TextBox ID="TextBox10" runat="server" Text='<%# Bind("posFilled") %>'></asp:TextBox>--%>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:CheckBox ID="chkFilled2" runat="server"/>
                    <asp:Label ID="lblFilled" runat="server" Visible="true"></asp:Label>
                    <%--<asp:Label ID="Label10" runat="server" Text='<%# Bind("posFilled") %>'></asp:Label>--%>
                </ItemTemplate>

提前谢谢你。我添加了一个标签,以查看响应是什么,或者它是否响应,如果确实如此,那么只使用标签内容。但不愿意。

1 个答案:

答案 0 :(得分:1)

不是使用“1”.ToString()或“0”.ToString(),为什么不直接使用控件呢?您不需要标签。

    cmd.Parameters.Add("@posFill", SqlDbType.Bit).Value = chkFilled.Checked;

编辑:

 cmd.Parameters.Add("@posFill", SqlDbType.Bit).Value = ((CheckBox)gvPerson.Rows[e.RowIndex].FindControl("chkFilled")).Checked;

EDIT2:

您只有一个int列。您应该这样做以确保您输入的值与参数的数据类型匹配:

 int tempInt = -1;

 if (int.TryParse(strID, out tempInt))
 {
      cmd.Parameters.Add("@_id", SqlDbType.Int).Value = tempInt;

      cmd.ExecuteNonquery();
 }

实际上,在尝试执行命令之前,应该验证所有数据。您应该为所有参数分配正确的数据类型。如果你传递一个SqlDbType.Date,你应该传递一个DateTime变量,它应该实际解析为DateTime。