I’m using C# ASP.NET, and I’ve got a submission page which, when you click the add product link, opens a popup “add product” page that has a dropdown list of products which can be added to a submission. Business rules dictate there can be only one instance of each product on any submission, so I’ve run two separate switch case statements, one to eliminate items from the drop down list once they’ve been selected in the popup, and one which queries the db to determine if there are any products already associated with the submission, and if so, eliminate those options from the drop down as well. Once the user selects an item from the dropdown, a panel for that product is made visible for user input.
Both of these switch cases function, in that they eliminate the items which should not be available, but since I put in the queried switch case, the panels no longer become visible upon selection. When I remove the queried switch case, the panels do become visible upon selection, but the products which are already associated with the submission are still available in the dropdown.
I know there is a conflict here somewhere, but I’m racked as to what it is as this is my first go at programming. The code is below. Please disregard the queries themselves; this is a non-active training project and my handler forbade parameterization because he wanted me to learn basics first. I understand the injection vulnerabilities.
public partial class AddProduct : System.Web.UI.Page
{
protected void BtnAddProduct_Click(object sender, EventArgs e)
{
switch (DdlProductList.SelectedValue)
{
case "1":
PanelEpl.Visible = true;
DdlProductList.Items.Remove(DdlProductList.SelectedItem);
break;
case "2":
PanelProf.Visible = true;
DdlProductList.Items.Remove(DdlProductList.SelectedItem);
break;
case "3":
PanelCrime.Visible = true;
DdlProductList.Items.Remove(DdlProductList.SelectedItem);
break;
case "4":
PanelFid.Visible = true;
DdlProductList.Items.Remove(DdlProductList.SelectedItem);
break;
case "5":
PanelNotProf.Visible = true;
DdlProductList.Items.Remove(DdlProductList.SelectedItem);
break;
case "6":
PanelPriv.Visible = true;
DdlProductList.Items.Remove(DdlProductList.SelectedItem);
break;
case "7":
PanelPub.Visible = true;
DdlProductList.Items.Remove(DdlProductList.SelectedItem);
break;
default:
break;
}
}
protected void Page_Load(object sender, EventArgs e)
{
string x = Request.QueryString["SubId"];
string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
string displayQuery = "SELECT CustName, CustAdd, CustCity, CustState, CustZip FROM Customer WHERE SubId =" + x;
string broQuery = "SELECT EntityType FROM Broker WHERE SubId =" + x;
string ddlQuery = "SELECT ProductId FROM SubmissionProducts WHERE SubmissionId =" + x;
using (SqlConnection displayConn = new SqlConnection(connectionString))
{
displayConn.Open();
SqlCommand DlistCmd = new SqlCommand(ddlQuery, displayConn);
using (SqlDataReader Ddldr = DlistCmd.ExecuteReader())
{
while (Ddldr.Read())
{
switch (Ddldr.GetInt32(0))
{
case 1:
DdlProductList.Items.RemoveAt(1);
break;
case 2:
DdlProductList.Items.RemoveAt(2);
break;
case 3:
DdlProductList.Items.RemoveAt(3);
break;
case 4:
DdlProductList.Items.RemoveAt(4);
break;
case 5:
DdlProductList.Items.RemoveAt(5);
break;
case 6:
DdlProductList.Items.RemoveAt(6);
break;
case 7:
DdlProductList.Items.RemoveAt(7);
break;
default:
break;
}
}
}
I think you are not considering the Page postback state. Put your page_load code under the
If(!IsPostBack)condition: