UPDATING database using datagrid : error on UPDATE button click

G

Guest

I am using a datagrid to write data to the database. I am using the
walkthrough by msdn "Using a datagrid to Read & write data to Database". But
when i hit the update button I get the following error: :

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information about
the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set
to an instance of an object.

Source Error:


Line 348:
Line 349: // Updates the dataset table.
Line 350: r.CompanyName = track;
Line 351: r.Region =esn;
Line 352:


Source File: c:\inetpub\wwwroot\demo\boflog\viewrec.aspx.cs Line: 350

Stack Trace:


[NullReferenceException: Object reference not set to an instance of an
object.]
BofLog.ViewRec.DataGrid1_UpdateCommand_1(Object source,
DataGridCommandEventArgs e) in
c:\inetpub\wwwroot\demo\boflog\viewrec.aspx.cs:350

System.Web.UI.WebControls.DataGrid.OnUpdateCommand(DataGridCommandEventArgs
e) +109
System.Web.UI.WebControls.DataGrid.OnBubbleEvent(Object source, EventArgs
e) +507
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +26
System.Web.UI.WebControls.DataGridItem.OnBubbleEvent(Object source,
EventArgs e) +100
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +26
System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +120

System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +115
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +138
System.Web.UI.Page.ProcessRequestMain() +1292


--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:1.1.4322.2032; ASP.NET
Version:1.1.4322.2032


MY CODE for UPDATE button click is::

private void DataGrid1_UpdateCommand_1(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string track, esn;

// Gets the value of the key field of the row being updated
string key = DataGrid1.DataKeys[e.Item.ItemIndex].ToString();

// Gets get the value of the controls (textboxes) that the user
// updated. The DataGrid columns are exposed as the Cells collection.
// Each cell has a collection of controls. In this case, there is only
one
// control in each cell -- a TextBox control. To get its value,
// you copy the TextBox to a local instance (which requires casting)
// and extract its Text property.
//
// The first column -- Cells(0) -- contains the Update and Cancel buttons.
TextBox tb;

// Gets the value the TextBox control in the third column
tb = (TextBox)(e.Item.Cells[1].Controls[0]);
track = tb.Text;

// Gets the value the TextBox control in the fourth column
tb = (TextBox)(e.Item.Cells[3].Controls[0]);
esn = tb.Text;

// Finds the row in the dataset table that matches the
// one the user updated in the grid. This example uses a
// special Find method defined for the typed dataset, which
// returns a reference to the row.
dsCompany.FormFieldsRow r;
r = dsCompany1.FormFields.FindBy_AsiOrder_(key);

// Updates the dataset table.
r.CompanyName = track;
r.Region =esn;

// Calls a SQL statement to update the database from the dataset
sqlDataAdapter1.Update(dsCompany1);

// Takes the DataGrid row out of editing mode
DataGrid1.EditItemIndex = -1;

// Refreshes the grid
DataGrid1.DataBind();
}

I think the problem is bcoz " key" is a string & r maybe is an int. But when
I tried doing
r = dsCompany1.FormFields.FindBy_AsiOrder_(INT.PARSE(key));
it showed error!

Please help.

thanks
 
W

W.G. Ryan eMVP

Betting that it's the r = dsCompany1.formFields.FindBy_AsiOrder_(key) as
suspected. Right before the r.CompanyName = track; - wrap it in a if(r !=
null){
r.companyName = track;
r.Region = esn;
}

That will only fix the null reference exception but not the logic problem.--
you'll need to check the value of key and see what's being found (nothing )
but I'd stick some data in there that I new was there and just step through
it.
--
W.G. Ryan MVP (Windows Embedded)

TiBA Solutions
www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
pmud said:
I am using a datagrid to write data to the database. I am using the
walkthrough by msdn "Using a datagrid to Read & write data to Database". But
when i hit the update button I get the following error: :

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information about
the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set
to an instance of an object.

Source Error:


Line 348:
Line 349: // Updates the dataset table.
Line 350: r.CompanyName = track;
Line 351: r.Region =esn;
Line 352:


Source File: c:\inetpub\wwwroot\demo\boflog\viewrec.aspx.cs Line: 350

Stack Trace:


[NullReferenceException: Object reference not set to an instance of an
object.]
BofLog.ViewRec.DataGrid1_UpdateCommand_1(Object source,
DataGridCommandEventArgs e) in
c:\inetpub\wwwroot\demo\boflog\viewrec.aspx.cs:350

System.Web.UI.WebControls.DataGrid.OnUpdateCommand(DataGridCommandEventArgs
e) +109
System.Web.UI.WebControls.DataGrid.OnBubbleEvent(Object source, EventArgs
e) +507
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +26
System.Web.UI.WebControls.DataGridItem.OnBubbleEvent(Object source,
EventArgs e) +100
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +26
System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +120
System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.Rai
sePostBackEvent(String eventArgument) +115
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +138
System.Web.UI.Page.ProcessRequestMain() +1292


-------------------------------------------------------------------------- ------
Version Information: Microsoft .NET Framework Version:1.1.4322.2032; ASP.NET
Version:1.1.4322.2032


MY CODE for UPDATE button click is::

private void DataGrid1_UpdateCommand_1(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string track, esn;

// Gets the value of the key field of the row being updated
string key = DataGrid1.DataKeys[e.Item.ItemIndex].ToString();

// Gets get the value of the controls (textboxes) that the user
// updated. The DataGrid columns are exposed as the Cells collection.
// Each cell has a collection of controls. In this case, there is only
one
// control in each cell -- a TextBox control. To get its value,
// you copy the TextBox to a local instance (which requires casting)
// and extract its Text property.
//
// The first column -- Cells(0) -- contains the Update and Cancel buttons.
TextBox tb;

// Gets the value the TextBox control in the third column
tb = (TextBox)(e.Item.Cells[1].Controls[0]);
track = tb.Text;

// Gets the value the TextBox control in the fourth column
tb = (TextBox)(e.Item.Cells[3].Controls[0]);
esn = tb.Text;

// Finds the row in the dataset table that matches the
// one the user updated in the grid. This example uses a
// special Find method defined for the typed dataset, which
// returns a reference to the row.
dsCompany.FormFieldsRow r;
r = dsCompany1.FormFields.FindBy_AsiOrder_(key);

// Updates the dataset table.
r.CompanyName = track;
r.Region =esn;

// Calls a SQL statement to update the database from the dataset
sqlDataAdapter1.Update(dsCompany1);

// Takes the DataGrid row out of editing mode
DataGrid1.EditItemIndex = -1;

// Refreshes the grid
DataGrid1.DataBind();
}

I think the problem is bcoz " key" is a string & r maybe is an int. But when
I tried doing
r = dsCompany1.FormFields.FindBy_AsiOrder_(INT.PARSE(key));
it showed error!

Please help.

thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top