开发者

updating a column depending on checkbox value

i'm trying to update a column in my database based on wether or not a checkbox is checked in a gridview. Also the update happens only after a button_click. The problem is probably my syntax so if anyone could correct me, that would be much appreciated.

See here my code:

protected void ButtonAfTeHalen_Click(object sender, EventArgs e)
    {
       foreach (GridViewRow r in GridViewOrders.Rows)
       {
          if (((CheckBox)r.Cells[0].FindControl("CheckBoxATH")).Checked == true
                        && (Label)r.Cells[3].FindControl("LabelOrderID") != null)
          {
              string conn2 = "Data Source=pc-...";
              CheckBox checkBoxATH = (CheckBox)GridViewOrders.FindControl("CheckBoxATH");
              Label orderID = (Label)r.Cells[3].FindControl("LabelOrderID");
              LabelTestID.Visible = true;
              LabelTestID.Text = orderID.Text.ToString();

              System.Data.SqlClient.SqlConnection sqlConn10 = new System.Data.SqlClient.SqlConnection(conn2);
              sqlConn10.Open();
              System.Data.SqlClient.SqlCommand updateCommand =
                   new System.Data.SqlClient.SqlCommand("UPDATE tblOrders SET Status= " + checkBoxATH.Checked + " WHERE tOrderId=@orderID", sqlConn10);
                updateCommand.Parameters.AddWithValue("@orderID", LabelTestID.Text);
                upda开发者_如何学JAVAteCommand.ExecuteNonQuery();
          }
       }
    }

The error says: Object reference not set to an instance of an object.

Error @ update statement. But it does show the ID of the order on the label. Only the ID of the lowest checked order in the gridview tho, not all selected IDs.

Regards Mati


Hey, so i've found the solution. Here is my code ( vote up if you like ;) )

        for (int i = 0; i < GridViewOrder.Rows.Count; i++)
        {
            CheckBox ck = (CheckBox)GridViewOrder.Rows[i].Cells[0].FindControl("CheckBoxATH");
            Label orderID = (Label)GridViewOrder.Rows[i].Cells[5].FindControl("LabelOrderID");

            if (ck != null)
            {
                string conn = "Data Source=pc-...";
                System.Data.SqlClient.SqlConnection sqlConn = new System.Data.SqlClient.SqlConnection(conn);
                sqlConn.Open();
                System.Data.SqlClient.SqlCommand updateCommand = new System.Data.SqlClient.SqlCommand("UPDATE tblOrders SET tOrderATH = '" + ck.Checked + "' WHERE tOrderId= '" + orderID.Text + "'", sqlConn);
                updateCommand.Parameters.AddWithValue("@orderID", orderID.Text);
                updateCommand.ExecuteNonQuery();
            }
        }


Sound like a null reference to me. Check all you references used in your update statement and make sure they are not null.

//daniel


I was also facing the same problem. I solved it this way, hope it also solves your prob..

     if ((Boolean)((DataGridViewCheckBoxCell)r.Cells[0].FindControl("CheckBoxATH").FormattedValue)
 && (Label)r.Cells[3].FindControl("LabelOrderID") != null)

Here i have used formattedvalue because the checkbox's state is not changed than it gives a null value.

In datagridview checkbox cell works in a mysterious ways, if the checkbox is checked than sometimes it gives value true or sometimes it gives value checked...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