5 Replies - 5194 Views - Last Post: 27 April 2011 - 03:26 AM Rate Topic: -----

#1 awebb  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 08-March 09

Problem getting correct value in dropdownlist.selecteditem.tostring

Posted 22 April 2011 - 08:01 PM

I am using a LinqDataSource to fill a gridview with four columns from a products table in a SQL database. The gridview fills with the correct data for each product. I use the same LinqDataSource to populate 10 drop down list allowing users to select up to 10 products. I then add the order to an orders table and the selected products, quantities, and order number to a order details table.

The problem I am experiencing is when using the "dropdownlist.selecteditem". Most of the time I get the correct product information for the product selected. But some products consistently return incorrect data - data for another product. The incorrect data is very consistent however; meaning even though I get data for a product different than what is selected I always get the same wrong data for a given product.

Below is the asp code.
 <p>
        <asp:GridView ID="GridView1" runat="server" AllowSorting="True" 
            AutoGenerateColumns="False" DataSourceID="ldsDGVProducts" CellPadding="4" 
            ForeColor="#333333" GridLines="None">
            <AlternatingRowStyle BackColor="White" />
            <Columns>
                <asp:BoundField DataField="ProductID" HeaderText="Product ID" ReadOnly="True" 
                    SortExpression="ProductID" />
                <asp:BoundField DataField="Price" HeaderText="Price" ReadOnly="True" 
                    SortExpression="Price" DataFormatString="{0:c}" />
                <asp:BoundField DataField="ProductName" HeaderText="ProductName" 
                    ReadOnly="True" SortExpression="ProductName" />
                <asp:BoundField DataField="Type" HeaderText="Type" ReadOnly="True" 
                    SortExpression="Type" />
            </Columns>
            <EditRowStyle BackColor="#2461BF" />
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#EFF3FB" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#F5F7FB" />
            <SortedAscendingHeaderStyle BackColor="#6D95E1" />
            <SortedDescendingCellStyle BackColor="#E9EBEF" />
            <SortedDescendingHeaderStyle BackColor="#4870BE" />
        </asp:GridView>
       
        <asp:LinqDataSource ID="ldsDGVProducts" runat="server" 
            ContextTypeName="dailybread.DailyBreadDataContext" EntityTypeName="" 
            Select="new (ProductID, Price, ProductName, Type, Picture)" 
            TableName="Products" Where="Quantity &gt; @Quantity">
            <WhereParameters>
                <asp:Parameter DefaultValue="0" Name="Quantity" Type="Int16" />
            </WhereParameters>
       
        </asp:LinqDataSource>
    </p>
<p>
        <span class="style1"><strong>To place an order please fill complete the form below.
        </strong></span>
        <br />
        Customer Name:&nbsp;
        <asp:TextBox ID="txtCustomerName" runat="server" Width="207px"></asp:TextBox>
        
        <br />
        
        <br />
        <asp:Label ID="lblProduct" runat="server" Text="Product:"></asp:Label>
&nbsp;<asp:DropDownList ID="ddlProducts" runat="server" DataSourceID="ldsDGVProducts" 
            DataTextField="ProductName" DataValueField="price">
        </asp:DropDownList>
&nbsp;&nbsp;
        <asp:Label ID="lblQuantity" runat="server" Text="Quantity:"></asp:Label>
&nbsp;<asp:TextBox ID="txtQuantity" runat="server" MaxLength="2" Width="24px">0</asp:TextBox>
&nbsp;&nbsp;
        <asp:Button ID="btnAddItem" runat="server" Text="Add another Item" />
        <br />

        <asp:Button ID="btnPlaceOrder" runat="server" Text="Place Order" 
            PostBackUrl="~/Default.aspx" />
&nbsp;&nbsp;
        <asp:Button ID="btnReset" runat="server" Text="Reset Form" />
        <asp:HiddenField ID="counter" runat="server" Value="1" />
        <br />
        <br />
    </p>
</asp:Content>




This is an excerpt of the vb code behind file.

Protected Sub btnPlaceOrder_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnPlaceOrder.Click
        ' If the customer entered their name assign it to a variable and caluclate the subtotal and total based on their selection
        If Not String.IsNullOrEmpty(txtCustomerName.Text.Trim) Then
            Customer = txtCustomerName.Text
            Call calculatesubtotal()
            total = subtotal * 1.0625
            Try
                Call addorder()
                Call OrderDetails()
                MsgBox("You order has been received and will be available for pickup in 15 Minutes.", MsgBoxStyle.OkOnly, "Thank You")
                Call ResetForm()
            Catch ex As Exception
                MsgBox(ex.Message.ToString)
            End Try
        Else
            'Ask the user to enter their name if they have not already done so.
            MsgBox("A customer name is required. Please try again", MsgBoxStyle.OkOnly, "What's Your Name?")
            txtCustomerName.ReadOnly = False
        End If
    End Sub


