I have First DropDownList as a selection of a Lens Material. Based upon that material selection, a second list fills with lenses available in that material. The problem is,,, when you do click the submit buttom, the information written to the database
is not correct for the second dropdown list. It chooses the first item in the list rather than the selected item and writes that info to the db. I am NOT well versed on coding or development so I need all of the help I can get.
Thank all for any and all input. Below is the routine for filling the dropdownlists
// Initialize Connection
conn = new SqlConnection(connectionString);
comm = new SqlCommand("SELECT ECPLensType1.*, ECPLenses.Material FROM ECPLenses INNER JOIN ECPLensType1 ON ECPLensType1.LensID = ECPLenses.LensID WHERE ECPLensType1.LensID=" + cboODMaterial.SelectedItem.Value, conn);
// Error handling for database code
try
{
//Open the connection
conn.Open();
//Close the reader
reader.Close();
}
catch
{
lblError.Text = "* An error occurred retrieving a list of Available Lenses..";
}
finally
{
//Close the connection
conn.Close();
}
}
Its possible that you may not be appending databound items. Click on your dropdown list, click properties. Check that Append Databound Items is set to true.
// Initialize Connection
conn = new SqlConnection(connectionString);
comm = new SqlCommand("SELECT ECPLensType1.*, ECPLenses.Material FROM ECPLenses INNER JOIN ECPLensType1 ON ECPLensType1.LensID = ECPLenses.LensID WHERE ECPLensType1.LensID=" + cboODMaterial.SelectedItem.Value, conn);
// Error handling for database code
try
{
//Open the connection
conn.Open();
//Close the reader
reader.Close();
}
catch
{
lblError.Text = "* An error occurred retrieving a list of Available Lenses..";
}
finally
{
//Close the connection
conn.Close();
}
}
comm = new SqlCommand("SELECT ECPLensType1.*, ECPLenses.Material FROM ECPLenses INNER JOIN ECPLensType1 ON ECPLensType1.LensID = ECPLenses.LensID WHERE ECPLensType1.LensID=" + cboODMaterial.SelectedValue.ToString(), conn);
set a breakpoint at this point to see what the SqlCommand actually is.
Thank you for your suggestion. It does not work. :(
On selection of the lens material in the first ddl it does repopulate the second ddl with the available lenses in that material. You can select the lens that you want and it show in the ddl that it is selected.
The problem is... Once you click the submit button to write the info to the database, the lens selection changes to the first lens in the ddl and writes THAT lens selection to the database and not the one in which you actually selected.
Not that I'm a fan of posting complete code but in this case, I'm thinking that is what you'll need to do. Post the full code for both aspx and code behind so I can replicate your scenario here. I think what you want to happen is to keep that selection you
chose after you set it. Instead of autopostback=true, why don't you use the OnIndexChanged event. This way, it sets a value that can be accessed over and over again until you change it again, then it changes the variable's value.
eyeguy1
Member
2 Points
61 Posts
HELP.. Filling Second DropDownList based on parameters of selection in another DropDown List.
May 07, 2012 05:31 PM|LINK
I have First DropDownList as a selection of a Lens Material. Based upon that material selection, a second list fills with lenses available in that material. The problem is,,, when you do click the submit buttom, the information written to the database is not correct for the second dropdown list. It chooses the first item in the list rather than the selected item and writes that info to the db. I am NOT well versed on coding or development so I need all of the help I can get.
Thank all for any and all input. Below is the routine for filling the dropdownlists
SqlConnection conn;
SqlCommand comm;
SqlDataReader reader;
// Read the connection string from Web.Config
string connectionString =
ConfigurationManager.ConnectionStrings["ValueVision"].ConnectionString;
// Initialize Connection
conn = new SqlConnection(connectionString);
comm = new SqlCommand("SELECT ECPLensType1.*, ECPLenses.Material FROM ECPLenses INNER JOIN ECPLensType1 ON ECPLensType1.LensID = ECPLenses.LensID WHERE ECPLensType1.LensID=" + cboODMaterial.SelectedItem.Value, conn);
// Error handling for database code
try
{
//Open the connection
conn.Open();
reader = comm.ExecuteReader();
cboODStyle.DataSource = reader;
cboODStyle.DataValueField = "LensID";
cboODStyle.DataTextField = "Description";
cboODStyle.DataBind();
//Close the reader
reader.Close();
}
catch
{
lblError.Text = "* An error occurred retrieving a list of Available Lenses..";
}
finally
{
//Close the connection
conn.Close();
}
}
bbcompent1
All-Star
32994 Points
8509 Posts
Moderator
Re: HELP.. Filling Second DropDownList based on parameters of selection in another DropDown List...
May 07, 2012 05:35 PM|LINK
Its possible that you may not be appending databound items. Click on your dropdown list, click properties. Check that Append Databound Items is set to true.
eyeguy1
Member
2 Points
61 Posts
Re: HELP.. Filling Second DropDownList based on parameters of selection in another DropDown List...
May 07, 2012 05:51 PM|LINK
Thank you...
But, that did not resolve the issue
bbcompent1
All-Star
32994 Points
8509 Posts
Moderator
Re: HELP.. Filling Second DropDownList based on parameters of selection in another DropDown List...
May 07, 2012 05:53 PM|LINK
Post the aspx code as well. Might be something in there that's causing it.
eyeguy1
Member
2 Points
61 Posts
Re: HELP.. Filling Second DropDownList based on parameters of selection in another DropDown List...
May 07, 2012 06:07 PM|LINK
<asp:DropDownList ID="cboODMaterial" runat="server" AutoPostBack="true" OnSelectedIndexChanged="btnSelectOD_Click1"
style="z-index: 1; left: 168px; top: 18px; position: absolute; width: 143px; height: 22px"
TabIndex="6">
</asp:DropDownList>
<asp:DropDownList ID="cboODStyle" runat="server"
style="z-index: 1; left: 409px; top: 17px; position: absolute; width: 248px"
TabIndex="7" AppendDataBoundItems="True" AutoPostBack="True">
</asp:DropDownList>
Here is more of the code behind file
protected void Page_Load(object sender, EventArgs e)
{
if (Page.Session["Username"] == null && Page.Session["Username"] == null)
{
Response.Redirect("ECPLogin.aspx");
}
lblWelcome.Text = "Welcome back " + Session["Username"];
lblAccountNumber.Text = Session["Username"].ToString();
if (!IsPostBack)
LoadLensMaterialList();
if(!IsPostBack)
btnSelectOD_Click1(null, null);
}
private void LoadLensMaterialList()
{
SqlConnection conn;
SqlCommand comm;
SqlDataReader reader;
// Read the connection string from Web.Config
string connectionString =
ConfigurationManager.ConnectionStrings["ValueVision"].ConnectionString;
// Initialize Connection
conn = new SqlConnection(connectionString);
comm = new SqlCommand("SELECT LensID, Material FROM ECPLenses", conn);
// Error handling for database code
try
{
//Open the connection
conn.Open();
reader = comm.ExecuteReader();
cboODMaterial.DataSource = reader;
cboODMaterial.DataValueField = "LensID";
cboODMaterial.DataTextField = "Material";
cboODMaterial.DataBind();
//Close the reader
reader.Close();
}
catch
{
lblError.Text = "* An error occurred retrieving a list of Available Lenses..";
}
finally
{
//Close the connection
conn.Close();
}
}
protected void btnSelectOD_Click1(object sender, EventArgs e)
{
{
// Response.Write("Filling");
SqlConnection conn;
SqlCommand comm;
SqlDataReader reader;
// Read the connection string from Web.Config
string connectionString =
ConfigurationManager.ConnectionStrings["ValueVision"].ConnectionString;
// Initialize Connection
conn = new SqlConnection(connectionString);
comm = new SqlCommand("SELECT ECPLensType1.*, ECPLenses.Material FROM ECPLenses INNER JOIN ECPLensType1 ON ECPLensType1.LensID = ECPLenses.LensID WHERE ECPLensType1.LensID=" + cboODMaterial.SelectedItem.Value, conn);
// Error handling for database code
try
{
//Open the connection
conn.Open();
reader = comm.ExecuteReader();
cboODStyle.DataSource = reader;
cboODStyle.DataValueField = "LensID";
cboODStyle.DataTextField = "Description";
cboODStyle.DataBind();
//Close the reader
reader.Close();
}
catch
{
lblError.Text = "* An error occurred retrieving a list of Available Lenses..";
}
finally
{
//Close the connection
conn.Close();
}
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
SqlConnection conn;
SqlCommand comm;
string connectionString = ConfigurationManager.ConnectionStrings["ValueVision"].ConnectionString;
conn = new SqlConnection(connectionString);
comm = new SqlCommand(
"INSERT INTO OrdersDB (AccountNumber, Username, Lastname, Firstname, JobType, OrderType, ODMaterial, ODStyle, " +
........................................................................................
bbcompent1
All-Star
32994 Points
8509 Posts
Moderator
Re: HELP.. Filling Second DropDownList based on parameters of selection in another DropDown List...
May 08, 2012 12:44 PM|LINK
I think your query should be like this instead:
comm = new SqlCommand("SELECT ECPLensType1.*, ECPLenses.Material FROM ECPLenses INNER JOIN ECPLensType1 ON ECPLensType1.LensID = ECPLenses.LensID WHERE ECPLensType1.LensID=" + cboODMaterial.SelectedValue.ToString(), conn);set a breakpoint at this point to see what the SqlCommand actually is.
eyeguy1
Member
2 Points
61 Posts
Re: HELP.. Filling Second DropDownList based on parameters of selection in another DropDown List...
May 08, 2012 02:22 PM|LINK
Thank you for your suggestion. It does not work. :(
On selection of the lens material in the first ddl it does repopulate the second ddl with the available lenses in that material. You can select the lens that you want and it show in the ddl that it is selected.
The problem is... Once you click the submit button to write the info to the database, the lens selection changes to the first lens in the ddl and writes THAT lens selection to the database and not the one in which you actually selected.
bbcompent1
All-Star
32994 Points
8509 Posts
Moderator
Re: HELP.. Filling Second DropDownList based on parameters of selection in another DropDown List...
May 08, 2012 02:32 PM|LINK
Not that I'm a fan of posting complete code but in this case, I'm thinking that is what you'll need to do. Post the full code for both aspx and code behind so I can replicate your scenario here. I think what you want to happen is to keep that selection you chose after you set it. Instead of autopostback=true, why don't you use the OnIndexChanged event. This way, it sets a value that can be accessed over and over again until you change it again, then it changes the variable's value.
sriramabi
Contributor
4351 Points
1277 Posts
Re: HELP.. Filling Second DropDownList based on parameters of selection in another DropDown List...
May 08, 2012 02:39 PM|LINK
Hai
pls remove AutoPostBack="True" property in Your Two Dropdownlist...
thaen check it....
thank u
bbcompent1
All-Star
32994 Points
8509 Posts
Moderator
Re: HELP.. Filling Second DropDownList based on parameters of selection in another DropDown List...
May 08, 2012 02:41 PM|LINK
Remove AutoPostBack="True" from cboODStyle drop down list.