Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.0 and 1.1 Basics
|
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 27th, 2004, 09:48 PM
Registered User
 
Join Date: Jan 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Populating a Dropdown with C# backed with SQL Ser

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!!!

 
Old January 28th, 2004, 11:40 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Quote:
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.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Populating dropdown within a datagrid ninel ASP.NET 2.0 Professional 2 May 31st, 2006 02:41 AM
Populating dropdown within a datagrid ninel ASP.NET 2.0 Basics 7 May 26th, 2006 03:26 PM
Populating dropdown within a datagrid ninel General .NET 0 May 26th, 2006 12:27 PM
populating dropdown values rupen Classic ASP Basics 16 October 10th, 2005 09:48 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.