473,327 Members | 2,055 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

error while updating grid

375 256MB
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

"NullReference Exception was handled near the code"

Expand|Select|Wrap|Line Numbers
  1.  protected void GridView2_RowUpdating(object sender,GridViewUpdateEventArgs e)
  2.     {
  3.         String S1;
  4.         SqlConnection MyCon = new SqlConnection("Server=HEMA\\SQLEXPRESS;Integrated Security=SSPI;database=demo");
  5.         MyCon.Open();
  6.  
  7.         ImageButton b1 = new ImageButton();
  8.         TextBox t1 = new TextBox();
  9.         TextBox t2 = new TextBox();
  10.         foreach (GridViewRow gv in GridView2.Rows)
  11.         {
  12.             b1 = (ImageButton)gv.FindControl("imagebutton1");
  13.             t1 = (TextBox)gv.FindControl("TextBox1");
  14.             t2 = (TextBox)gv.FindControl("TextBox2");
  15.  
  16.             if (b1.CommandName == "Update")
  17.  
  18.             {
  19.                 S1 = "update t1 set name='" + t2.Text + "' where id=" + t1.Text + "";
  20.                 SqlCommand UpdateCmd = new SqlCommand(S1, MyCon);
  21.                 Response.Write("updated");
  22.                 UpdateCmd.ExecuteNonQuery();
  23.             }
  24.  
  25.         }
  26.  
  27.         MyCon.Close();
  28.  
  29.  
  30.     }
What's the error and how to rectify it
thanks
Aug 3 '07 #1
5 1271
radcaesar
759 Expert 512MB
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 ?
Aug 3 '07 #2
cmrhema
375 256MB
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
Aug 3 '07 #3
Floydan
24
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

"NullReference Exception was handled near the code"

Expand|Select|Wrap|Line Numbers
  1.  protected void GridView2_RowUpdating(object sender,GridViewUpdateEventArgs e)
  2. {
  3. String S1;
  4. SqlConnection MyCon = new SqlConnection("Server=HEMA\\SQLEXPRESS;Integrated Security=SSPI;database=demo");
  5. MyCon.Open();
  6.  
  7. ImageButton b1 = new ImageButton();
  8. TextBox t1 = new TextBox();
  9. TextBox t2 = new TextBox();
  10. foreach (GridViewRow gv in GridView2.Rows)
  11. {
  12. b1 = (ImageButton)gv.FindControl("imagebutton1");
  13. t1 = (TextBox)gv.FindControl("TextBox1");
  14. t2 = (TextBox)gv.FindControl("TextBox2");
  15.  
  16. if (b1.CommandName == "Update")
  17.  
  18. {
  19. S1 = "update t1 set name='" + t2.Text + "' where id=" + t1.Text + "";
  20. SqlCommand UpdateCmd = new SqlCommand(S1, MyCon);
  21. Response.Write("updated");
  22. UpdateCmd.ExecuteNonQuery();
  23. }
  24.  
  25. }
  26.  
  27. MyCon.Close();
  28.  
  29.  
  30. }
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.

Expand|Select|Wrap|Line Numbers
  1. protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e) {
  2. string query = "update t1 set name='@Name' where id=@Id;";
  3. string connStr = "Server=HEMA\\SQLEXPRESS;Integrated Security=SSPI;database=demo";
  4. using (SqlConnection conn = new SqlConnection(connStr)) {
  5. SqlCommand cmd = new SqlCommand(query, conn);
  6. cmd.Connection.Open();
  7. cmd.Parameters.Add("@Name", SqlDbType.NVarChar, 255);
  8. cmd.Parameters.Add("@Id", SqlDbType.Int);
  9. foreach (GridViewRow row in GridView2.Rows) {
  10. if (row.RowState == DataControlRowState.Edit) {
  11.     TextBox nameBox = row.FindControl("TextBox1");
  12.     TextBox idBox = row.FindControl("TextBox2");
  13.  
  14.     int id = -1;
  15.     Int32.TryParse(id.Text, out id);
  16.     if(id > 0) {
  17.      cmd.Parameters["@Name"].Value = nameBox.Text.Replace("'", "''");
  18.      cmd.Parameters["@Id"].Value = id;
  19.  
  20.      cmd.ExecuteNonQuery();
  21.     }
  22. }
  23. }
  24. cmd.Connection.Close();
  25. cmd.Dispose();
  26. }
  27. }
  28.  
If you are just updating it 1 row at a time then this might help:

Expand|Select|Wrap|Line Numbers
  1. protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e) {
  2.  string query = "update t1 set name='@Name' where id=@Id;";
  3.  string connStr = "Server=HEMA\\SQLEXPRESS;Integrated Security=SSPI;database=demo";
  4.  using (SqlConnection conn = new SqlConnection(connStr)) {
  5.   SqlCommand cmd = new SqlCommand(query, conn);
  6.   cmd.Connection.Open();
  7.   cmd.Parameters.Add("@Name", SqlDbType.NVarChar, 255);
  8.   cmd.Parameters.Add("@Id", SqlDbType.Int);
  9.   foreach (GridViewRow row in GridView2.Rows) {
  10.    if (row.RowState == DataControlRowState.Edit) {
  11.     TextBox nameBox = row.FindControl("TextBox1");
  12.     TextBox idBox = row.FindControl("TextBox2");
  13.  
  14.     int id = -1;
  15.     Int32.TryParse(id.Text, out id);
  16.     if(id > 0) {
  17.      cmd.Parameters["@Name"].Value = nameBox.Text.Replace("'", "''");
  18.      cmd.Parameters["@Id"].Value = id;
  19.  
  20.      cmd.ExecuteNonQuery();
  21.     }
  22.    }
  23.   }
  24.   cmd.Connection.Close();
  25.   cmd.Dispose();
  26.  }
  27. }
  28.  
