By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,656 Members | 800 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,656 IT Pros & Developers. It's quick & easy.

Drop Down Lists Query

P: n/a
Hi,

I new to C# so please forgive me if this is simple. I've got 2 drop
down lists the first is hard coded with data where as the second is
populated from a database. The options that appear in the second should
be determined by what is selected in the first drop down list. When the
page loads it automatically populates the second list with the default
value via a proceedure:

public void popDDL(string dbName)
{

string strConn;
string strSQL;
strConn = "SERVER=localhost,1433; DATABASE="+dbName+"; user
id=HMI_User; password=HMI_User; Language=British";
strSQL = @"SELECT name FROM table1 ORDER BY name";
conn = new SqlConnection( strConn );

try
{
conn.Open();
cmdSelect = new SqlCommand(strSQL, conn);
SqlDataReader dtr = cmdSelect.ExecuteReader();
if (dtr.HasRows)
{
ddlMachine.DataSource = dtr;
ddlMachine.DataTextField = "name";
ddlMachine.DataValueField = "name";
ddlMachine.DataBind();
}
}
catch (Exception ex)
{
lblError.Text = "Error! Could not generate search information!
"+ex;
}
finally
{
conn.Close();
}
}

In the first drop down list the values are the database names. What I'm
am trying to do is something like:
onClick(popDDL("databasename"))
so the page is refreshed with the new data however I'm not sure how to
do it in C#. Any help would be appreciated.

Thanks

Simon

Jan 27 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
This is simple in .NET but you must remember that there will be a post back
to the server. In the DropDownList control add this:

AutoPostBack="True"
OnSelectedIndexChanged="FirstDropDownSelectionChan ge"

Where FirstDropDownSelectionChange is the method name that refreshes the 2nd
drop down.

--
adi
"accyboy1981" wrote:
Hi,

I new to C# so please forgive me if this is simple. I've got 2 drop
down lists the first is hard coded with data where as the second is
populated from a database. The options that appear in the second should
be determined by what is selected in the first drop down list. When the
page loads it automatically populates the second list with the default
value via a proceedure:

public void popDDL(string dbName)
{

string strConn;
string strSQL;
strConn = "SERVER=localhost,1433; DATABASE="+dbName+"; user
id=HMI_User; password=HMI_User; Language=British";
strSQL = @"SELECT name FROM table1 ORDER BY name";
conn = new SqlConnection( strConn );

try
{
conn.Open();
cmdSelect = new SqlCommand(strSQL, conn);
SqlDataReader dtr = cmdSelect.ExecuteReader();
if (dtr.HasRows)
{
ddlMachine.DataSource = dtr;
ddlMachine.DataTextField = "name";
ddlMachine.DataValueField = "name";
ddlMachine.DataBind();
}
}
catch (Exception ex)
{
lblError.Text = "Error! Could not generate search information!
"+ex;
}
finally
{
conn.Close();
}
}

In the first drop down list the values are the database names. What I'm
am trying to do is something like:
onClick(popDDL("databasename"))
so the page is refreshed with the new data however I'm not sure how to
do it in C#. Any help would be appreciated.

Thanks

Simon

Jan 27 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.