nested drop down

M

Me LK

I have a set of drop downs nested in a datagrid. Each drop down is a
size or color for an item. Each row of the grid has a button which
leads to a cart. When a user selects something from the first row or
nested drop downs the page refreshes, the correct item is selected in
the list and the and upon clicking the add to cart button the correct
choices show up in the database. When a person selects something from
a row other then the first row of the grid the page refreshes, the
correct item shows in the list but when the item is added to the
database the data from the top row of the grid is added.


I would love some help on this one.


Code is as follows

If Not IsPostBack Then

' Data for products
--------------------------------------------------

Dim connection As New SqlConnection(connectionString)
'Create and Initialize the command Object
Dim command As New SqlCommand("new_GetChildDetail",
connection)
command.CommandType = CommandType.StoredProcedure

'Add an Input Parameter
command.Parameters.Add("@ProductCode", SqlDbType.NVarChar,
50)
command.Parameters("@ProductCode").Value = ProductCode



Dim productDetail As New SqlDataAdapter
productDetail.SelectCommand = command

Dim dsProductDetail As New DataSet
productDetail.Fill(dsProductDetail)


' ProductList.DataSource = dsProductDetail
' ProductList.DataBind()
'Data for Colors---------------------------------------------------

Dim connection2 As New SqlConnection(connectionString)
'Create and Initialize the command Object
Dim command2 As New SqlCommand("spGetProductColor2",
connection2)
command2.CommandType = CommandType.StoredProcedure

'Add an Input Parameter
command2.Parameters.Add("@StrProductCode",
SqlDbType.NVarChar, 50)
command2.Parameters("@StrProductCode").Value =
strProductCode

Dim ColorDetail As New SqlDataAdapter
ColorDetail.SelectCommand = command2

Dim dsColorDetail As New DataSet
ColorDetail.Fill(dsColorDetail)

(duplicate coding for size omited)

'relationships--------------------------------
Dim datarelation As New DataSet
ColorDetail.Fill(datarelation, "colors")
productDetail.Fill(datarelation, "products")
sizedetail.Fill(datarelation, "sizes")

datarelation.Relations.Add("ProdToColors",
datarelation.Tables("products").Columns("intProductID"),
datarelation.Tables("colors").Columns("intProductID"))
datarelation.Relations.Add("ProdTosizes",
datarelation.Tables("products").Columns("intProductID"),
datarelation.Tables("sizes").Columns("intProductID"))
Cache.Insert("datarelation", datarelation)


itemInfo.DataSource = datarelation.Tables("products")
itemInfo.DataBind()

End If

Protected Sub itemInfo_itemCommand(ByVal source As Object, ByVal e As
System.web.UI.WebControls.DataGridCommandEventArgs) Handles
itemInfo.ItemCommand



'
COLOR--------------------------------------------------------

Dim Color
Dim ddlColors As DropDownList
ddlColors = (itemInfo.Items(0).FindControl("ddlColors"))

If Not ddlColors Is Nothing Then

Color = ddlColors.SelectedItem.Value.ToString

Else
Color = "NA"

End If
'PRODUCTS----------------------------------------------------------------------

Dim intProductID As Integer = e.CommandArgument


'Add the product to the shopping cart
ShoppingCart.AddProduct(intProductID, Psize, Color)

Dim redirect = "shoppingcart.aspx"
Response.Redirect(redirect)


End Sub


_______________________________________________________________________________
Code for grid

<asp:datagrid id="itemInfo" runat="server" AutoGenerateColumns="False" <Columns>
<asp:BoundColumn DataField="strProductName" HeaderText="Item Name"> </
asp:BoundColumn>
<asp:TemplateColumn>
<ItemTemplate>
<asp:DropDownList Width="150"
AutoPostBack="True" id="ddlcolors" runat="server" datasource='<%#
Container.DataItem.CreateChildView("ProdToColors") %>'
datatextfield="strColorID" datavaluefield="strColorID"> </
asp:DropDownList>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<ItemTemplate>
<asp:LinkButton id="linkButton" CommandName='<
%#databinder.eval(Container.dataitem, "intProductID") %>'
CommandArgument='<%#databinder.eval(Container.dataitem,
"intProductID") %>' Text='<%#databinder.eval(Container.dataitem,
"intProductID") %>' Runat="server"> </asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>
 
M

Me LK

I narrowed down the error. Now I just need to know how to fix it.

Dim Psize
Dim ddlPsize As DropDownList
ddlPsize = (itemInfo.Items(0).FindControl("ddlsizes"))


It is choosing the first row becuase of the above code.

I tried ddlPsize =
(itemInfo.Items(itemInfo.SelectedIndex).FindControl("ddlsizes"))

But I got this error
Index was out of range. Must be non-negative and less than the size of
the collection. Parameter name: index



Can anyone help me at this point?
 
L

LVP

check what is the SelectedIndex Value is First
If nothing is selected then it is -1 ( negative one )

Then Move on
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top