Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. DropDownList is giving following error : System.NullReferenceException

DropDownList is giving following error : System.NullReferenceException

Scheduled Pinned Locked Moved ASP.NET
csharpasp-netdesigndata-structuresdebugging
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    M S Varma
    wrote on last edited by
    #1

    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) +3825

    Please he

    V T 2 Replies Last reply
    0
    • M M S Varma

      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) +3825

      Please he

      V Offline
      V Offline
      VJ Reddy
      wrote on last edited by
      #2

      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 the DataKeyNames property but accessing the DataKeys[e.RowIndex] value at

      customer.Update(Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0].ToString())

      Try setting the DataKeyNames as shown in above reference like

      datakeynames="CustomerID"

      1 Reply Last reply
      0
      • M M S Varma

        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) +3825

        Please he

        T Offline
        T Offline
        taha bahraminezhad Jooneghani
        wrote on last edited by
        #3

        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!

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups