Subject: Populating a Dropdown with C# backed with SQL Ser
Posted By: DotNetNoob Post Date: 1/27/2004 8:48:34 PM
Hey guys I am wondering if u can help me out with something.
I can't seem to fill the dropdown list on my .aspx file =S
=============================================
Let just say i have createnewaccount.aspx in it is a aspx form and this dropdown:
=============================================
<aspropDownList id="dpdProvinces" runat="server"></aspropDownList>


==============================================
Then in my createnewaccount.cs file I have the following function
==============================================
        //Fill a dropdown list Parameters: Dropdown id, database table name
        public static bool FillDropDownList(DropDownList dDl,string tblName)
        {            
            const string strServer = "testServer";
            const string strDBId = "SA";
            const string strDBPwd = "";
            const string strDBName = "dotNetDemo";
            //Connection string for SQL Server DB
            string ConnectionString =
                "server=" + strServer +
                ";uid=" + strDBId +
                ";pwd=" + strDBPwd +
                ";database=" + strDBName + ";";
            //Create a connection
            SqlConnection conn = new SqlConnection(ConnectionString);
            try
            {
                //SQL string to execute
                string strSQL = "SELECT * FROM " + tblName;
                SqlDataAdapter myDataAdapter = new SqlDataAdapter(strSQL, conn);
                DataSet myDataSet =  new DataSet();
                myDataAdapter.Fill(myDataSet,tblName);
                //Set the Data Value and Text fields to the db column
                dDl.DataTextField  = myDataSet.Tables[0].Columns["Name"].ColumnName.ToString();
                dDl.DataValueField = myDataSet.Tables[0].Columns["Id"].ColumnName.ToString();
                dDl.Items.Insert(0, "<--- Select --->");
            }    
            catch // Exception Removed
            {
                return false;    
            }
                finally
            {
                conn.Close();
            }
            return true;
        } // end Fill FillDropDownList function



Now should that work already by the databind i did with the function or do i have to loop through the table data and fill in the function? i tried both ways and doesn't seem to work. I kno my connection string works because I tried it somewhere else.. and I have data in the Province table and the fields are named correctly.. any suggestions would be appreciated.. thanks!!!

Reply By: planoie Reply Date: 1/28/2004 10:40:56 AM
quote:
Originally posted by DotNetNoob
Then in my createnewaccount.cs file I have the following function
==============================================
        //Fill a dropdown list Parameters: Dropdown id, database table name
        public static bool FillDropDownList(DropDownList dDl,string tblName)

This interests me for two reasons:
A) why is the file *.cs and not *.aspx.cs? Was this a typo? Usually (in VS.net, the codebehind file is named just like the web form file *.aspx but with .cs tacked on.  But you could certainly manually do it differently.
B) Your function is modified as "static".  This further supports the idea that this function is not in the webform's class code (either codebehind or inline), which leads to my next question...

Where are you calling this function?

Peter
------------------------------------------------------
Work smarter, not harder.

Go to topic 9088

Return to index page 961
Return to index page 960
Return to index page 959
Return to index page 958
Return to index page 957
Return to index page 956
Return to index page 955
Return to index page 954
Return to index page 953
Return to index page 952