?
Solved

ASP.NET - passing a DropDownList as a parameter

Posted on 2005-03-30
11
Medium Priority
?
347 Views
Last Modified: 2010-04-07
Hello there.

I have a method that takes a DropDownList as a parameter, connects to an SQL DB and fills the DropDownList with some data. I'm getting the following error.

Source Error:
Line 29:                BindCombo.BindToCombo(fnamecmb);

Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
   MyTestProjectFiles.Main.Page_Load(Object sender, EventArgs e) in c:\myprojects\mytestproject\mytestprojectfiles\main.aspx.cs:29
   System.Web.UI.Control.OnLoad(EventArgs e)
   System.Web.UI.Control.LoadRecursive()
   System.Web.UI.Page.ProcessRequestMain()

Here is the Sub I'm trying to call: (inside VBControls.DBConnect class)

 Public Sub BindToCombo(ByVal myObj As System.Web.UI.WebControls.DropDownList)

        Dim ds As New DataSet
        Dim adapt As New SqlDataAdapter
        Dim strConn As String = "server=localhost;" + "database=pubs;Trusted_Connection=Yes"
        Dim sqlConn As New SqlConnection(strConn)

        adapt.SelectCommand = New SqlCommand("SELECT fname FROM [employee]", sqlConn)
        adapt.Fill(ds, "employee")
        myObj.DataSource = ds
        myObj.DataBind()

        sqlConn.Close()
    End Sub

And here is how I try to call it:

include...
namespace MyTestProjectFiles
{
     public class Main : System.Web.UI.Page
     {
          protected System.Web.UI.WebControls.DropDownList fnamecmb;

          public VBControls.DBConnect BindCombo;

          private void Page_Load(object sender, System.EventArgs e)
          {    
               BindCombo.BindToCombo(fnamecmb);
          }
...

Whatever I do I get the 'Object reference not set to an instance of an object' error.

Any ideas?

Many thanks.
 
0
Comment
Question by:chrispkotsiopoulos
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
11 Comments
 
LVL 20

Expert Comment

by:ihenry
ID: 13669499
I'm not very sure at which line the exception is actually gets thrown. It always good to have a check in the function since you never expect any null object happens to be passed to the function. And better yet, put your code under try and catch block, so when any exception occur it will be handled gracefully and of course you can get better idea of what happen. After you follow the following code, post here the exception stack trace.

Public Sub BindToCombo(ByVal myObj As System.Web.UI.WebControls.DropDownList)

    If myObj Is Nothing Then
       Throw new ArgumentNullException("myObj")
    End If

    Dim ds As New DataSet
    Dim adapt As New SqlDataAdapter
    Dim strConn As String = "server=localhost;" + "database=pubs;Trusted_Connection=Yes"
    Dim sqlConn As New SqlConnection(strConn)

    adapt.SelectCommand = New SqlCommand("SELECT fname FROM [employee]", sqlConn)
    Try
        adapt.Fill(ds, "employee")
        myObj.DataSource = ds
        myObj.DataBind()

        sqlConn.Close()
    Catch
        throw
    Finally
        'do some clean up here...
        'well, you can do it later..
    End Try
End Sub
0
 
LVL 9

Expert Comment

by:zaghaghi
ID: 13669625
hi:

Change:
        Public Sub BindToCombo(ByVal myObj As System.Web.UI.WebControls.DropDownList)
To:
        Public Sub BindToCombo(ByRef myObj As System.Web.UI.WebControls.DropDownList)
0
 
LVL 9

Expert Comment

by:zaghaghi
ID: 13669653

also change:
    adapt.Fill(ds, "employee")
to
    adapt.Fill(ds)
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:chrispkotsiopoulos
ID: 13670392
zaghaghi I made the changes but I had no luck. I also changed BindCombo.BindToCombo(fnamecmb); to BindCombo.BindToCombo(ref fnamecmb);

ihenry the sub BindToCombo never executes as the program crashes at the point I try to call the sub:

BindCombo.BindToCombo(fnamecmb);
0
 
LVL 20

Expert Comment

by:ihenry
ID: 13670418
Ok, the problem seems come from the BindCombo object then. What's the BindCombo? are you sure you have initialized the object?

BindCombo = VBControls.DBConnect();
BindCombo.BindToCombo(fnamecmb);
0
 
LVL 20

Expert Comment

by:ihenry
ID: 13670431
sorry for the typo, it should be


BindCombo = new VBControls.DBConnect();
BindCombo.BindToCombo(fnamecmb);
0
 

Author Comment

by:chrispkotsiopoulos
ID: 13697475
Well...
It almost worked. It doesn't crash anymore but instead of filling the combo box with the 23 employee fnames it populates the combo with "System.Data.DataRowView" repeated 23 times!
0
 
LVL 20

Accepted Solution

by:
ihenry earned 1400 total points
ID: 13697782
Modify the BindToCombo function to

        ....
        adapt.Fill(ds, "employee")
        myObj.DataSource = ds
        myObj.DataTextField = "fname"
        myObj.DataBind()
        ....
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

777 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