Sub OrderDetails()
        Dim list As New ArrayList
        Dim list2 As New ArrayList
        'add items to the two arrays based on the hidden variable counter.
        Select Case counter.Value
            Case Is = 1
                list.Add(ddlProducts.SelectedItem.ToString)

                list2.Add(txtQuantity.Text.Trim)
            Case Is = 2
                list.Add(ddlProducts.SelectedItem.ToString)
                list.Add(ddlProducts0.SelectedItem.ToString)

                list2.Add(txtQuantity.Text.Trim)
                list2.Add(txtQuantity0.Text.Trim)
            Case Is = 3
                list.Add(ddlProducts.SelectedItem.ToString)
                list.Add(ddlProducts0.SelectedItem.ToString)
                list.Add(ddlProducts1.SelectedItem.ToString)

                list2.Add(txtQuantity.Text.Trim)
                list2.Add(txtQuantity0.Text.Trim)
                list2.Add(txtQuantity1.Text.Trim)
            Case Is = 4
                list.Add(ddlProducts.SelectedItem.ToString)
                list.Add(ddlProducts0.SelectedItem.ToString)
                list.Add(ddlProducts1.SelectedItem.ToString)
                list.Add(ddlProducts2.SelectedItem.ToString)

                list2.Add(txtQuantity.Text.Trim)
                list2.Add(txtQuantity0.Text.Trim)
                list2.Add(txtQuantity1.Text.Trim)
                list2.Add(txtQuantity2.Text.Trim)
            Case Is = 5
                list.Add(ddlProducts.SelectedItem.ToString)
                list.Add(ddlProducts0.SelectedItem.ToString)
                list.Add(ddlProducts1.SelectedItem.ToString)
                list.Add(ddlProducts2.SelectedItem.ToString)
                list.Add(ddlProducts3.SelectedItem.ToString)

                list2.Add(txtQuantity.Text.Trim)
                list2.Add(txtQuantity0.Text.Trim)
                list2.Add(txtQuantity1.Text.Trim)
                list2.Add(txtQuantity2.Text.Trim)
                list2.Add(txtQuantity3.Text.Trim)
            Case Is = 6
                list.Add(ddlProducts.SelectedItem.ToString)
                list.Add(ddlProducts0.SelectedItem.ToString)
                list.Add(ddlProducts1.SelectedItem.ToString)
                list.Add(ddlProducts2.SelectedItem.ToString)
                list.Add(ddlProducts3.SelectedItem.ToString)
                list.Add(ddlProducts4.SelectedItem.ToString)

                list2.Add(txtQuantity.Text.Trim)
                list2.Add(txtQuantity0.Text.Trim)
                list2.Add(txtQuantity1.Text.Trim)
                list2.Add(txtQuantity2.Text.Trim)
                list2.Add(txtQuantity3.Text.Trim)
                list2.Add(txtQuantity4.Text.Trim)
            Case Is = 7
                list.Add(ddlProducts.SelectedItem.ToString)
                list.Add(ddlProducts0.SelectedItem.ToString)
                list.Add(ddlProducts1.SelectedItem.ToString)
                list.Add(ddlProducts2.SelectedItem.ToString)
                list.Add(ddlProducts3.SelectedItem.ToString)
                list.Add(ddlProducts4.SelectedItem.ToString)
                list.Add(ddlProducts5.SelectedItem.ToString)

                list2.Add(txtQuantity.Text.Trim)
                list2.Add(txtQuantity0.Text.Trim)
                list2.Add(txtQuantity1.Text.Trim)
                list2.Add(txtQuantity2.Text.Trim)
                list2.Add(txtQuantity3.Text.Trim)
                list2.Add(txtQuantity4.Text.Trim)
                list2.Add(txtQuantity5.Text.Trim)
            Case Is = 8
                list.Add(ddlProducts.SelectedItem.ToString)
                list.Add(ddlProducts0.SelectedItem.ToString)
                list.Add(ddlProducts1.SelectedItem.ToString)
                list.Add(ddlProducts2.SelectedItem.ToString)
                list.Add(ddlProducts3.SelectedItem.ToString)
                list.Add(ddlProducts4.SelectedItem.ToString)
                list.Add(ddlProducts5.SelectedItem.ToString)
                list.Add(ddlProducts6.SelectedItem.ToString)

                list2.Add(txtQuantity.Text.Trim)
                list2.Add(txtQuantity0.Text.Trim)
                list2.Add(txtQuantity1.Text.Trim)
                list2.Add(txtQuantity2.Text.Trim)
                list2.Add(txtQuantity3.Text.Trim)
                list2.Add(txtQuantity4.Text.Trim)
                list2.Add(txtQuantity5.Text.Trim)
                list2.Add(txtQuantity6.Text.Trim)
            Case Is = 9
                list.Add(ddlProducts.SelectedItem.ToString)
                list.Add(ddlProducts0.SelectedItem.ToString)
                list.Add(ddlProducts1.SelectedItem.ToString)
                list.Add(ddlProducts2.SelectedItem.ToString)
                list.Add(ddlProducts3.SelectedItem.ToString)
                list.Add(ddlProducts4.SelectedItem.ToString)
                list.Add(ddlProducts5.SelectedItem.ToString)
                list.Add(ddlProducts6.SelectedItem.ToString)
                list.Add(ddlProducts7.SelectedItem.ToString)

                list2.Add(txtQuantity.Text.Trim)
                list2.Add(txtQuantity0.Text.Trim)
                list2.Add(txtQuantity1.Text.Trim)
                list2.Add(txtQuantity2.Text.Trim)
                list2.Add(txtQuantity3.Text.Trim)
                list2.Add(txtQuantity4.Text.Trim)
                list2.Add(txtQuantity5.Text.Trim)
                list2.Add(txtQuantity6.Text.Trim)
                list2.Add(txtQuantity7.Text.Trim)
            Case Is = 10
                list.Add(ddlProducts.SelectedItem.ToString)
                list.Add(ddlProducts0.SelectedItem.ToString)
                list.Add(ddlProducts1.SelectedItem.ToString)
                list.Add(ddlProducts2.SelectedItem.ToString)
                list.Add(ddlProducts3.SelectedItem.ToString)
                list.Add(ddlProducts4.SelectedItem.ToString)
                list.Add(ddlProducts5.SelectedItem.ToString)
                list.Add(ddlProducts6.SelectedItem.ToString)
                list.Add(ddlProducts7.SelectedItem.ToString)
                list.Add(ddlProducts8.SelectedItem.ToString)

                list2.Add(txtQuantity.Text.Trim)
                list2.Add(txtQuantity0.Text.Trim)
                list2.Add(txtQuantity1.Text.Trim)
                list2.Add(txtQuantity2.Text.Trim)
                list2.Add(txtQuantity3.Text.Trim)
                list2.Add(txtQuantity4.Text.Trim)
                list2.Add(txtQuantity5.Text.Trim)
                list2.Add(txtQuantity6.Text.Trim)
                list2.Add(txtQuantity7.Text.Trim)
                list2.Add(txtQuantity8.Text.Trim)
        End Select

        Dim connection As String = "Data Source=ITGL6060\WEBBSQL;Initial Catalog=DailyBread;Integrated Security=True"
        Dim sql = "select MAX(OrderID) as lastEntry from Orders" 'gets max order Id from Orders
        Dim con As New SqlConnection(connection)
        con.Open()
        Dim cmd As New SqlCommand(sql, con)
        Dim reader As SqlDataReader = cmd.ExecuteReader()

        While reader.Read()
            For i = 0 To list.Count - 1
                Dim sql2 = "select ProductID from Products where ProductName = @ProductName" 'gets ProductID
                Dim con2 As New SqlConnection(connection)
                con2.Open()
                Dim cmd2 As New SqlCommand(sql2, con2)
                cmd2.Parameters.Add("@ProductName", SqlDbType.VarChar).Value = list.Item(i)
                Dim reader2 As SqlDataReader = cmd2.ExecuteReader()

                While reader2.Read()
                    If list2.Item(i) > 0 Then
                        Dim sql3 = "insert into OrderDetails (OrderID, ProductID, Quantity) values (@OrderID, @ProductID, @Quantity)" ' inserts data into order details
                        Dim con3 As New SqlConnection(connection)
                        con3.Open()
                        Dim cmd3 As New SqlCommand(sql3, con3)
                        cmd3.Parameters.Add("@OrderID", SqlDbType.Int).Value = CInt(reader("lastEntry"))
                        cmd3.Parameters.Add("@ProductID", SqlDbType.Int).Value = CInt(reader2("ProductID"))
                        cmd3.Parameters.Add("@Quantity", SqlDbType.Int).Value = CInt(list2.Item(i))
                        cmd3.ExecuteNonQuery()
                        cmd3.Dispose()
                        con3.Close()
                    End If
                End While
                reader2.Close()
                cmd2.Dispose()
                con2.Close()
            Next
        End While
        reader.Close()
        cmd.Dispose()
        con.Close()

    End Sub



