Click here to Skip to main content
15,894,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am getting the following error when i am trying to fill a Dropdown inside the GridView:

Object reference not set to an instance of an object.

the code is
C#
protected void ProjectOnFormGrid_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                string selectcommand;
                DataSet dsrow;
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    selectcommand = "select QuantityDescId,QuantityDescName from QuantityDescriptions";
                    DropDownList EditddQuantDesc = (DropDownList)e.Row.FindControl("EditddQuantDesc");
                    if (EditddQuantDesc == null)
                    {
                        da = new SqlDataAdapter(selectcommand, con);
                        dsrow = new DataSet();
                       da.Fill(dsrow, "QuantityDescriptions");
                       EditddQuantDesc.DataSource = dsrow.Tables["QuantityDescriptions"];
                       EditddQuantDesc.DataValueField = dsrow.Tables[0].Columns[0].ToString();
                       EditddQuantDesc.DataTextField = dsrow.Tables[0].Columns[1].ToString();
                        
                    }
                }
                if (e.Row.RowType == DataControlRowType.Footer)
                {
                    selectcommand = "select QuantityDescId,QuantityDescName from QuantityDescriptions";
                    DropDownList FooterddQuantDesc = (DropDownList)e.Row.FindControl("FooterddQuantDesc");
                    if (FooterddQuantDesc != null)
                    {
                        da = new SqlDataAdapter(selectcommand, con);
                        dsrow = new DataSet();
                        da.Fill(dsrow, "QuantityDescriptions");
                        FooterddQuantDesc.DataSource = dsrow;
                        FooterddQuantDesc.DataValueField = dsrow.Tables[0].Columns[0].ToString();
                        FooterddQuantDesc.DataTextField = dsrow.Tables[0].Columns[1].ToString();
                    }
                }
            }
            catch (Exception ce)
            {
                lblError.Text = ce.Message;
                lblError.Visible = true;
            }
        }
I am getting error at this line:
EditddQuantDesc.DataSource = dsrow.Tables["QuantityDescriptions"];
Posted
Updated 8-Nov-10 0:27am
v3

1 solution

C#
if (EditddQuantDesc == null)
{
      da = new SqlDataAdapter(selectcommand, con);
      dsrow = new DataSet();
   da.Fill(dsrow, "QuantityDescriptions");
   EditddQuantDesc.DataSource = dsrow.Tables["QuantityDescriptions"];
   EditddQuantDesc.DataValueField = dsrow.Tables[0].Columns[0].ToString();
   EditddQuantDesc.DataTextField = dsrow.Tables[0].Columns[1].ToString();
}


You check if EditddQuantDesc null and work with it anyway and as the error let's you know, it isn't set to a reference of an object.

Good luck!
 
Share this answer
 
Comments
Vigneshb6 8-Nov-10 6:19am    
I got it
Actually it should be if(EditddQuantDesc!=null)
Vigneshb6 8-Nov-10 6:20am    
Thanks Nijboer for ur answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900