i have 2 buttons in a shopping cart which is a datagrid...I have one column called price. I also have 2 buttons on each row od the datagrid. I want to be able to click on one button and that will add the value of the item in that row to the total. I also
want to click on the other button and that would decrease the total.
Here is my code with what i thought i had to add from yours getting a compilation error
BC30205: End of statement expected.
Source Error:
Line 35:
Line 36:
Line 37: protected void gdvproducts_RowCommand(object sender, GridViewCommandEventArgs e) Line 38: {
Line 39: int index = Convert.ToInt32(e.CommandArgument);
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Dim strDatabaseNameAndLocation As String
strDatabaseNameAndLocation = Server.MapPath("shop.mdb")
Dim strSQLCommand As String
strSQLCommand = "SELECT Product.* FROM Product ORDER BY Product.productID ASC;"
Dim objOleDbConnection As System.Data.OleDb.OleDbConnection
objOleDbConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0; Data Source=" & strDatabaseNameAndLocation)
objOleDbConnection .Open()
Dim objOleDbCommand As System.Data.OleDb.OleDbCommand
objOleDbCommand = New System.Data.OleDb.OleDbCommand(strSQLCommand, objOleDbConnection)
Dim objOleDbDataReader As System.Data.OleDb.OleDbDataReader
objOleDbDataReader = objOleDbCommand.ExecuteReader()
gdvproducts.DataSource = objOleDbDataReader
Dim datDataTable As System.Data.DataTable
datDataTable = New System.Data.DataTable()
datDataTable.Load(objOleDbDataReader)
gdvproducts.DataSource = datDataTable
gdvproducts.DataBind()
objOleDbConnection .Close()
Dim inttotal As string
inttotal = 0
lbltotal.Text = inttotal
End Sub
protected void gdvproducts_RowCommand(object sender, GridViewCommandEventArgs e)
{
int index = Convert.ToInt32(e.CommandArgument);
switch (e.CommandName)
{
case "Add":
this.lblTotal.Text = (Convert.ToDouble(this.lblTotal.Text) + Convert.ToDouble(this.gdvproducts.Rows[index].Cells[2].Text)).ToString();
break;
case "Remove":
this.lblTotal.Text = (Convert.ToDouble(this.lblTotal.Text) - Convert.ToDouble(this.gdvproducts.Rows[index].Cells[2].Text)).ToString();
break;
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Supplies Shop</title>
</head>
<body>
<asp:Image ID="imglogo" ImageUrl = "images/shoplogo.jpg" runat="server"></asp:Image>
<form id="webform" runat="server">
<div>
<asp:GridView ID="gdvproducts" runat="server" AutoGenerateColumns="False"
DataKeyNames="productID" onrowcommand="gdvproducts_RowCommand" >
<Columns>
<asp:BoundField DataField="productID" HeaderText="Product ID"
InsertVisible="False" ReadOnly="True" SortExpression="productID" />
<asp:BoundField DataField="productName" HeaderText="Name"
SortExpression="productName" />
<asp:BoundField DataField="productPrice" HeaderText="Price"
SortExpression="productPrice" />
<asp:TemplateField HeaderText="Add">
<ItemTemplate>
<asp:Button id="btnAdd" runat="server" postbackurl="cart.aspx" text="add" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Remove">
<ItemTemplate>
<asp:Button id="btnRemove" runat="server" postbackurl="cart.aspx" text="remove" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<p>
<asp:Label ID="lbltotal" runat="server"></asp:Label></p>
</div>
</form>
</body>
</html>
hi, the code i have posted earlier is in C#, the below code for vb.net so refer below code
Protected Sub Page_Load(sender As Object, e As EventArgs)
If Not Page.IsPostBack Then
Me.BindGrid()
End If
End Sub
Private Sub BindGrid()
Dim dt As New DataTable()
dt.Columns.Add("Price")
dt.Rows.Add(New [Object]() {"3.19"})
dt.Rows.Add(New [Object]() {"3.56"})
dt.Rows.Add(New [Object]() {"4.25"})
dt.Rows.Add(New [Object]() {"4.89"})
Me.GridView1.DataSource = dt
Me.GridView1.DataBind()
Me.GridView2.DataSource = dt
Me.GridView2.DataBind()
End Sub
Protected Sub GridView1_RowCommand(sender As Object, e As GridViewCommandEventArgs)
Dim index As Integer = Convert.ToInt32(e.CommandArgument)
Select Case e.CommandName
Case "Add"
Me.lblTotal.Text = (Convert.ToDouble(Me.lblTotal.Text) + Convert.ToDouble(Me.GridView1.Rows(index).Cells(0).Text)).ToString()
Exit Select
Case "Remove"
Me.lblTotal.Text = (Convert.ToDouble(Me.lblTotal.Text) - Convert.ToDouble(Me.GridView1.Rows(index).Cells(0).Text)).ToString()
Exit Select
End Select
End Sub
Protected Sub GridView2_RowCommand(sender As Object, e As GridViewCommandEventArgs)
Dim index As Integer = Convert.ToInt32(e.CommandArgument)
Select Case e.CommandName
Case "Add"
Me.lblTotal2.Text = (Convert.ToDouble(Me.lblTotal2.Text) + Convert.ToDouble(TryCast(Me.GridView2.Rows(index).FindControl("lblPrice"), Label).Text)).ToString()
Exit Select
Case "Remove"
Me.lblTotal2.Text = (Convert.ToDouble(Me.lblTotal2.Text) - Convert.ToDouble(TryCast(Me.GridView2.Rows(index).FindControl("lblPrice"), Label).Text)).ToString()
Exit Select
End Select
End Sub
Hi
Thanks for your replies and I feel really stupid for not realising that was c#, it now compiles but nothing appearing in the total label when i press the add button!! Code below
Any suggestions?
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Dim strDatabaseNameAndLocation As String
strDatabaseNameAndLocation = Server.MapPath("shop.mdb")
Dim strSQLCommand As String
strSQLCommand = "SELECT Product.* FROM Product ORDER BY Product.productID ASC;"
Dim objOleDbConnection As System.Data.OleDb.OleDbConnection
objOleDbConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0; Data Source=" & strDatabaseNameAndLocation)
objOleDbConnection .Open()
Dim objOleDbCommand As System.Data.OleDb.OleDbCommand
objOleDbCommand = New System.Data.OleDb.OleDbCommand(strSQLCommand, objOleDbConnection)
Dim objOleDbDataReader As System.Data.OleDb.OleDbDataReader
objOleDbDataReader = objOleDbCommand.ExecuteReader()
gdvproducts.DataSource = objOleDbDataReader
Dim datDataTable As System.Data.DataTable
datDataTable = New System.Data.DataTable()
datDataTable.Load(objOleDbDataReader)
gdvproducts.DataSource = datDataTable
gdvproducts.DataBind()
objOleDbConnection .Close()
'Dim inttotal As string
'inttotal = 0
'lblTotal.Text = inttotal
End Sub
Protected Sub gdvproducts_RowCommand(sender As Object, e As GridViewCommandEventArgs)
Dim index As Integer = Convert.ToInt32(e.CommandArgument)
Select Case e.CommandName
Case "Add"
Me.lblTotal.Text = (Convert.ToDouble(Me.lblTotal.Text) + Convert.ToDouble(Me.gdvproducts.Rows(index).Cells(2).Text)).ToString()
Exit Select
Case "Remove"
Me.lblTotal.Text = (Convert.ToDouble(Me.lblTotal.Text) - Convert.ToDouble(Me.gdvproducts.Rows(index).Cells(2).Text)).ToString()
Exit Select
End Select
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Supplies Shop</title>
</head>
<body>
<asp:Image ID="imglogo" ImageUrl = "images/shoplogo.jpg" runat="server"></asp:Image>
<form id="webform" runat="server">
<div>
<asp:GridView ID="gdvproducts" runat="server" AutoGenerateColumns="False"
DataKeyNames="productID" onrowcommand="gdvproducts_RowCommand" >
<Columns>
<asp:BoundField DataField="productID" HeaderText="Product ID"
InsertVisible="False" ReadOnly="True" SortExpression="productID" />
<asp:BoundField DataField="productName" HeaderText="Name"
SortExpression="productName" />
<asp:BoundField DataField="productPrice" HeaderText="Price"
SortExpression="productPrice" />
<asp:TemplateField HeaderText="Add">
<ItemTemplate>
<asp:Button id="btnAdd" runat="server" postbackurl="cart.aspx" text="add" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Remove">
<ItemTemplate>
<asp:Button id="btnRemove" runat="server" postbackurl="cart.aspx" text="remove" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<p>
<asp:Label ID="lblTotal" runat="server" Text = "0"></asp:Label></p>
</div>
</form>
</body>
</html>
hi, first remove the postbackurl="cart.aspx" from both the buttons, and you did not give CommandName and CommandArgument in Add & Remove buttons , cleraly go thru my code and use
bouncyger
Member
8 Points
68 Posts
updating a shopping cart
May 03, 2012 09:42 PM|LINK
i have 2 buttons in a shopping cart which is a datagrid...I have one column called price. I also have 2 buttons on each row od the datagrid. I want to be able to click on one button and that will add the value of the item in that row to the total. I also want to click on the other button and that would decrease the total.
Total is in a label below the datagrid.
Any help appreciated.
karthicks
All-Star
31334 Points
5414 Posts
Re: updating a shopping cart
May 04, 2012 06:47 AM|LINK
hi, here i have given two examples, whichever suits for you you can use
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" onrowcommand="GridView1_RowCommand"> <Columns> <asp:BoundField HeaderText="Price" DataField="Price" /> <asp:ButtonField HeaderText="Add" ButtonType="Button" Text="Add" CommandName="Add" /> <asp:ButtonField HeaderText="Remove" ButtonType="Button" Text="Remove" CommandName="Remove" /> </Columns> </asp:GridView> <asp:Label ID="lblTotal" runat="server" Text="0"></asp:Label> <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false" onrowcommand="GridView2_RowCommand"> <Columns> <asp:TemplateField HeaderText="Price"> <ItemTemplate> <asp:Label ID="lblPrice" runat="server" Text='<%# Eval("Price") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Add"> <ItemTemplate> <asp:Button ID="btnAdd" runat="server" Text="Add" CommandName="Add" CommandArgument='<%# Container.DataItemIndex %>' /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Remove"> <ItemTemplate> <asp:Button ID="btnRemove" runat="server" Text="Remove" CommandName="Remove" CommandArgument='<%# Container.DataItemIndex %>' /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <asp:Label ID="lblTotal2" runat="server" Text="0"></asp:Label> protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) this.BindGrid(); } private void BindGrid() { DataTable dt = new DataTable(); dt.Columns.Add("Price"); dt.Rows.Add(new Object[] { "3.19" }); dt.Rows.Add(new Object[] { "3.56" }); dt.Rows.Add(new Object[] { "4.25" }); dt.Rows.Add(new Object[] { "4.89" }); this.GridView1.DataSource = dt; this.GridView1.DataBind(); this.GridView2.DataSource = dt; this.GridView2.DataBind(); } protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { int index = Convert.ToInt32(e.CommandArgument); switch (e.CommandName) { case "Add": this.lblTotal.Text = (Convert.ToDouble(this.lblTotal.Text) + Convert.ToDouble(this.GridView1.Rows[index].Cells[0].Text)).ToString(); break; case "Remove": this.lblTotal.Text = (Convert.ToDouble(this.lblTotal.Text) - Convert.ToDouble(this.GridView1.Rows[index].Cells[0].Text)).ToString(); break; } } protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e) { int index = Convert.ToInt32(e.CommandArgument); switch (e.CommandName) { case "Add": this.lblTotal2.Text = (Convert.ToDouble(this.lblTotal2.Text) + Convert.ToDouble((this.GridView2.Rows[index].FindControl("lblPrice") as Label).Text)).ToString(); break; case "Remove": this.lblTotal2.Text = (Convert.ToDouble(this.lblTotal2.Text) - Convert.ToDouble((this.GridView2.Rows[index].FindControl("lblPrice") as Label).Text)).ToString(); break; } }Karthick S
bouncyger
Member
8 Points
68 Posts
Re: updating a shopping cart
May 04, 2012 08:56 AM|LINK
Line 35: Line 36: Line 37: protected void gdvproducts_RowCommand(object sender, GridViewCommandEventArgs e) Line 38: { Line 39: int index = Convert.ToInt32(e.CommandArgument);<%@ Page Language="VB" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Dim strDatabaseNameAndLocation As String strDatabaseNameAndLocation = Server.MapPath("shop.mdb") Dim strSQLCommand As String strSQLCommand = "SELECT Product.* FROM Product ORDER BY Product.productID ASC;" Dim objOleDbConnection As System.Data.OleDb.OleDbConnection objOleDbConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0; Data Source=" & strDatabaseNameAndLocation) objOleDbConnection .Open() Dim objOleDbCommand As System.Data.OleDb.OleDbCommand objOleDbCommand = New System.Data.OleDb.OleDbCommand(strSQLCommand, objOleDbConnection) Dim objOleDbDataReader As System.Data.OleDb.OleDbDataReader objOleDbDataReader = objOleDbCommand.ExecuteReader() gdvproducts.DataSource = objOleDbDataReader Dim datDataTable As System.Data.DataTable datDataTable = New System.Data.DataTable() datDataTable.Load(objOleDbDataReader) gdvproducts.DataSource = datDataTable gdvproducts.DataBind() objOleDbConnection .Close() Dim inttotal As string inttotal = 0 lbltotal.Text = inttotal End Sub protected void gdvproducts_RowCommand(object sender, GridViewCommandEventArgs e) { int index = Convert.ToInt32(e.CommandArgument); switch (e.CommandName) { case "Add": this.lblTotal.Text = (Convert.ToDouble(this.lblTotal.Text) + Convert.ToDouble(this.gdvproducts.Rows[index].Cells[2].Text)).ToString(); break; case "Remove": this.lblTotal.Text = (Convert.ToDouble(this.lblTotal.Text) - Convert.ToDouble(this.gdvproducts.Rows[index].Cells[2].Text)).ToString(); break; } } </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Supplies Shop</title> </head> <body> <asp:Image ID="imglogo" ImageUrl = "images/shoplogo.jpg" runat="server"></asp:Image> <form id="webform" runat="server"> <div> <asp:GridView ID="gdvproducts" runat="server" AutoGenerateColumns="False" DataKeyNames="productID" onrowcommand="gdvproducts_RowCommand" > <Columns> <asp:BoundField DataField="productID" HeaderText="Product ID" InsertVisible="False" ReadOnly="True" SortExpression="productID" /> <asp:BoundField DataField="productName" HeaderText="Name" SortExpression="productName" /> <asp:BoundField DataField="productPrice" HeaderText="Price" SortExpression="productPrice" /> <asp:TemplateField HeaderText="Add"> <ItemTemplate> <asp:Button id="btnAdd" runat="server" postbackurl="cart.aspx" text="add" /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Remove"> <ItemTemplate> <asp:Button id="btnRemove" runat="server" postbackurl="cart.aspx" text="remove" /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <p> <asp:Label ID="lbltotal" runat="server"></asp:Label></p> </div> </form> </body> </html>karthicks
All-Star
31334 Points
5414 Posts
Re: updating a shopping cart
May 04, 2012 09:13 AM|LINK
hi, the code i have posted earlier is in C#, the below code for vb.net so refer below code
Protected Sub Page_Load(sender As Object, e As EventArgs) If Not Page.IsPostBack Then Me.BindGrid() End If End Sub Private Sub BindGrid() Dim dt As New DataTable() dt.Columns.Add("Price") dt.Rows.Add(New [Object]() {"3.19"}) dt.Rows.Add(New [Object]() {"3.56"}) dt.Rows.Add(New [Object]() {"4.25"}) dt.Rows.Add(New [Object]() {"4.89"}) Me.GridView1.DataSource = dt Me.GridView1.DataBind() Me.GridView2.DataSource = dt Me.GridView2.DataBind() End Sub Protected Sub GridView1_RowCommand(sender As Object, e As GridViewCommandEventArgs) Dim index As Integer = Convert.ToInt32(e.CommandArgument) Select Case e.CommandName Case "Add" Me.lblTotal.Text = (Convert.ToDouble(Me.lblTotal.Text) + Convert.ToDouble(Me.GridView1.Rows(index).Cells(0).Text)).ToString() Exit Select Case "Remove" Me.lblTotal.Text = (Convert.ToDouble(Me.lblTotal.Text) - Convert.ToDouble(Me.GridView1.Rows(index).Cells(0).Text)).ToString() Exit Select End Select End Sub Protected Sub GridView2_RowCommand(sender As Object, e As GridViewCommandEventArgs) Dim index As Integer = Convert.ToInt32(e.CommandArgument) Select Case e.CommandName Case "Add" Me.lblTotal2.Text = (Convert.ToDouble(Me.lblTotal2.Text) + Convert.ToDouble(TryCast(Me.GridView2.Rows(index).FindControl("lblPrice"), Label).Text)).ToString() Exit Select Case "Remove" Me.lblTotal2.Text = (Convert.ToDouble(Me.lblTotal2.Text) - Convert.ToDouble(TryCast(Me.GridView2.Rows(index).FindControl("lblPrice"), Label).Text)).ToString() Exit Select End Select End SubRefer : http://www.developerfusion.com/tools/convert/csharp-to-vb/
Mark both my replies as answered one
Karthick S
bouncyger
Member
8 Points
68 Posts
Re: updating a shopping cart
May 04, 2012 09:46 AM|LINK
Hi Thanks for your replies and I feel really stupid for not realising that was c#, it now compiles but nothing appearing in the total label when i press the add button!! Code below Any suggestions? <%@ Page Language="VB" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Dim strDatabaseNameAndLocation As String strDatabaseNameAndLocation = Server.MapPath("shop.mdb") Dim strSQLCommand As String strSQLCommand = "SELECT Product.* FROM Product ORDER BY Product.productID ASC;" Dim objOleDbConnection As System.Data.OleDb.OleDbConnection objOleDbConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0; Data Source=" & strDatabaseNameAndLocation) objOleDbConnection .Open() Dim objOleDbCommand As System.Data.OleDb.OleDbCommand objOleDbCommand = New System.Data.OleDb.OleDbCommand(strSQLCommand, objOleDbConnection) Dim objOleDbDataReader As System.Data.OleDb.OleDbDataReader objOleDbDataReader = objOleDbCommand.ExecuteReader() gdvproducts.DataSource = objOleDbDataReader Dim datDataTable As System.Data.DataTable datDataTable = New System.Data.DataTable() datDataTable.Load(objOleDbDataReader) gdvproducts.DataSource = datDataTable gdvproducts.DataBind() objOleDbConnection .Close() 'Dim inttotal As string 'inttotal = 0 'lblTotal.Text = inttotal End Sub Protected Sub gdvproducts_RowCommand(sender As Object, e As GridViewCommandEventArgs) Dim index As Integer = Convert.ToInt32(e.CommandArgument) Select Case e.CommandName Case "Add" Me.lblTotal.Text = (Convert.ToDouble(Me.lblTotal.Text) + Convert.ToDouble(Me.gdvproducts.Rows(index).Cells(2).Text)).ToString() Exit Select Case "Remove" Me.lblTotal.Text = (Convert.ToDouble(Me.lblTotal.Text) - Convert.ToDouble(Me.gdvproducts.Rows(index).Cells(2).Text)).ToString() Exit Select End Select End Sub </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Supplies Shop</title> </head> <body> <asp:Image ID="imglogo" ImageUrl = "images/shoplogo.jpg" runat="server"></asp:Image> <form id="webform" runat="server"> <div> <asp:GridView ID="gdvproducts" runat="server" AutoGenerateColumns="False" DataKeyNames="productID" onrowcommand="gdvproducts_RowCommand" > <Columns> <asp:BoundField DataField="productID" HeaderText="Product ID" InsertVisible="False" ReadOnly="True" SortExpression="productID" /> <asp:BoundField DataField="productName" HeaderText="Name" SortExpression="productName" /> <asp:BoundField DataField="productPrice" HeaderText="Price" SortExpression="productPrice" /> <asp:TemplateField HeaderText="Add"> <ItemTemplate> <asp:Button id="btnAdd" runat="server" postbackurl="cart.aspx" text="add" /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Remove"> <ItemTemplate> <asp:Button id="btnRemove" runat="server" postbackurl="cart.aspx" text="remove" /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <p> <asp:Label ID="lblTotal" runat="server" Text = "0"></asp:Label></p> </div> </form> </body> </html>karthicks
All-Star
31334 Points
5414 Posts
Re: updating a shopping cart
May 05, 2012 04:07 AM|LINK
hi, first remove the postbackurl="cart.aspx" from both the buttons, and you did not give CommandName and CommandArgument in Add & Remove buttons , cleraly go thru my code and use
<asp:TemplateField HeaderText="Add">
<ItemTemplate>
<asp:Button ID="btnAdd" runat="server" Text="Add" CommandName="Add" CommandArgument='<%# Container.DataItemIndex %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Remove">
<ItemTemplate>
<asp:Button ID="btnRemove" runat="server" Text="Remove" CommandName="Remove" CommandArgument='<%# Container.DataItemIndex %>' />
</ItemTemplate>
</asp:TemplateField>
also you should not blindly bind your grid on every postback, so encapsulate Page_Load code with
If Not Page.IsPostBack Then
' code to bind grid
End If
Mark all my replies as answered one
Karthick S