dropdownlist blues - can't bind datasource to datareader in codebehind

P

Phil Streiff

I prototyped a data-entry webform w/ c# & inline aspx. After I got it
working, I tried to split my code out to a codebehind file (.cs) & now
my dropdownlist isn't populating with choices for the user to select.

Here's my code:
--------------------------------------------------

public void Page_Load (object sender, System.EventArgs e) {
if(!IsPostBack) {
SqlConnection myConn = new
SqlConnection(ConfigurationSettings.AppSettings["connString"]);
SqlCommand acctCmd = new SqlCommand("sp_Get_AccountType");
acctCmd.CommandType = CommandType.StoredProcedure;

myConn.Open();
SqlDataReader drAccount = acctCmd.ExecuteReader();

//Set up the data binding.
ddlAcctType.DataSource = drAccount;
ddlAcctType.DataTextField = "AccountType";
ddlAcctType.DataValueField = "AcctTypeID";
ddlAcctType.DataBind();

//Close the connection.
myConn.Close();
drAccount.Close();

//Add the item at the first position.
ddlAcctType.Items.Insert(0, "<---- Select ---->");
}
}
-----------------------------------------------------
here is aspx reference:
<asp:DropDownList id=ddlAcctType runat="server"
width="200px"></asp:DropDownList>
-----------------------------------------------------

The .aspx page now displays with no errors but the dropdownlist
appears empty. What am I doing wrong that's causing the ddl to not
populate in codebehind?

TIA,
Phil
 
T

Teemu Keiski

Hi,

a) confirm that the code is generally running
b) If it is, then check that should the procedure in question returns
something e.g is there any data?

a) Seems to be the problem because even at the first time doesn't seem to
get to the DDL.


--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist
http://blogs.aspadvice.com/joteke


I prototyped a data-entry webform w/ c# & inline aspx. After I got it
working, I tried to split my code out to a codebehind file (.cs) & now
my dropdownlist isn't populating with choices for the user to select.

Here's my code:
--------------------------------------------------

public void Page_Load (object sender, System.EventArgs e) {
if(!IsPostBack) {
SqlConnection myConn = new
SqlConnection(ConfigurationSettings.AppSettings["connString"]);
SqlCommand acctCmd = new SqlCommand("sp_Get_AccountType");
acctCmd.CommandType = CommandType.StoredProcedure;

myConn.Open();
SqlDataReader drAccount = acctCmd.ExecuteReader();

//Set up the data binding.
ddlAcctType.DataSource = drAccount;
ddlAcctType.DataTextField = "AccountType";
ddlAcctType.DataValueField = "AcctTypeID";
ddlAcctType.DataBind();

//Close the connection.
myConn.Close();
drAccount.Close();

//Add the item at the first position.
ddlAcctType.Items.Insert(0, "<---- Select ---->");
}
}
-----------------------------------------------------
here is aspx reference:
<asp:DropDownList id=ddlAcctType runat="server"
width="200px"></asp:DropDownList>
-----------------------------------------------------

The .aspx page now displays with no errors but the dropdownlist
appears empty. What am I doing wrong that's causing the ddl to not
populate in codebehind?

TIA,
Phil
 
P

Phil Streiff

Teemu:

Thanks for your reply. Per your suggestion, I tried to compile my .cs
file and got the following message:

Error CS5001 MaSSCRequest.aspx.exe does not have an entry point
defined. Normal termination.

I'll do some research to find out what this means. If you have any
ideas, let me know.

TIA,
Phil
 
T

Teemu Keiski

Umm...that sounds like you have wrong type of VS project in use? Compiling
ASP.NET project will never result into an exe file (or why would it have
aspx.exe file in use).

Check the project options and there everything related to the
compilation/project type.

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist
http://blogs.aspadvice.com/joteke

Teemu:

Thanks for your reply. Per your suggestion, I tried to compile my .cs
file and got the following message:

Error CS5001 MaSSCRequest.aspx.exe does not have an entry point
defined. Normal termination.

I'll do some research to find out what this means. If you have any
ideas, let me know.

TIA,
Phil
 

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

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top