Any thoughts or suggestions would be greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Problem getting correct value in dropdownlist.selecteditem.tostring

#2 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 446
  • View blog
  • Posts: 1,501
  • Joined: 28-April 09

Re: Problem getting correct value in dropdownlist.selecteditem.tostring

Posted 22 April 2011 - 10:54 PM

have you ran the debugger and stepped it through the process to see what it's doing? I only see one drop down in the aspx code you pasted so it's kind of hard to know what's going on with the rest of them. Have you tried using SelectedValue instead of SelectedItem.ToString? If you've stepped it through with the debugger do the arraylists seem to contain all of the correct data? I'm not real familiar with VB.Net but nothing jumps out at me as an obvious culprit in the code you've posted so far.
Was This Post Helpful? 1
  • +
  • -

#3 awebb  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 08-March 09

Re: Problem getting correct value in dropdownlist.selecteditem.tostring

Posted 24 April 2011 - 07:25 AM

View PostNakor, on 23 April 2011 - 06:54 AM, said:

have you ran the debugger and stepped it through the process to see what it's doing? I only see one drop down in the aspx code you pasted so it's kind of hard to know what's going on with the rest of them. Have you tried using SelectedValue instead of SelectedItem.ToString? If you've stepped it through with the debugger do the arraylists seem to contain all of the correct data? I'm not real familiar with VB.Net but nothing jumps out at me as an obvious culprit in the code you've posted so far.




