dropdownlist and sqlquery result in System.Data.Common.DbDataRecor

G

Guest

I know this must be something very simple, since none of the online samples
for populating a dropdownlist from a sql database indicate this is difficult
with asp.net... but I'm still not getting what I'm hoping for.

Basically, I can attach to my database, query results and if I use a
datagrid, I can see all of my columns/rows. What I want to do now is perform
the same query and display only one of my columns in a dropdownlist, so that
I can later use the list to perform specialized queries.

My query works, but my display is showing "System.Data.Common.DbDataRecord"
instead of the row values in my database.

I know I must be missing something trivial, a using statement, or a cast of
some kind or a property setting, but I can't locate what I'm not doing
correctly?

Can anyone shed a little light for me?

Thanks,

Sherwood

My code below:

<asp:DropDownList id="DropDownChoice" runat="server" ></asp:DropDownList>

if (!IsPostBack)
{
SqlCommand myCommand = new SqlCommand("select distinct myColumn from
myTable", myConnection);

myConnection.Open();

DropDownChoice.DataSource=
myCommand.ExecuteReader(CommandBehavior.CloseConnection);

DropDownChoice.DataBind();

DropDownChoice.Items.Insert(0, new ListItem("-- Choose --"));

}
 
G

Guest

Probably you need to specify which field in the table you want to bind with
and display even though you only have one collumn in the datareader. Try to
insert this line of code
DropDwonChoice.DataTextField = "myCollumn";
 
G

Guest

Thanks! I put that line before the executereader and it works now.

DropDownChoice.DataTextField = "myColumn";
DropDownChoice.DataSource=
myCommand.ExecuteReader(CommandBehavior.CloseConnection);

Sherwood
 

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