luckydexte
Programmer
Hello,
I am struggling with this problem for several days now and cannot find a solution. I have a web form, when I enter a value it does a query on the value entered. I have several dropdown lists and need to populate the values. Some of these lists are dynamic and some are static. Currently I having a problem with the static dropdown lists. An example of code that WORKS is below:
private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack) {
ddlActiveYN.Items.Insert(0, "Select One");
}
}
private void txtChargeCode_TextChanged(object sender, System.EventArgs e)
{
addChargeCodeDataToForm();
}
private void addChargeCodeDataToForm()
{
string strChargeCode;
strChargeCode = txtChargeCode.Text;
try {
cmdChargeCodeById.Parameters["@chargecode"].Value = strChargeCode;
sqlConnection1.Open();
System.Data.SqlClient.SqlDataReader dreader;
dreader = cmdChargeCodeById.ExecuteReader(CommandBehavior.SingleRow);
if (dreader.Read()) {
string strActive;
// Have to rebuild the DDL
ddlActiveYN.Items.Clear();
ddlActiveYN.Items.Insert(0, "Select One");
ddlActiveYN.Items.Insert(1, "Y");
ddlActiveYN.Items.Insert(2, "N");
ddlActiveYN.Items.FindByValue(strActive).Selected = true;
}
dreader.Close();
}
catch (System.Data.SqlClient.SqlException) {
lblError.Text = "Server Error: Unable to load database!";
}
catch (System.Web.HttpException) {
lblError.Text = "No idea what is going on!";
}
catch {
lblError.Text = "In Exception";
}
finally {
sqlConnection1.Close();
}
}
For some reason I have to rebuild the dropdown list before I can use the following line:
ddlActiveYN.Items.FindByValue(strActive).Selected = true;
If I do not use the clear, and then rebuild the DDL I get the following error:
A DropDownList cannot have multiple items selected.
Any ideas??? Thanks.
Brandon
I am struggling with this problem for several days now and cannot find a solution. I have a web form, when I enter a value it does a query on the value entered. I have several dropdown lists and need to populate the values. Some of these lists are dynamic and some are static. Currently I having a problem with the static dropdown lists. An example of code that WORKS is below:
private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack) {
ddlActiveYN.Items.Insert(0, "Select One");
}
}
private void txtChargeCode_TextChanged(object sender, System.EventArgs e)
{
addChargeCodeDataToForm();
}
private void addChargeCodeDataToForm()
{
string strChargeCode;
strChargeCode = txtChargeCode.Text;
try {
cmdChargeCodeById.Parameters["@chargecode"].Value = strChargeCode;
sqlConnection1.Open();
System.Data.SqlClient.SqlDataReader dreader;
dreader = cmdChargeCodeById.ExecuteReader(CommandBehavior.SingleRow);
if (dreader.Read()) {
string strActive;
// Have to rebuild the DDL
ddlActiveYN.Items.Clear();
ddlActiveYN.Items.Insert(0, "Select One");
ddlActiveYN.Items.Insert(1, "Y");
ddlActiveYN.Items.Insert(2, "N");
ddlActiveYN.Items.FindByValue(strActive).Selected = true;
}
dreader.Close();
}
catch (System.Data.SqlClient.SqlException) {
lblError.Text = "Server Error: Unable to load database!";
}
catch (System.Web.HttpException) {
lblError.Text = "No idea what is going on!";
}
catch {
lblError.Text = "In Exception";
}
finally {
sqlConnection1.Close();
}
}
For some reason I have to rebuild the dropdown list before I can use the following line:
ddlActiveYN.Items.FindByValue(strActive).Selected = true;
If I do not use the clear, and then rebuild the DDL I get the following error:
A DropDownList cannot have multiple items selected.
Any ideas??? Thanks.
Brandon