DropDownList is giving following error : System.NullReferenceException
-
I am trying to edit values in a row of a GridView, Gender DropDownList which is filled with values "Male" and "Female" is giving 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 65: DropDownList cmbType = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("cmbType");
Line 66:
Line 67: customer.Update(Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0].ToString()), txtName.Text, cmbGender.SelectedValue, txtCity.Text, cmbType.SelectedValue);
Line 68: GridView1.EditIndex = -1;
Line 69: FillCustomerInGrid();Source File: d:\ASP.NET Examples\LoginExp\CustomerData.aspx.cs Line: 67 Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
CustomerData.GridView1_RowUpdating(Object sender, GridViewUpdateEventArgs e) in d:\ASP.NET Examples\LoginExp\CustomerData.aspx.cs:67
System.Web.UI.WebControls.GridView.OnRowUpdating(GridViewUpdateEventArgs e) +131
System.Web.UI.WebControls.GridView.HandleUpdate(GridViewRow row, Int32 rowIndex, Boolean causesValidation) +908
System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +809
System.Web.UI.WebControls.GridView.OnBubbleEvent(Object source, EventArgs e) +163
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +56
System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(Object source, EventArgs e) +119
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +56
System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +106
System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +175
System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +31
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +32
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +242
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3825Please he
-
I am trying to edit values in a row of a GridView, Gender DropDownList which is filled with values "Male" and "Female" is giving 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 65: DropDownList cmbType = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("cmbType");
Line 66:
Line 67: customer.Update(Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0].ToString()), txtName.Text, cmbGender.SelectedValue, txtCity.Text, cmbType.SelectedValue);
Line 68: GridView1.EditIndex = -1;
Line 69: FillCustomerInGrid();Source File: d:\ASP.NET Examples\LoginExp\CustomerData.aspx.cs Line: 67 Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
CustomerData.GridView1_RowUpdating(Object sender, GridViewUpdateEventArgs e) in d:\ASP.NET Examples\LoginExp\CustomerData.aspx.cs:67
System.Web.UI.WebControls.GridView.OnRowUpdating(GridViewUpdateEventArgs e) +131
System.Web.UI.WebControls.GridView.HandleUpdate(GridViewRow row, Int32 rowIndex, Boolean causesValidation) +908
System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +809
System.Web.UI.WebControls.GridView.OnBubbleEvent(Object source, EventArgs e) +163
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +56
System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(Object source, EventArgs e) +119
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +56
System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +106
System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +175
System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +31
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +32
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +242
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3825Please he
As explained here http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.datakeys.aspx[^] When the DataKeyNames property is set, the GridView control automatically creates a DataKey object for each row in the control. The DataKey object contains the values of the field or fields specified in the DataKeyNames property. The DataKey objects are then added to the control's DataKeys collection. Use the DataKeys property to retrieve the DataKey object for a specific data row in the GridView control. The reason for
null reference exception
in the above code could be due to not setting theDataKeyNames
property but accessing theDataKeys[e.RowIndex]
value atcustomer.Update(Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0].ToString())
Try setting the
DataKeyNames
as shown in above reference likedatakeynames="CustomerID"
-
I am trying to edit values in a row of a GridView, Gender DropDownList which is filled with values "Male" and "Female" is giving 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 65: DropDownList cmbType = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("cmbType");
Line 66:
Line 67: customer.Update(Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0].ToString()), txtName.Text, cmbGender.SelectedValue, txtCity.Text, cmbType.SelectedValue);
Line 68: GridView1.EditIndex = -1;
Line 69: FillCustomerInGrid();Source File: d:\ASP.NET Examples\LoginExp\CustomerData.aspx.cs Line: 67 Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
CustomerData.GridView1_RowUpdating(Object sender, GridViewUpdateEventArgs e) in d:\ASP.NET Examples\LoginExp\CustomerData.aspx.cs:67
System.Web.UI.WebControls.GridView.OnRowUpdating(GridViewUpdateEventArgs e) +131
System.Web.UI.WebControls.GridView.HandleUpdate(GridViewRow row, Int32 rowIndex, Boolean causesValidation) +908
System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +809
System.Web.UI.WebControls.GridView.OnBubbleEvent(Object source, EventArgs e) +163
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +56
System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(Object source, EventArgs e) +119
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +56
System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +106
System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +175
System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +31
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +32
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +242
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3825Please he
you can't use .ToString() method and set it to a null value,
GridView1.DataKeys[e.RowIndex].Values[0]
is passing a null value! set DataKeys for your gridview!