“从 GridView 填充 SQL 数据库”复选框



我正在尝试在网格视图部分的末尾添加一个复选框,一旦选中它就会用"1"或"0"更新sql数据库。是的,它是在列名 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"; }

    }

该复选框位于网格视图上并正常运行,但这也是它的代码。

<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"。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;

编辑2:

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

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

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

最新更新