?
Solved

Object reference not set to an instance of an object.

Posted on 2007-08-12
4
Medium Priority
?
789 Views
Last Modified: 2008-01-09
I am trying to populate a dropdown list for my datagrid but keep getting the above error.  I know I must be missing something simple but I am new to this and need fast help.  Below is my code.
protected void slatergrid_OnItemDataBound(object sender, DataGridItemEventArgs e)
        {
            //    Get the data from DB to populate the DropDownlist
            SqlConnection dbConn = null;
            SqlCommand dcmd = new SqlCommand();
            string strConnection = null;
            SqlDataReader drReader = null;
            strConnection = System.Configuration.ConfigurationManager.ConnectionStrings["APPLY"].ToString();
            dbConn = new SqlConnection(strConnection);
            dbConn.Open();
            dcmd.Connection = dbConn;
            string SQL = "Select * from RPC";
            dcmd.Connection = dbConn;
            dcmd.CommandText = SQL;
            drReader = dcmd.ExecuteReader();
            if (drReader.HasRows)
            {
                while (drReader.Read())
                {
                    if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
                    {
                         //    Find the drop down control from datagrid items
                        DropDownList list = (DropDownList)
                        e.Item.FindControl("rpc_select");

                        //    Set the text and value properties for the dropdown list to appropriate columns
                        list.DataTextField = Convert.ToString(drReader["description"]);
                        list.DataValueField = Convert.ToString(drReader["RPC"]);
                        list.DataBind();
                    }
                }
               
            }
             
        }
0
Comment
Question by:mbart
4 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 19679623
on which line does the error occur?
that is essential to debug the problem.
0
 
LVL 22

Accepted Solution

by:
JimBrandley earned 2000 total points
ID: 19679628
You have not said where in the code it fails, but the most likely candidate is locating the list control. Try changing binding to this, then if the list is not found, there will be no exception.
DropDownList list = (DropDownList)e.Item.FindControl("rpc_select");

//    Set the text and value properties for the dropdown list to appropriate columns
if (list != null)
{
   list.DataTextField = Convert.ToString(drReader["description"]);
   list.DataValueField = Convert.ToString(drReader["RPC"]);
   list.DataBind();
}  
0
 
LVL 9

Expert Comment

by:pallosp
ID: 19679630
In which line do you get the error message?

I suspect that the SQL query returns records that have NULL values. You should check if the fields equal to DBNull.Value before convering them to string.

Another tip: check if the
DropDownList list = (DropDownList)e.Item.FindControl("rpc_select");
line finds the dropdownlist or returns null.

Is RPC both a table name and a column name?
0
 

Author Comment

by:mbart
ID: 19679812
Sorry, it was on the description line.  Added check for null and seems to be working.  List is empty but that may be a different issue.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Integration Management Part 2
Loops Section Overview
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question