|
-
Apr 19th, 2003, 06:10 PM
#1
Thread Starter
Fanatic Member
New to C# and .NET, Need some help with Database stuff *resolved*
I'm trying to pull some records from my MS SQL 2000 Database and I've been having problems determining how I can get these values into an drop down list.
Here's the code I have thus far.
Code:
private void Page_Load(object sender, System.EventArgs e)
{
SqlConnection conn;
SqlDataAdapter objDataReader;
// Put user code to initialize the page here
// cboLinks.Items.Add(new ListItem("Fark","http://www.fark.com"));
conn = new SqlConnection("server=localhost;uid=admin;pwd=adsf;Database=Test");
String SQLSelect = "SELECT * FROM Links";
objDataReader = new SqlDataAdapter(SQLSelect,conn);
//Don't know what goes here to add to combo box
conn.Close();
}
Edit: Oh yea the table is called Links, it contains 3 columns; ID, Text and Address.
Last edited by Graff; Apr 20th, 2003 at 03:04 PM.
If wishes were fishes we'd all cast nets.
-
Apr 19th, 2003, 08:13 PM
#2
Frenzied Member
Here is something I wipped up. Hope it helped.
PHP Code:
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)//Test for postback
{
SqlConnection conn = new SqlConnection("server=localhost;uid=sa;pwd=;database=pubs");
SqlCommand cm = conn.CreateCommand(); //create sql command
cm.CommandText = "Select * From Authors";
conn.Open();//open connection
SqlDataReader rdr = cm.ExecuteReader(CommandBehavior.CloseConnection); //execute reader and close connection after
cboList.DataSource = rdr; //using databinding; setup the datasource; NOTE: this line wont work for winforms apps
cboList.DataTextField = "au_fname"; //sets the column of the db table to load in the combo box
cboList.DataValueField = "au_lname"; //sets the column of the db table you want to display if you select whats in the combo box
cboList.DataBind(); //bind the control
}
}
Dont gain the world and lose your soul
-
Apr 20th, 2003, 02:21 PM
#3
Thread Starter
Fanatic Member
For some reason the value field wasn't incrimenting with the text field. I found a different way of doing it, it also cuts down on code a bit;
VB Code:
private void Page_Load(object sender, System.EventArgs e)
{
SqlConnection conn = new SqlConnection("server=localhost;uid=jkoa;pwd=jkao;database=Test");
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "SELECT Text,Address FROM Links";
conn.Open();
SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
while(rdr.Read())
{
cboLinks.Items.Add(new ListItem(rdr.GetValue(0).ToString(),rdr.GetValue().ToString()));
}
conn.Close();
}
If wishes were fishes we'd all cast nets.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|