2014-04-01 98 views
0

我想添加一个复选框到gridview部分的末尾,一旦选中它就用“1”或“0”更新sql数据库。是的,它完成了与列名posFill位。从gridview填充sql数据库复选框

这里是代码....

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> 

谢谢你提前。我添加了一个标签,看看会有什么回应,或者它是否回应,如果是,那么就使用标签内容。但宁愿不。

+0

有问题吗?请清楚地说明你在询问什么 – Pseudonym

+0

我试图在gridview部分的末尾添加一个复选框,一旦选中它,就会用“1”或“0”更新sql数据库。是的,它完成了与列名posFill位。 – bpaz23

+0

ewwwwwwwwwwwwww –

回答

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。

+0

感谢您的回复。我曾尝试过,但因为一些不合理的原因,只要我把它放在那里即可获得chkFilled.Checked在当前上下文中不存在。 – bpaz23

+0

我编辑了我的回复。现在应该给你在该特定行中所需的复选框。让我知道这是否适合你。如果你能找到你的标签并获得它的文本,那么你应该能够找到你的CheckBox并且检查它是否被检查。 =) – G3n1us

+0

我相信这是工作,但....当我提交更新它踢出一个不能转换字符串为int32错误。 – bpaz23