Results 1 to 3 of 3

Thread: New to C# and .NET, Need some help with Database stuff *resolved*

  1. #1

    Thread Starter
    Fanatic Member Graff's Avatar
    Join Date
    Jan 2002
    Location
    Calgary
    Posts
    668

    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.

  2. #2
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    Here is something I wipped up. Hope it helped.

    PHP Code:
    private void Page_Load(object senderSystem.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

  3. #3

    Thread Starter
    Fanatic Member Graff's Avatar
    Join Date
    Jan 2002
    Location
    Calgary
    Posts
    668
    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:
    1. private void Page_Load(object sender, System.EventArgs e)
    2.         {
    3.             SqlConnection conn = new SqlConnection("server=localhost;uid=jkoa;pwd=jkao;database=Test");
    4.             SqlCommand cmd = conn.CreateCommand();
    5.             cmd.CommandText = "SELECT Text,Address FROM Links";
    6.  
    7.             conn.Open();
    8.  
    9.                 SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
    10.                
    11.                 while(rdr.Read())
    12.                 {
    13.                     cboLinks.Items.Add(new ListItem(rdr.GetValue(0).ToString(),rdr.GetValue().ToString()));
    14.                 }
    15.  
    16.             conn.Close();
    17.         }
    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
  •  



Click Here to Expand Forum to Full Width