Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > Visual Web Developer 2005
|
Visual Web Developer 2005 Discuss creating ASP.NET 2.0 sites with Microsoft's Visual Web Developer 2005. If your question is more specific to a piece of code than the Visual tool, see the ASP.NEt 2.0 forums instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Web Developer 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 29th, 2006, 05:58 PM
Authorized User
 
Join Date: Jun 2006
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default Translating code from a DetailsView to a GridView

I am building a shopping cart and the example I am using uses a DetailsView control based on a sqlDataSource. There is a button in the details view that uses the following code to add an item to the shopping cart

This is the original code:

    Protected Sub btnAdd_Click(ByVal sender As Object, _
      ByVal e As System.EventArgs) _
      Handles btnAdd.Click
        ' get values from data source
        Dim dv As DataView
        dv = SqlDataSource1.Select( _
            DataSourceSelectArguments.Empty)
        Dim dr As DataRowView = dv(0)
        Dim ID As String = dr("ProductID")
        Dim name As String = dr("Name")
        Dim Price As Decimal
        ' get or create shopping cart
        Dim cart As ShoppingCart
        If Session("cart") Is Nothing Then
            cart = New ShoppingCart()
            Session("cart") = cart
        Else
            cart = Session("cart")
        End If

        ' add item to cart
        cart.AddItem(ID, name, Price)
End Sub

The RED portion is what is giving me a problem. I want to use a GridView control with a button on each row that will add the item to the cart.

This is the code I am using but I am not sure how to translate the RED portion. I tried a couple of things and they didn't work so I thought I would go back to square one and ask the experts here to see if anyone had any thoughts. Thanks for your help!

Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        ' get values from data source
        Dim dv As DataView
        dv = SqlDataSource1.Select( _
            DataSourceSelectArguments.Empty)
        Dim dr As DataRowView = dv(0)
        Dim username As String = Membership.GetUser.UserName
        Dim eventid As Integer = dr("eventID")
        Dim eventCode As String = dr("eventTypeCode")
        Dim eventDesc As String = dr("eventDesc")
        Dim eventfee As Decimal = dr("eventFee")
        Dim quantity As Integer = "1"
        Dim regItemType As String = dr("eventSportType")

        ' get or create shopping cart
        Dim cart As shoppingCart
        If Session("cart") Is Nothing Then
            cart = New shoppingCart()
            Session("cart") = cart
        Else
            cart = Session("cart")
        End If

        ' add item to cart
        cart.AddItem(username, eventid, eventCode, eventDesc, eventfee, quantity, regItemType)

End Sub

 
Old July 1st, 2006, 03:56 PM
Authorized User
 
Join Date: Jun 2006
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Okay...I figured out that I missed a Import System.Data at the top of the page and I got this to work except instead of adding a new item to the cart, it adds the same item over and over, and it's not even the one I've chosen! I don't have a "default value" set anywhere, but I believe it has something to do with the gridview control. I converted this code from a detailsview (which has only one item) bound to a sqldatasource to a gridview bound to a sqldatasource and I think I need something like a gridviewselected something but not sure how to do it..Thanks for your help!

rotected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs)

' get values from data source
Dim dv As DataView
dv = SqlDataSource1.Select( _
DataSourceSelectArguments.Empty)
Dim dr As Data.DataRowView = dv(0)
Dim username As String = dr("username")
Dim eventid As Integer = dr("eventID")
Dim eventCode As String = dr("eventTypeCode")
Dim eventDesc As String = dr("eventDesc")
Dim eventfee As Decimal = dr("eventFee")
Dim quantity As Integer = "1"
Dim regItemType As String = dr("eventSportType")

' get or create shopping cart
Dim cart As shoppingCart
If Session("cart") Is Nothing Then
cart = New shoppingCart()
Session("cart") = cart
Else
cart = Session("cart")
End If

' add item to cart
cart.AddItem(username, eventid, eventCode, eventDesc, eventfee, quantity, regItemType)

