Populating a Dropdown Listbox

C

Curt Emich

Can someone help me understand why the following code won't work? I'm
connected to an Access database with a connection object, but I'm unable to
populate the dropdown list box on the form.

Also, I can't seem to print diagnostics anywhere using "Response.Write" or
anything else.





private void Page_Load(object sender, System.EventArgs e)

{

string sSQL = "SELECT Statement_Id,Statement_In_Brief FROM Statements";

daStatements = new OleDbDataAdapter(sSQL,oleDbConnection1);

daStatements.Fill(dsStatements,"Statements");

DropDownList1.DataSource = dsStatements.Tables["Statements"];

DropDownList1.DataBind();

Response.Write("test");

} // end PageLoad event
 
A

Andrew de la Harpe

You need to set the DataTextField property to "Statement_In_Brief " and
optionally the DataValueField to "Statement_Id" before you bind.
A
 
C

Curt Emich

Thanks for your response.

OK, I added these two lines right before the line where I bind:


DropDownList1.DataTextField = "Statement_In_Brief";
DropDownList1.DataValueField = "Statement_ID";


Still nothing. I know the query works because I've cut it out and
executed it in Access. I must be doing something else wrong.



Remulac
 
P

Paul

I don't write code in C# but maybe I can help you troubleshoot a little.

You didn't say if you stepped through the page_load() with the debugger
and saw the response.write("test") line fire. Are you saying that it
fires and yet you see nothing in the browser?

If that isn't working then something is seriously whacked and all bets
are off on the DataAdapter!

Since you get no output to display and you get no error messages I
suspect that the code isn't even firing, and yet that doesn't seem to
make sense. Page_Load() has to fire as far as I know.

I know this may seem strange, but you have a strange problem, and I'm
beginning to think you're firing the wrong web form. If you have not
tried, set the aspx page in question to Start Page by right clicking it
in Solution Explorer and selecting Set as Start Page.

Believe me, you wouldn't be the first one to have forgotten this.

Let's see how that goes but definitely set the debugger breakpoint in
page_load if that page is indeed set as the Start Page.

~Paul
 
A

Andrew de la Harpe

Ok let's simplify the code a little.

Try this
string sSQL = "SELECT Statement_Id,Statement_In_Brief FROM Statements";

daStatements = new OleDbDataAdapter(sSQL,oleDbConnection1);

daStatements.Fill(dsStatements)

Response.Write( "Record count = " +
dsStatements.Table[0].Rows.Count.ToString());

If you don't at least see "Record count = 0" then the routine is not being
called

Make sure the Page load event is being called

A
 
C

Curt Emich

I guess page load is not being called. I made sure that this aspx page
was the first one to load, but I was certain of that already just by
seeing the empty listbox, which is the only control on this form.

The simplified code certainly should work. I wonder how a form can be
called without the form load being called?

Remulac
 
A

Andrew de la Harpe

It can happen.
Usually if you are using VS it sets the event up for you.
Look for this in your page if you are using code behind.
or if not
the top of your page should have
AutoEventWireup="true"

#region Web Form Designer generated code

override protected void OnInit(EventArgs e)

{

//

// CODEGEN: This call is required by the ASP.NET Web Form Designer.

//

InitializeComponent();

base.OnInit(e);

}


/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeComponent()

{

this.Load += new System.EventHandler(this.Page_Load);

}

#endregion
 
P

Paul

Curt, there is a way for Page_Load( ) to fail, or any other built in
routine. Sometimes VS whacks the routine and it will not fire - ever -
until you delete it and let VS rebuild it. Seriously. This has pissed
me off on more than one occasion.

Do this: Copy the code in Page_Load( ) to clipboard, and delete the
entire code including the declaration of the routine. Very important to
delete it all.

Then reselect it from the drop downs in VS and it will reappear as
empty. Fine. Now paste the code back in and fire up the form - it will
work this time.

Good luck.

~Paul
 
C

Curt Emich

Paul,

Not quite following you on that last part. Where do I select it again?
From what drop down?

Remulac
 
P

Paul

Curt, we're in the code-behind, and you have to get rid of the bogus
Page_Load() event handler. First save all the code in it by doing a
text CUT to clipboard or some other safe location.

Now Delete the handler/method - including its header. Everything - nuke
it. Don't just delete your code - delete the whole handler.

Now there is a dropdownlist for methods on the right at the top of the
page. Reselect Page_Load from the method dropdownlist. When you select
it the IDE will generate a brand new handler and plop you into it
automatically. It will be empty, of course. But it will also fire when
you start your app.

NOW: Paste your code from the old handler into it.

There is no harm done from deleting framework generated event handlers
because it will recreate any that are missing (it won't do that for your
own handlers or methods, though, so watch out for that). The problem
I've seen several times over the last year of working with this stuff is
that the event handlers, or some of them, will get WHACKED by the
framework and they will not fire - period. No power on earth can make
them fire until you delete the bogus ones and redo the code for the new
ones.

Believe me, this isn't the last time you'll see this so remember it
because when it strikes there are no obvious signs like exceptions!

Lemme know ....
~Paul
 
C

Curt Emich

Are you referring to the dropdown list immediately to the left of the
solution explorer? In that dropdown, I see references to my connection
object, my dropdown list object, my data adapter objects, and the call
to InitializeComponent(). When I delete the entire Page_Load()
function, it no longer appears in that dropdown to "reselect". I'm not
sure what I'm missing here.

I've cut the code out, but I had to retype the handler back in, and then
paste the old code into it. This made no difference. What you're
saying makes sense, but I have yet to see where I can "reselect" the
Page_Load() handler.
 
C

Curt Emich

Paul,

Are you referring to the dropdown listbox to the left of the project
window? I have a dropdown to the upper right of my code window. It
lists InitializeComponent() and all my data ojects, as well as the call
to Page_Load(). However, when I cut out the Page_Load() handler
altogether as you've suggested, it's no longer listed in this dropdown.
Therefore, there's no way I can see to "re-select" Page_Load after I've
cut it out of the code. Sorry, I'm still confused.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top