Aug 3 '07 #4
cmrhema
375 256MB
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.

Expand|Select|Wrap|Line Numbers
  1. protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e) {
  2. string query = "update t1 set name='@Name' where id=@Id;";
  3. string connStr = "Server=HEMA\\SQLEXPRESS;Integrated Security=SSPI;database=demo";
  4. using (SqlConnection conn = new SqlConnection(connStr)) {
  5. SqlCommand cmd = new SqlCommand(query, conn);
  6. cmd.Connection.Open();
  7. cmd.Parameters.Add("@Name", SqlDbType.NVarChar, 255);
  8. cmd.Parameters.Add("@Id", SqlDbType.Int);
  9. foreach (GridViewRow row in GridView2.Rows) {
  10. if (row.RowState == DataControlRowState.Edit) {
  11.     TextBox nameBox = row.FindControl("TextBox1");
  12.     TextBox idBox = row.FindControl("TextBox2");
  13.  
  14.     int id = -1;
  15.     Int32.TryParse(id.Text, out id);
  16.     if(id > 0) {
  17.      cmd.Parameters["@Name"].Value = nameBox.Text.Replace("'", "''");
  18.      cmd.Parameters["@Id"].Value = id;
  19.  
  20.      cmd.ExecuteNonQuery();
  21.     }
  22. }
  23. }
  24. cmd.Connection.Close();
  25. cmd.Dispose();
  26. }
  27. }
  28.  
If you are just updating it 1 row at a time then this might help:

Expand|Select|Wrap|Line Numbers
  1. protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e) {
  2.  string query = "update t1 set name='@Name' where id=@Id;";
  3.  string connStr = "Server=HEMA\\SQLEXPRESS;Integrated Security=SSPI;database=demo";
  4.  using (SqlConnection conn = new SqlConnection(connStr)) {
  5.   SqlCommand cmd = new SqlCommand(query, conn);
  6.   cmd.Connection.Open();
  7.   cmd.Parameters.Add("@Name", SqlDbType.NVarChar, 255);
  8.   cmd.Parameters.Add("@Id", SqlDbType.Int);
  9.   foreach (GridViewRow row in GridView2.Rows) {
  10.    if (row.RowState == DataControlRowState.Edit) {
  11.     TextBox nameBox = row.FindControl("TextBox1");
  12.     TextBox idBox = row.FindControl("TextBox2");
  13.  
  14.     int id = -1;
  15.     Int32.TryParse(id.Text, out id);
  16.     if(id > 0) {
  17.      cmd.Parameters["@Name"].Value = nameBox.Text.Replace("'", "''");
  18.      cmd.Parameters["@Id"].Value = id;
  19.  
  20.      cmd.ExecuteNonQuery();
  21.     }
  22.    }
  23.   }
  24.   cmd.Connection.Close();
  25.   cmd.Dispose();
  26.  }
  27. }
  28.  
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.
Aug 3 '07 #5
Floydan
24
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 =)
Aug 3 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Tim Bird | last post by:
I am writing SQL enterprise type application in c# and allowing ht user to execute queries, etc. The results are displayed in a grid using data adapteres, etc. Do I really have to write routines to...
2
by: Geir Sanne | last post by:
hi!! how am i supposed to update the db witht he changes the user make in webpage am trying to make a simple page that have a grid bound to a table the user can delete,add or update the grid...
3
by: Jon Agiato | last post by:
Hi, I am trying to use a data grid in a web application in which I have three tiers. The DataGrid is not set up to a data source, or a data adapter, so everytime I make a change I send the cell...
1
by: Geraldine Hobley | last post by:
hello, I have put thisup b4, but I think that people have misunderstood the problem, so I will make it clearer. I have a problem updating a dataset that is bound to a datagrid. The code that I...
14
by: Lars Netzel | last post by:
A little background: I use three Datagrids that are in a child parent relation. I Use Negative Autoincrement on the the DataTables and that's workning nice. My problem is when I Update these...
0
by: Michael Kellogg | last post by:
I have a problem wherein a query that updates a GridView doesn't seem to really stay in sync with a label I have above the GridView. I have a GridView object that I'm updating with information...
1
by: batista | last post by:
Hello all, I have a third praty grid control...named C1grid. Im using it in one of my apps.. Now, I have bind this grid to a custom dataset class named "DataViewEx". The code of the class is...
0
by: OldStd | last post by:
Updating data using 2 data sets I am having some problems in updating the database using two datasets as suggested by someone. 1. Data is displayed in a data grid from a dataset generated using...
10
by: Mike | last post by:
I have code that is doing some updating to a record. Its getting the ID to update from the Grid. I'm passing an INT to my method to update the record. My code is working though I'm still getting an...
2
by: =?Utf-8?B?VmFuZXNzYQ==?= | last post by:
Hi All! I am with a situation where I am not getting the right updating to the form's fields. The situation is the following one: I have one combobox and one textbox. I am using the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.