error while updating grid

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cmrhema
    Contributor
    • Jan 2007
    • 375

    #1

    error while updating grid

    hello,

    I have used a GridView to edit and update a row.
    I have produced the code below
    However i get an error as below
    I have underlined the place where i get the error

    "NullRefere nce Exception was handled near the code"

    Code:
     protected void GridView2_RowUpdating(object sender,GridViewUpdateEventArgs e)
        {
            String S1;
            SqlConnection MyCon = new SqlConnection("Server=HEMA\\SQLEXPRESS;Integrated Security=SSPI;database=demo");
            MyCon.Open();
    
            ImageButton b1 = new ImageButton();
            TextBox t1 = new TextBox();
            TextBox t2 = new TextBox();
            foreach (GridViewRow gv in GridView2.Rows)
            {
                b1 = (ImageButton)gv.FindControl("imagebutton1");
                t1 = (TextBox)gv.FindControl("TextBox1");
                t2 = (TextBox)gv.FindControl("TextBox2");
          
             [U]   if (b1.CommandName == "Update")[/U]
                    
                {
                    S1 = "update t1 set name='" + t2.Text + "' where id=" + t1.Text + "";
                    SqlCommand UpdateCmd = new SqlCommand(S1, MyCon);
                    Response.Write("updated");
                    UpdateCmd.ExecuteNonQuery();
                }
    
            }
    
            MyCon.Close();
          
            
        }
    What's the error and how to rectify it
    thanks
  • radcaesar
    Recognized Expert Contributor
    • Sep 2006
    • 759

    #2
    Put a break point and Trace (Using F11), b1 should be null in ur code.

    Where u have insert and Bind the Image button in ur grid. Is there any image button inside that ?

    Comment

    • cmrhema
      Contributor
      • Jan 2007
      • 375

      #3
      Originally posted by radcaesar
      Put a break point and Trace (Using F11), b1 should be null in ur code.

      Where u have insert and Bind the Image button in ur grid. Is there any image button inside that ?
      yes radcaesar, i have indeed placed imagebutton instead of link or normal button. i inserted using the porperties. there i gave the corresponding name of the bmp.
      As far as tracing is concerned i did trace it but i have already declared b1 as the image button1 in that case how am i supposed to get the error.

      thanks for your reply

      Comment

      • Floydan
        New Member
        • Aug 2007
        • 24

        #4
        Originally posted by cmrhema
        hello,

        I have used a GridView to edit and update a row.
        I have produced the code below
        However i get an error as below
        I have underlined the place where i get the error

        "NullRefere nce Exception was handled near the code"

        Code:
         protected void GridView2_RowUpdating(object sender,GridViewUpdateEventArgs e)
        {
        String S1;
        SqlConnection MyCon = new SqlConnection("Server=HEMA\\SQLEXPRESS;Integrated Security=SSPI;database=demo");
        MyCon.Open();
         
        ImageButton b1 = new ImageButton();
        TextBox t1 = new TextBox();
        TextBox t2 = new TextBox();
        foreach (GridViewRow gv in GridView2.Rows)
        {
        b1 = (ImageButton)gv.FindControl("imagebutton1");
        t1 = (TextBox)gv.FindControl("TextBox1");
        t2 = (TextBox)gv.FindControl("TextBox2");
         
        [u]if (b1.CommandName == "Update")[/u]
         
        {
        S1 = "update t1 set name='" + t2.Text + "' where id=" + t1.Text + "";
        SqlCommand UpdateCmd = new SqlCommand(S1, MyCon);
        Response.Write("updated");
        UpdateCmd.ExecuteNonQuery();
        }
         
        }
         
        MyCon.Close();
         
         
        }
        What's the error and how to rectify it
        thanks
        Hi,
        Are you updating it one row at a time or several at a time?

        If you are updating it several at a time then this might help. my only concern, since i havent tested this code myself is that .NET control ID's have to be unique, so each row will have diffrent id's for the controls.

        Code:
        protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e) {
        string query = "update t1 set name='@Name' where id=@Id;";
        string connStr = "Server=HEMA\\SQLEXPRESS;Integrated Security=SSPI;database=demo";
        using (SqlConnection conn = new SqlConnection(connStr)) {
        SqlCommand cmd = new SqlCommand(query, conn);
        cmd.Connection.Open();
        cmd.Parameters.Add("@Name", SqlDbType.NVarChar, 255);
        cmd.Parameters.Add("@Id", SqlDbType.Int);
        foreach (GridViewRow row in GridView2.Rows) {
        if (row.RowState == DataControlRowState.Edit) {
        	TextBox nameBox = row.FindControl("TextBox1");
        	TextBox idBox = row.FindControl("TextBox2");
         
        	int id = -1;
        	Int32.TryParse(id.Text, out id);
        	if(id > 0) {
        	 cmd.Parameters["@Name"].Value = nameBox.Text.Replace("'", "''");
        	 cmd.Parameters["@Id"].Value = id;
         
        	 cmd.ExecuteNonQuery();
        	}
        }
        }
        cmd.Connection.Close();
        cmd.Dispose();
        }
        }
        If you are just updating it 1 row at a time then this might help:

        Code:
        protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e) {
         string query = "update t1 set [email="name='@Name'"]name='@Name'[/email] where [email="id=@Id"]id=@Id[/email];";
         string connStr = "Server=HEMA\\SQLEXPRESS;Integrated Security=SSPI;database=demo";
         using (SqlConnection conn = new SqlConnection(connStr)) {
          SqlCommand cmd = new SqlCommand(query, conn);
          cmd.Connection.Open();
          cmd.Parameters.Add("@Name", SqlDbType.NVarChar, 255);
          cmd.Parameters.Add("@Id", SqlDbType.Int);
          foreach (GridViewRow row in GridView2.Rows) {
           if (row.RowState == DataControlRowState.Edit) {
        	TextBox nameBox = row.FindControl("TextBox1");
        	TextBox idBox = row.FindControl("TextBox2");
         
        	int id = -1;
        	Int32.TryParse(id.Text, out id);
        	if(id > 0) {
        	 cmd.Parameters["@Name"].Value = nameBox.Text.Replace("'", "''");
        	 cmd.Parameters["@Id"].Value = id;
         
        	 cmd.ExecuteNonQuery();
        	}
           }
          }
          cmd.Connection.Close();
          cmd.Dispose();
         }
        }

        Comment

        • cmrhema
          Contributor
          • Jan 2007
          • 375

          #5
          Originally posted by Floydan
          Hi,
          Are you updating it one row at a time or several at a time?

          If you are updating it several at a time then this might help. my only concern, since i havent tested this code myself is that .NET control ID's have to be unique, so each row will have diffrent id's for the controls.

          Code:
          protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e) {
          string query = "update t1 set name='@Name' where id=@Id;";
          string connStr = "Server=HEMA\\SQLEXPRESS;Integrated Security=SSPI;database=demo";
          using (SqlConnection conn = new SqlConnection(connStr)) {
          SqlCommand cmd = new SqlCommand(query, conn);
          cmd.Connection.Open();
          cmd.Parameters.Add("@Name", SqlDbType.NVarChar, 255);
          cmd.Parameters.Add("@Id", SqlDbType.Int);
          foreach (GridViewRow row in GridView2.Rows) {
          if (row.RowState == DataControlRowState.Edit) {
          	TextBox nameBox = row.FindControl("TextBox1");
          	TextBox idBox = row.FindControl("TextBox2");
           
          	int id = -1;
          	Int32.TryParse(id.Text, out id);
          	if(id > 0) {
          	 cmd.Parameters["@Name"].Value = nameBox.Text.Replace("'", "''");
          	 cmd.Parameters["@Id"].Value = id;
           
          	 cmd.ExecuteNonQuery();
          	}
          }
          }
          cmd.Connection.Close();
          cmd.Dispose();
          }
          }
          If you are just updating it 1 row at a time then this might help:

          Code:
          protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e) {
           string query = "update t1 set [email="name='@Name'"]name='@Name'[/email] where [email="id=@Id"]id=@Id[/email];";
           string connStr = "Server=HEMA\\SQLEXPRESS;Integrated Security=SSPI;database=demo";
           using (SqlConnection conn = new SqlConnection(connStr)) {
            SqlCommand cmd = new SqlCommand(query, conn);
            cmd.Connection.Open();
            cmd.Parameters.Add("@Name", SqlDbType.NVarChar, 255);
            cmd.Parameters.Add("@Id", SqlDbType.Int);
            foreach (GridViewRow row in GridView2.Rows) {
             if (row.RowState == DataControlRowState.Edit) {
          	TextBox nameBox = row.FindControl("TextBox1");
          	TextBox idBox = row.FindControl("TextBox2");
           
          	int id = -1;
          	Int32.TryParse(id.Text, out id);
          	if(id > 0) {
          	 cmd.Parameters["@Name"].Value = nameBox.Text.Replace("'", "''");
          	 cmd.Parameters["@Id"].Value = id;
           
          	 cmd.ExecuteNonQuery();
          	}
             }
            }
            cmd.Connection.Close();
            cmd.Dispose();
           }
          }
          Thanks floydan for replying that to in such a precise manner.
          I may update a single row or many rows at a time.
          I tried your code
          Unfortunately i cannot use
          int id = -1;
          Int32.TryParse( id.Text, out id);

          because i am using an image button for edit update which does not have a text property. i tried to change to tostring but to no success.

          now to the most funniest part because i missed the above lines what happens is only the alternate record gets updated.
          i am now working on it

          Anyway thank you very much indeed.

          Comment

          • Floydan
            New Member
            • Aug 2007
            • 24

            #6
            Well as I said, it was just something i wrote from the top of my head without any real testing =)

            Since i have no idea how your gridview looks alot of it are based on pure quessing =)

            Comment

            Working...