Note that there are some explanatory texts on larger screens.

plurals
  1. PODrop down list based on another drop down list
    primarykey
    data
    text
    <p>I am having an issue with two interrelated drop down lists. dlJobName is a list of current jobs in the system. dlStage is a list of available stages based on the selected value of dlJobName. I have autopostback = True on both. There is a third drop down list that has a list of values. The three selections together are used by a gridview to produce the results. I am having trouble with the dlStage dropdown list. I am getting an error : 'dlStage' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value </p> <p>The question I have is what is the best way to code this to prevent this error. What appears to be happening is that I have tried to retain the last value selected in a variable and it is causing my issue. Here is the code :</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Data.SqlClient; using System.Web.Security; using System.Configuration; namespace AnnoTracker { public partial class WebForm1 : System.Web.UI.Page { public static class MyVariables { public static int PI = 0; public static string JN = ""; public static string ST = ""; public static string AT = ""; } protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { if (MyVariables.AT == "") { dlAnnoType.SelectedValue = "Agency Error"; dlAnnoType.DataBind(); } if (MyVariables.AT != "") { dlAnnoType.SelectedValue = MyVariables.AT; dlAnnoType.DataBind(); } if (MyVariables.JN == "") { dlJobName.SelectedIndex = 0; dlJobName.DataBind(); } if (MyVariables.JN != "") { dlJobName.SelectedValue = MyVariables.JN; dlJobName.DataBind(); } if (MyVariables.ST == "") { dlStage.SelectedIndex = 0; dlStage.DataBind(); } if (MyVariables.JN != "") { dlStage.SelectedValue = MyVariables.ST; dlStage.DataBind(); } dlStage.SelectedIndex = 0; dlStage.DataBind(); BindData(); if (MyVariables.PI == 0) { gvSummary.PageIndex = 0; gvSummary.DataBind(); } if (MyVariables.PI != 0) { gvSummary.PageIndex = MyVariables.PI; gvSummary.DataBind(); } } } protected void EditSummary(object sender, GridViewEditEventArgs e) { gvSummary.EditIndex = e.NewEditIndex; string _custName = gvSummary.DataKeys[e.NewEditIndex].Value.ToString(); BindData(); } protected void CancelEdit(object sender, GridViewCancelEditEventArgs e) { gvSummary.EditIndex = -1; BindData(); } protected void gvSummary_PageIndexChanging(object sender, GridViewPageEventArgs e) { gvSummary.PageIndex = e.NewPageIndex; MyVariables.PI = e.NewPageIndex; BindData(); } protected void RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow &amp;&amp; gvSummary.EditIndex == e.Row.RowIndex) { DropDownList dlBU = (DropDownList)e.Row.FindControl("dlBU"); string _custName = gvSummary.DataKeys[e.Row.RowIndex].Values[1].ToString(); string BUquery = "select distinct Unit from vw_BU where Business='" + _custName + "'"; SqlCommand BUcmd = new SqlCommand(BUquery); dlBU.DataSource = GetData(BUcmd); dlBU.DataTextField = "Unit"; dlBU.DataValueField = "Unit"; dlBU.DataBind(); dlBU.Items.FindByValue((e.Row.FindControl("lblBU") as Label).Text).Selected = true; DropDownList dlPA = (DropDownList)e.Row.FindControl("dlPA"); string _PAcustName = gvSummary.DataKeys[e.Row.RowIndex].Values[1].ToString(); string PAquery = "select PA from PA where Business='" + _PAcustName + "' order by PA"; SqlCommand PAcmd = new SqlCommand(PAquery); dlPA.DataSource = GetData(PAcmd); dlPA.DataTextField = "PA"; dlPA.DataValueField = "PA"; dlPA.DataBind(); dlPA.Items.FindByValue((e.Row.FindControl("lblPA") as Label).Text).Selected = true; DropDownList dlET = (DropDownList)e.Row.FindControl("dlET"); string ETquery = "select distinct ErrorType from ErrorType order by ErrorType"; SqlCommand ETcmd = new SqlCommand(ETquery); dlET.DataSource = GetData(ETcmd); dlET.DataTextField = "ErrorType"; dlET.DataValueField = "ErrorType"; dlET.DataBind(); dlET.Items.FindByValue((e.Row.FindControl("lblET") as Label).Text).Selected = true; DropDownList dlAA = (DropDownList)e.Row.FindControl("dlAA"); string AAquery = "select distinct AAA from ActualAgencyError"; SqlCommand AAcmd = new SqlCommand(AAquery); dlAA.DataSource = GetData(AAcmd); dlAA.DataTextField = "AAA"; dlAA.DataValueField = "AAA"; dlAA.DataBind(); dlAA.Items.FindByValue((e.Row.FindControl("lblAA") as Label).Text).Selected = true; DropDownList dlSupport = (DropDownList)e.Row.FindControl("dlSupport"); string SupQuery = "select distinct Support from Support"; SqlCommand Supcmd = new SqlCommand(SupQuery); dlSupport.DataSource = GetData(Supcmd); dlSupport.DataTextField = "Support"; dlSupport.DataValueField = "Support"; dlSupport.DataBind(); dlSupport.Items.FindByValue((e.Row.FindControl("lblSupport") as Label).Text).Selected = true; } } protected void UpdateSummary(object sender, GridViewUpdateEventArgs e) { MyVariables.AT = dlAnnoType.SelectedValue; //dlAnnoType.DataBind(); MyVariables.JN = dlJobName.SelectedValue; //dlJobName.DataBind(); MyVariables.ST = dlStage.SelectedValue; //dlStage.DataBind(); MyVariables.PI = gvSummary.PageIndex; //gvSummary.DataBind(); string BU = (gvSummary.Rows[e.RowIndex].FindControl("dlBU") as DropDownList).SelectedItem.Value; string ET = (gvSummary.Rows[e.RowIndex].FindControl("dlET") as DropDownList).SelectedItem.Value; string AA = (gvSummary.Rows[e.RowIndex].FindControl("dlAA") as DropDownList).SelectedItem.Value; string PA = (gvSummary.Rows[e.RowIndex].FindControl("dlPA") as DropDownList).SelectedValue; string AnnotationNumber = gvSummary.DataKeys[e.RowIndex].Value.ToString(); string sgkComments = (gvSummary.Rows[e.RowIndex].FindControl("tbsgkComm") as TextBox).Text; string Support = (gvSummary.Rows[e.RowIndex].FindControl("dlSupport") as DropDownList).SelectedValue; string SupportName = (gvSummary.Rows[e.RowIndex].FindControl("tbSupportName") as TextBox).Text; string BusImpact = (gvSummary.Rows[e.RowIndex].FindControl("tbBusImpact") as TextBox).Text; string strConnString = ConfigurationManager.ConnectionStrings["SRM_MetricConnectionString"].ConnectionString; using (SqlConnection con = new SqlConnection(strConnString)) { string query = "update vw_GridviewSource set [BusinessUnit] = @BU, [ErrorType] = @ET, [sgkComments] = @sgk, [ActualAgencyError] = @AA, [PA] = @PA, [Support] = @Support, [SupportName] = @SupportName, [BusImpact] = @BusImpact where [AnnotationNumber] = @AnnoNum"; using (SqlCommand cmd = new SqlCommand(query)) { cmd.Connection = con; cmd.Parameters.AddWithValue("@BU", BU); cmd.Parameters.AddWithValue("@AnnoNum", AnnotationNumber); cmd.Parameters.AddWithValue("@ET", ET); cmd.Parameters.AddWithValue("@AA", AA); cmd.Parameters.AddWithValue("@sgk", sgkComments); cmd.Parameters.AddWithValue("@PA", PA); cmd.Parameters.AddWithValue("@Support", Support); cmd.Parameters.AddWithValue("@SupportName", SupportName); cmd.Parameters.AddWithValue("@BusImpact", BusImpact); con.Open(); cmd.ExecuteNonQuery(); con.Close(); Response.Redirect(Request.Url.AbsoluteUri); } } gvSummary.PageIndex = MyVariables.PI; gvSummary.DataBind(); BindData(); } private void BindData() { gvSummary.PageIndex = MyVariables.PI; gvSummary.DataBind(); if (MyVariables.JN != "" &amp;&amp; MyVariables.ST != "" &amp;&amp; MyVariables.AT != "") { dlJobName.SelectedValue = MyVariables.JN; dlJobName.DataBind(); dlStage.SelectedValue = MyVariables.ST; dlStage.DataBind(); dlAnnoType.SelectedValue = MyVariables.AT; dlAnnoType.DataBind(); } gvSummary.PageIndex = MyVariables.PI; gvSummary.DataBind(); String conString = System.Configuration.ConfigurationManager.ConnectionStrings["SRM_MetricConnectionString"].ConnectionString; string query = "select [Page_ID],[AnnotationNumber],[AnnotationBy],[PA],[AnnotationType],[BusinessUnit] as Unit,[ErrorType],[ActualAgencyError],AnnotationComments,[sgkComments],[ActualAgencyError],Support,SupportName, BusImpact,Cust from vw_GridviewSource order by [Page_ID]"; SqlCommand cmd = new SqlCommand(); if (dlJobName.SelectedValue != "" &amp; dlStage.SelectedValue != "") { query = "select [Page_ID],[AnnotationNumber],[AnnotationBy],[PA],[AnnotationType],[BusinessUnit] as Unit,[ErrorType],[ActualAgencyError],AnnotationComments,[sgkComments],[ActualAgencyError],Support,SupportName, BusImpact,Cust from vw_GridviewSource where Name = '" + dlJobName.SelectedValue + "' and AnnotationDate = '" + dlStage.SelectedValue + "' order by [Page_ID]"; //cmd.Parameters.AddWithValue("@Name", dlJobName.SelectedValue); //cmd.Parameters.AddWithValue("@Stage", dlStage.SelectedValue); } if (dlAnnoType.SelectedValue != "" &amp;&amp; (dlJobName.SelectedValue.Length &lt; 2 &amp;&amp; dlStage.SelectedValue.Length &lt; 2)) { query = "select [Page_ID],[AnnotationNumber],[AnnotationBy],[PA],[AnnotationType],[BusinessUnit] as Unit,[ErrorType],[ActualAgencyError],AnnotationComments,[sgkComments],[ActualAgencyError],Support,SupportName, BusImpact,Cust from vw_GridviewSource where AnnotationType = '" + dlAnnoType.SelectedValue + "' order by [Page_ID]"; } if (dlAnnoType.SelectedValue != "" &amp;&amp; (dlJobName.SelectedValue != "" &amp;&amp; dlStage.SelectedValue != "")) { query = "select [Page_ID],[AnnotationNumber],[AnnotationBy],[PA],[AnnotationType],[BusinessUnit] as Unit,[ErrorType],[ActualAgencyError],AnnotationComments,[sgkComments],[ActualAgencyError],Support,SupportName, BusImpact,Cust from vw_GridviewSource where AnnotationType = '" + dlAnnoType.SelectedValue + "' and Name = '" + dlJobName.SelectedValue + "' and AnnotationDate = '" + dlStage.SelectedValue + "' order by [Page_ID]"; } cmd.CommandText = query; SqlConnection con = new SqlConnection(conString); cmd.CommandType = CommandType.Text; cmd.Connection = con; SqlDataAdapter sda = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); sda.Fill(ds); gvSummary.DataSource = ds; gvSummary.PageIndex = MyVariables.PI; gvSummary.DataBind(); } private DataTable GetData(SqlCommand cmd) { string strConnString = ConfigurationManager.ConnectionStrings["SRM_MetricConnectionString"].ConnectionString; using (SqlConnection con = new SqlConnection(strConnString)) { using (SqlDataAdapter sda = new SqlDataAdapter()) { cmd.Connection = con; sda.SelectCommand = cmd; using (DataTable dt = new DataTable()) { sda.Fill(dt); return dt; } } } } protected void dlJobName_SelectedIndexChanged(object sender, EventArgs e) { MyVariables.JN = dlJobName.SelectedValue; dlJobName.DataBind(); MyVariables.ST = dlStage.SelectedValue; dlStage.DataBind(); MyVariables.AT = dlAnnoType.SelectedValue; dlAnnoType.DataBind(); BindData(); MyVariables.PI = gvSummary.PageIndex; gvSummary.DataBind(); } protected void dlStage_SelectedIndexChanged(object sender, EventArgs e) { MyVariables.JN = dlJobName.SelectedValue; //dlStage.DataBind(); MyVariables.ST = dlStage.SelectedValue; MyVariables.AT = dlAnnoType.SelectedValue; gvSummary.DataBind(); BindData(); } protected void dlAnnoType_SelectedIndexChanged(object sender, EventArgs e) { MyVariables.AT = dlAnnoType.SelectedValue; MyVariables.JN = dlJobName.SelectedValue; //dlStage.DataBind(); MyVariables.ST = dlStage.SelectedValue; gvSummary.DataBind(); BindData(); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload