Solved

Null Value in Control While trying to populate drop down list

Posted on 2008-10-17
8
669 Views
Last Modified: 2011-04-14
I am trying to dynamically populate the controls on a web page.  Regardless of whether there are 1 dropdpwnlist or several, I would like to call the same method to populate.  The problem I am having is trying to get the dropdown to work dynamically by carrying the name.  This way I do not have to write code to populate every field.   The dropdown casting does not give me an error.  It is when I try to assign the data source that it gives me a runtime error that the "Object reference not set to an instance of an object."  What I am doing wrong or am I doing it completely wrong?  

Thanks,
Doug
populateDropDownList(controlID, sql);
 

protected void populateDropDownList(String controlID, String sql)

    {

        DBAccessUsingDB2 db2Access = new DBAccessUsingDB2();

        CustomUtilities cu = new CustomUtilities();

        DataSet ds = new DataSet();

        DropDownList ddlId = (DropDownList)cu.FindControl(controlID);

        ds = db2Access.FindRecords(ds, sql);

        if (ds.Tables[0].Rows.Count > 0)

        {

            DDLID.DataSource = ds.Tables[0];

            DDLID.DataTextField = ds.Tables[0].Columns[1].ColumnName.ToString();

            agencyIDDDL.DataValueField = ds.Tables[0].Columns[0].ColumnName.ToString();

            agencyIDDDL.DataBind();

        }

Open in new window

0
Comment
Question by:anewlevel4u
  • 4
8 Comments
 
LVL 18

Accepted Solution

by:
Priest04 earned 500 total points
ID: 22745441
Looking at these two lines

DropDownList ddlId = (DropDownList)cu.FindControl(controlID);
DDLID.DataSource = ds.Tables[0];

these two variables have different casing, are therefore are not pointing to the same object. Should it be
protected void populateDropDownList(String controlID, String sql)

    {

        DBAccessUsingDB2 db2Access = new DBAccessUsingDB2();

        CustomUtilities cu = new CustomUtilities();

        DataSet ds = new DataSet();

        DropDownList ddlId = (DropDownList)cu.FindControl(controlID);

        ds = db2Access.FindRecords(ds, sql);

        if (ds.Tables[0].Rows.Count > 0)

        {

            ddlId.DataSource = ds.Tables[0];

            ddlId.DataTextField = ds.Tables[0].Columns[1].ColumnName.ToString();

            agencyIDDDL.DataValueField = ds.Tables[0].Columns[0].ColumnName.ToString();

            agencyIDDDL.DataBind();

        }

Open in new window

0
 

Author Comment

by:anewlevel4u
ID: 22766737
I figured the solution.  The casing was not the problem.  I simply needed to send the control type as a parameter in the method call.  See below the new code.
populateDropDownList(agencyNameDDL, sql);
 

//simply send the control type through the parameter in the method

protected void populateDropDownList(DropDownList ddlId, String sql)

{

     DBAccessUsingDB2 db2Access = new DBAccessUsingDB2();

     DataSet ds = new DataSet();

     ds = db2Access.FindRecords(ds, sql);

     if (ds.Tables[0].Rows.Count > 0)

     {

         //The value send to the method can be called directly.

         //This could be updated for whatever type you need.

         ddlId.DataSource = ds.Tables[0];

         ddlId.DataTextField = ds.Tables[0].Columns["NAMEFEIN"].ColumnName.ToString();

         ddlId.DataValueField = ds.Tables[0].Columns[0].ColumnName.ToString();

         ddlId.DataBind();

     }        

}

Open in new window

0
 
LVL 18

Expert Comment

by:Priest04
ID: 22768445
It shouldnt make any difference, both methods should work. As you said it above, it throws an error on this line.

DDLID.DataSource = ds.Tables[0];

"Object reference not set to an instance of an object."

this means that DDLID is not instantiated, or is equal to NULL. I dont see where have you declared this variable, and in your latter example you use ddlId, which is of different casing. So I dont understand how can you say its not the casing problem? :)
0
 
LVL 18

Expert Comment

by:Priest04
ID: 22815437
I find that anewlevel4u's statement that the resolution was to send the control reference is not correct. It doesn't make any difference if control ID is passed to a FindControl method, or a control reference is passed, both will have the same result - reference to a ListBox control.

This is the line that caused the exception:

DDLID.DataSource = ds.Tables[0];

"Object reference not set to an instance of an object."

This without doubt indicates that DDLID is not pointing to an object on the heap.  There can be several reasons for this:

1) DropDownList ddlId = (DropDownList)cu.FindControl(controlID);

As it can be seen, ddlID and DDLID are not the same. Could be that DDLID was declared somewhere outside the procedure, so there was a mistake in casing.

2) a wrong Control ID is passed to a method, resulting in ddlId to have a NULL reference.

In either case, this has nothing to do with passing the control reference as a parameter. And ss it can be seen in anewlevel4u's solution, casing WAS corrected.

I disagree with people denying the correct answer, just to get refunded points. anewlevel4u did not even replied to my remarks, could be because he (she) didn't have an argument to pass.
0
 
LVL 18

Expert Comment

by:Priest04
ID: 22842115
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Windows Service to Receive TCP Packets 4 121
How to use xmlReader and idatareader  ? 4 55
Why Does This Page Not Show Up? 4 22
Exit the loop 4 34
Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
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.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

910 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now