'redirect to cart page
username = Request.QueryString("username")
eventid = Request.QueryString("eventID")
Response.Redirect("Cart.aspx")

End Sub



Here is my GridView Code

        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="eventID"
            DataSourceID="SqlDataSource1">
            <Columns>
                <asp:BoundField DataField="eventID" HeaderText="eventID" ReadOnly="True" SortExpression="eventID"
                    Visible="False" />
                <asp:BoundField DataField="GameYear" HeaderText="GameYear" SortExpression="GameYear"
                    Visible="False" />
                <asp:BoundField DataField="eventTypeCode" HeaderText="eventTypeCode" SortExpression="eventTypeCode" />
                <asp:BoundField DataField="sportTitle" HeaderText="sportTitle" SortExpression="sportTitle"
                    Visible="False" />
                <asp:BoundField DataField="eventDesc" HeaderText="eventDesc" SortExpression="eventDesc" />
                <asp:BoundField DataField="eventStartDate" HeaderText="eventStartDate" SortExpression="eventStartDate" />
                <asp:BoundField DataField="eventFee" HeaderText="eventFee" SortExpression="eventFee" />
                <asp:BoundField DataField="eventTeamFee" HeaderText="eventTeamFee" SortExpression="eventTeamFee" />
                <asp:BoundField DataField="eventSportType" HeaderText="eventSportType" SortExpression="eventSportType"
                    Visible="False" />
                <asp:TemplateField HeaderText="Register">
                    <ItemTemplate>
                        <asp:Button ID="btnAdd" runat="server" CausesValidation="false" CommandName="Select"
                            OnClick="btnAdd_Click" Text="Register" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>


And Here is My Cart:

 <asp:GridView ID="GridView1" runat="server"
        AutoGenerateColumns="False"
        EmptyDataText="Your shopping cart is empty.">
        <Columns>
            <asp:BoundField DataField="username"
                HeaderText="UserName"
                ReadOnly="True" >
                <HeaderStyle HorizontalAlign="Left" />
            </asp:BoundField>
            <asp:BoundField DataField="eventID"
                HeaderText="EventID"
                ReadOnly="True" >
                <HeaderStyle HorizontalAlign="Left" />
            </asp:BoundField>
             <asp:BoundField DataField="eventCode"
                HeaderText="eventCode"
                ReadOnly="True" >
                <HeaderStyle HorizontalAlign="Left" />
            </asp:BoundField>
                        <asp:BoundField DataField="eventDesc"
                HeaderText="eventDesc"
                ReadOnly="True" >
                <HeaderStyle HorizontalAlign="Left" />
            </asp:BoundField>
            <asp:BoundField DataField="eventFee"
                HeaderText="eventFee"
                ReadOnly="True"
                DataFormatString="{0:c}" >
                <HeaderStyle HorizontalAlign="Left" />
            </asp:BoundField>
            <asp:BoundField DataField="Quantity"
                HeaderText="Quantity" >
                <HeaderStyle HorizontalAlign="Left" />
            </asp:BoundField>
            <asp:BoundField DataField="regItemType"
                HeaderText="regItemType" >
                <HeaderStyle HorizontalAlign="Left" />
            </asp:BoundField>
            <asp:CommandField EditText="Change"
                ShowDeleteButton="True"
                ShowEditButton="True" >
                <ItemStyle BorderStyle="None" />
                <HeaderStyle BorderStyle="None" />
            </asp:CommandField>
        </Columns>
    </asp:GridView>








Similar Threads
Thread Thread Starter Forum Replies Last Post
Using Gridview to Edit NOT DetailsView ? kalel_4444 BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 3 December 4th, 2008 11:36 PM
VB GridView & DetailsView Rich57 BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 0 February 1st, 2008 12:03 PM
Button on GridView that goes to DetailsView AndrewD ASP.NET 2.0 Basics 0 November 30th, 2005 11:09 AM
Can't get DetailsView to work with GridView oneworld95 ASP.NET 2.0 Basics 3 July 16th, 2005 07:19 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.