A
accyboy1981
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
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