I'm afraid I might have added unnecessary confusion to your situation. But
if you do want a <select you want to use a DropDownList, not a datalist.
<asp:dropdownlist id="ddl" runat="server" />
// Put user code to initialize the page here
String connectionString = "Initial Catalog=Grocery;Data
Source=localhost;user=Grocery";
DataSet ds = new DataSet();
SqlConnection cn = new SqlConnection(connectionString);
cn.Open();
//SqlCommand cmd = new SqlCommand("Select * From UserQualifiedName", cn);
//cmd.CommandType = CommandType.Text;
String select = "SELECT UserID, LastName + ', ' + FirstName AS Name FROM
dbo.[User]";
SqlDataAdapter da = new SqlDataAdapter(select, cn);
da.Fill(ds);
ddl.DataSource = ds;
ddl.DataTextField = "Name" 'this is new
ddl.DataValueField = "UserId 'this is new
ddl.DataBind();
Don't worry about datalist's for now :)
As for the datareader vs the dataset it doesn't have anything to do with the
control you are using it with, but how you want to deal with the actual data
container (datareader or dataset). The datareader is connected to the
database and can only be read forward - once. it's like a forward-only
recordset in ASP. It can't be cached, you can't loop through it twice or do
fancy things with it, but man it's fast ;) The Dataset is diconnected. The
minute you do a Da.fill(ds), the dataset is a full blown asp.net object -
forget the database. It can be cached in memory, you can add rows to it (it
won't add them automatically to the database though), you can add columns,
loop through it as many times as you want...it's a lot more like an array
from that sense...
Hope this helps.
karl
<St*************@IDX.COM> wrote in message
news:eh*************@tk2msftngp13.phx.gbl...
I am now officially confused about things I used to understand in ASP! All
I want to do is have a list similar to <SELECT of yonder days. I want the
items in a SQL Server View to show in the list. In the olden days of my
youth, this would have been done by looping through a recordset with a
forward only/read only cursor. The options seem to be:
1. A DataList bound to a DataSet which is populated by a DataAdapter. This
is what I seem to have tried to do here. (I have tried a bunch of things
and am QUITE confused.) I apparently am missing only ItemTemplate in this
case. (The QuickStart did not seem to cover the ItemTemplate at all,
unless my brain is squishy.)
2. Somehow I ought to be able to use a DataReader. But I cannot see what
control is best used with DataReader and how. It seems that if you use
these server side controls, instead of HTML tags, the controls maintain
some kind of state through the use of corresponding hidden elements so
that trips to the server still yield the same value in the control. This
is pretty nifty. So what control plays nice with DataReader?
I am screwed when I have to do something HARD! Just kidding. It is always
like this for me. Early in the learning phase I resemble a fish flapping
on the beach. After a while I will start getting familiar!
Thanks for your help.
S
Ooppss..:) I thought she was using a dropdownlist :) You can use a
datagrid instead of a list to get things to automatically show up.
Karl
"Craig Deelsnyder" <cdeelsny@no_spam_4_meyahoo.com> wrote in message
news:op**************@web-1161d.webservices.nrt... On Mon, 23 Aug 2004 08:48:09 -0700, <St*************@IDX.COM> wrote:
> Hi. I am trying to get used to AS.NET. I have been doing ASP classic
for > years, and am now in a position to do ASP.NET. I am in the stumbling > around until I get my bearings phase. I hope you will bear with me. I am > going through the QuickStart. After reading a little, I am trying to
> implement a simple page on a simple project I have made up. I have a
page > called default.aspx. I want it to load a list of user names from a
SQL > database when the page loads.
>
> Observe this from the HTML view
>
>
> <asp:datalist id=dlUsers runat="server" BorderColor="#C00000"
> BorderStyle="Double"></asp:DataList>
>
> (I added the border because I could not tell if the datalist was absent > or
> the data which should be IN the datalist.
>
> Observe the underlying class code:
>
> private void Page_Load(object sender, System.EventArgs e) > {
> // Put user code to initialize the page here
> String connectionString = "Initial
> Catalog=Grocery;Data Source=localhost;user=Grocery";
> DataSet ds = new DataSet();
> SqlConnection cn = new
> SqlConnection(connectionString);
> cn.Open();
> //SqlCommand cmd = new SqlCommand("Select *
From > UserQualifiedName", cn);
> //cmd.CommandType = CommandType.Text;
>
> String select = "SELECT UserID, LastName +
', ' + > FirstName AS Name FROM dbo.[User]";
> SqlDataAdapter da = new
SqlDataAdapter(select, > cn);
>
> da.Fill(ds);
> dlUsers.DataSource = ds;
> dlUsers.DataBind();
>
> }
>
> This code runs, but it does not list in the datalist. The datalist
> displays as I see a tiny red box completely empty of content. I am not > seeing any methods for the SqlDataAdapter like Open or Make It So > Number > 1. There is defintely a record in the table in question.
>
> Can anyone give me a nudge on what I might be doing wrong? My handy
dandy > ASP.NET books won't arrive from amazon until tomorrow.
>
> Thanks!
>
> S
>
You need to define an ItemTemplate for the DataList (inside of it); right now you haven't defined anything for the content of each item. Only the DataGrid can auto-generate its columns.....
The QuickStart should show how to do this...or Google a little on > DataList and ItemTemplate...
--
Craig Deelsnyder
Microsoft MVP - ASP/ASP.NET