mbart
asked on
Object reference not set to an instance of an object.
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.Confi gurationMa nager.Conn ectionStri ngs["APPLY "].ToStrin g();
dbConn = new SqlConnection(strConnectio n);
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.AlternatingIt em || e.Item.ItemType == ListItemType.Item)
{
// Find the drop down control from datagrid items
DropDownList list = (DropDownList)
e.Item.FindControl("rpc_se lect");
// Set the text and value properties for the dropdown list to appropriate columns
list.DataTextField = Convert.ToString(drReader[ "descripti on"]);
list.DataValueField = Convert.ToString(drReader[ "RPC"]);
list.DataBind();
}
}
}
}
protected void slatergrid_OnItemDataBound
{
// 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.Confi
dbConn = new SqlConnection(strConnectio
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.AlternatingIt
{
// Find the drop down control from datagrid items
DropDownList list = (DropDownList)
e.Item.FindControl("rpc_se
// Set the text and value properties for the dropdown list to appropriate columns
list.DataTextField = Convert.ToString(drReader[
list.DataValueField = Convert.ToString(drReader[
list.DataBind();
}
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.FindC ontrol("rp c_select") ;
line finds the dropdownlist or returns null.
Is RPC both a table name and a column name?
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.FindC
line finds the dropdownlist or returns null.
Is RPC both a table name and a column name?
ASKER
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.
that is essential to debug the problem.