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" - 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");
-
-
if (b1.CommandName == "Update")
-
-
{
-
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
5 1271
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 ?
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
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" - 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");
-
- if (b1.CommandName == "Update")
-
-
{
-
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. -
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: -
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();
-
}
-
}
-
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. -
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: -
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();
-
}
-
}
-
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.
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 =)
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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
|
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...
| |