First of all, thanks for taking the time to read and reply. When I step through it with the debugger there are no errors and most products work correctly. But approximately 1/3 of them will return data for a different product than what is really selected. I just posted code for one procedure but I use the selecteditem.tostring in several places and it gets the same wrong value every place that I call it and the exact same wrong data every time the program is run. I store product name in DatasTextField(selecteditem) and the product price in DataValueField(selectedvalue). If I use selectedvalue it gets the wrong data just the same as using selecteditem. I can't figure out why certain items work perfectly and certain ones return data for a different item(row in database). Googling continues.... Thanks again.
Was This Post Helpful? 0
  • +
  • -

#4 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 446
  • View blog
  • Posts: 1,501
  • Joined: 28-April 09

Re: Problem getting correct value in dropdownlist.selecteditem.tostring

Posted 24 April 2011 - 12:10 PM

Do all of the dropdownlists contain the right data in them? Do any of the dropdowns contain the same text? Since you're pulling data from the databased based on the name of the product if you have products with the same name in more than one drop down then you will end up retrieving the same data for both items. Really hard to say though without being able to really see how it's running myself.
Was This Post Helpful? 0
  • +
  • -

#5 awebb  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 08-March 09

Re: Problem getting correct value in dropdownlist.selecteditem.tostring

Posted 24 April 2011 - 07:50 PM

@Nakor. The dropdowns do contain the correct data. Your first reply appears to be leading in the right direction of the problem. I wrote down every item in the dropdown that returns incorrect results and there is of course a pattern. If two items have the same price, the first item in the database table with the price of the selected item is the data that is returned. The selectedvalue of the dropdownlist holds the price data.

The item that is actually selected in the dropdown list changes when add items buttons is clicked. So I guess that means the selecteditem.tostring is returning the proper data but I need to figure out why the selected item changes to the first item with the same price as the item I really select.

If I leave the datavaluefield (selectedvalue) blank and only utilize the selected item property it works correctly. However, I use the price in calculating a subtotal so I am going to try and find a way to leave price in the selectedvalue field and still return the proper results. Thanks for your help. I'll update the thread if I find a solution just in case you or anyone else find it beneficial.
Was This Post Helpful? 0
  • +
  • -

#6 marinus  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 135
  • View blog
  • Posts: 575
  • Joined: 14-April 10

Re: Problem getting correct value in dropdownlist.selecteditem.tostring

Posted 27 April 2011 - 03:26 AM

Have you tried

SelectedItem.Text


or

SelectedItem.Value

This post has been edited by marinus: 27 April 2011 - 03:27 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1