locked
'ddBatch' has a SelectedValue which is invalid because it does not exist in the list of items. RRS feed

  • Question

  • User709978785 posted

    How can I prevent this error? It only happens if there are no batches for the date range. If there are no batched the drop down could just be blank, until a new date range is provided.

    'ddBatch' has a SelectedValue which is invalid because it does not exist in the list of items.
    Parameter name: value

    Heres my code that binds the drop down:

    Sub BindBatchDropDown()
            Dim Con As SqlConnection
            Dim dtrBatch As SqlDataReader
            Dim strBatch As String
    
            Dim ConnectStr As String = _
            ConfigurationManager.ConnectionStrings("SQL2ConnectionString").ConnectionString
            Con = New SqlConnection(ConnectStr)
            Con.Open()
            strBatch = "Select Batch_ID,Batch_Posted,Batch_Type FROM tblBatch_number WHERE Batch_Posted Between @BatchStart AND @BatchEnd Order By Batch_Posted DESC"
            MyCommand = New SqlCommand(strBatch, Con)
            MyCommand.Parameters.Add("@BatchStart", SqlDbType.VarChar, 80).Value = System.DateTime.Today.ToShortDateString()
            MyCommand.Parameters.Add("@BatchEnd", SqlDbType.VarChar, 80).Value = System.DateTime.Today.ToShortDateString()
            dtrBatch = MyCommand.ExecuteReader()
    
            ddBatch.DataSource = dtrBatch
            ddBatch.DataValueField = "Batch_ID"
            ddBatch.DataTextField = "Batch_Posted"
            ddBatch.DataBind()
            ddBatch.Items.Insert(0, "")
            dtrBatch.Close()
            Con.Close()
        End Sub
    
     
    Tuesday, January 1, 2008 2:11 PM

Answers

  • User1268090313 posted

    Hi Jackxxx,

    I have tested your case as the following code snippet, it works well in my sample,
    the null string was be inserted into the DropDownlist as selected value.

            ArrayList ar1 = new ArrayList();  // bind some data
            ar1.Add("item1");
            ar1.Add("item2");
            ar1 = null;
            ddBatch.DataSource = ar1;
            ddBatch.DataBind();
            ddBatch.Items.Insert(0, "");
           
        OR
            ArrayList ar1 = null; //bind to null data source
            ddBatch.DataSource = ar1;
            ddBatch.DataBind();
            ddBatch.Items.Insert(0, "");
           
    perhaps you should try to debug your code, please insert a breakpoint in your code and step into it, and then
    you will find where the bug is. If you still cannot figure out the bug, I think you should post the detailed code
    and information here for troubleshooting.

    Regards,
    Xun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 3, 2008 4:16 AM

All replies

  • User-558565241 posted

    "ddBatch' has a SelectedValue which is invalid because it does not exist in the list of items.
    Parameter name: value"

    Not sure we have enough information here, where are you getting this error message from, a stack trace? Are you sure it's coming from this part of your code or somewhere else?

    If it is for sure coming from this part of your code try clearing the items in the drop down in the first line like ddBatch.Items.Clear() before you bind.

    Tuesday, January 1, 2008 4:34 PM
  • User558493921 posted

    The problem is ""(Null Value) Plz check out

    Thank u

    Baba

    Please remember to click "Mark as Answer" on this post if it helped you. 

    Tuesday, January 1, 2008 10:56 PM
  • User709978785 posted

    All I'm trying to do here is add a blank item as the selected value so the sqldatasource is passed a null to return all rows. The Batch_ID is the primary key and is an Identity int. Does this make a difference?

    Sub BindBatchDropDown()
            Dim Con As SqlConnection
            Dim dtrBatch As SqlDataReader
            Dim strBatch As String
    
            Dim ConnectStr As String = _
            ConfigurationManager.ConnectionStrings("SQL2ConnectionString").ConnectionString
            Con = New SqlConnection(ConnectStr)
            Con.Open()
            strBatch = "Select Batch_ID FROM tblBatch_number"
            MyCommand = New SqlCommand(strBatch, Con)
            dtrBatch = MyCommand.ExecuteReader()
    
            ddBatch.DataSource = dtrBatch
            ddBatch.DataValueField = "Batch_ID"
            ddBatch.DataTextField = "Batch_ID"
            ddBatch.DataBind()
            ddBatch.Items.Insert(0, "")
            dtrBatch.Close()
            Con.Close()
        End Sub
    
     
    Wednesday, January 2, 2008 7:10 PM
  • User-558565241 posted

    Oh, OK I might be getting closer now to understanding your issue.

    I'm not good at vb so I'll give you the sql expressions you'll need

     If batch id is empty ==> SELECT Batch_ID FROM tblBatch_number

    If batch Id has a value ==> SELECT Batch_ID FROM tblBatch_number WHERE Batch_ID = batchID

    Where BatchID comes from your selected value. 

    Note that you can't pass a null or empty value into your TSQL from .NET that I'm aware of. If you must just do so you should create a stored procedure where it would be very easy set your input parameter value to null.

     

    Wednesday, January 2, 2008 7:55 PM
  • User709978785 posted

    I'm not sure you are understanding what I need.

    All I want is a blank row at the top of my drop down when it is populated with the Batch_ID's, so the SQLDatasource is passed a null.

           ddBatch.Items.Insert(0, "")

    Wednesday, January 2, 2008 8:15 PM
  • User-558565241 posted

    yes, you can't pass your Sql datasource a null, you could make the value zero ("0") instead, and trap if the user selected "0" to alter your select query accordingly.

    ddBatch.Items.Insert(0,new ListItem("","0"))

    in C#

    Wednesday, January 2, 2008 8:37 PM
  • User1268090313 posted

    Hi Jackxxx,

    I have tested your case as the following code snippet, it works well in my sample,
    the null string was be inserted into the DropDownlist as selected value.

            ArrayList ar1 = new ArrayList();  // bind some data
            ar1.Add("item1");
            ar1.Add("item2");
            ar1 = null;
            ddBatch.DataSource = ar1;
            ddBatch.DataBind();
            ddBatch.Items.Insert(0, "");
           
        OR
            ArrayList ar1 = null; //bind to null data source
            ddBatch.DataSource = ar1;
            ddBatch.DataBind();
            ddBatch.Items.Insert(0, "");
           
    perhaps you should try to debug your code, please insert a breakpoint in your code and step into it, and then
    you will find where the bug is. If you still cannot figure out the bug, I think you should post the detailed code
    and information here for troubleshooting.

    Regards,
    Xun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 3, 2008 4:16 AM