Deleting a row from a table in your database ....

windows

Guest
Hi, I've been stuck on this problem for absolutley ages now and now that even google is failing me, Ive decided to come on here and ask for help.

Currently im creating a mock e-commerce online shop website using ASP.NET and c#.

Im now on the Cart page where the user can view what he or she has bought, it shows a table of the item, quantity, price etc etc.

Now I want to add a link button into the table of products (the table from the database known as cart) which will let the user delete a product from the table.

Do I need to create another connection to deal with deleting the product ?

2nd question .... I need to create a label which calculates the total cost of the order (in conjunction with the price x quantity), I also need this for the separate items in the cart as well.

Any help ? Thanks in advance.just use an SQL statement
DELETE * FROM tabel WHERE columnName = CartIDYeah I know, but im new to the asp.net stuff and am struggling to connect the SQL statement along with the link button in the table.Now im getting an error message saying ; expected, although im 99.99% sure that I have a ; everywhere its needed !

grrrrr

heres my html and code

<%@ Page Language="c#" Debug="true" Inherits="dvd.viewCart" Src=http://www.webdeveloper.com/forum/archive/index.php/"viewCart.cs" %>
<html>
<head>
</head>
<body>
<form runat="server">
<h4>Your Cart:
</h4>
<asp:datagrid id="dgdvdList" runat="server" OnItemCommand="Detail" AutogenerateColumns="False" BorderWidth="3" CellPadding="2" Font-Size="12" BorderColor="black" BorderStyle="Solid" BackColor="Aqua">
<HeaderStyle forecolor="black" />
<ItemStyle forecolor="Red" />
<AlternatingItemStyle forecolor="darkblue" />
<Columns>
<asp:boundcolumn HeaderText="dvdTitle" DataField="dvdTitle" />
<asp:boundcolumn HeaderText="Genre" DataField="Genre" />
<asp:boundcolumn HeaderText="Price" DataField="Price" />
<asp:boundcolumn HeaderText="Quantity" DataField="Qty" />
<asp:buttonColumn ButtonType="LinkButton" commandName="delete" Text="Delete" />
</Columns>
</asp:datagrid>
<br />
<br />
<asp:button id="Button1" onclick="ReturnToList" runat="server" Font-Size="12" BorderColor="black" BackColor="Aqua" borderstyle="solid" text="Return to list"></asp:button>
</form>
</body>
</html>



using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;


namespace dvd
{
public class viewCart : Page
{
public TextBox Name;
public Label Message;
public DataGrid dgdvdList;

public void Page_Load(object sender, EventArgs e)
{
BindDataGrid();
}

public void BindDataGrid()
{

OleDbConnection conn = new OleDbConnection() ;
string connStr;
connStr = "Provider=Microsoft.Jet.OLEDB.4.0;";
connStr += "Data Source= " + Server.MapPath("..\\..\\data\\dvds.mdb");
conn.ConnectionString = connStr;

// Build command object //

string queryString;
queryString = "SELECT dvdTitle, Genre, Price, Qty FROM qtCart WHERE OrderNbr = " + Convert.ToInt32(Session["sOrderNbr"]);
OleDbCommand dbCommand = new OleDbCommand();
dbCommand.CommandText = queryString;
dbCommand.Connection = conn;

// Open the connection //
conn.Open();

// Build and fill the dataReader //
OleDbDataReader dataReader = dbCommand.ExecuteReader();

//Bind the data to the datagrid
dgdvdList.DataSource=dataReader;
dgdvdList.DataBind();

conn.Close();

} // end of BindDataGrid

// copied this code from properties.cs, but took out the response.redirect

public void Detail(object sender, DataGridCommandEventArgs e)
{
//Build connection object //
OleDbConnection conn = new OleDbConnection() ;
string connStr;
connStr = "Provider=Microsoft.Jet.OLEDB.4.0;";
connStr += "Data Source= " + Server.MapPath("..\\..\\data\\dvds.mdb");
conn.ConnectionString = connStr;

// Build command object //
string queryString;
queryString = "Delete * from tCart where ProductCode = Session["sProductCode"]";
OleDbCommand dbCommand = new OleDbCommand();
dbCommand.CommandText = queryString;
dbCommand.Connection = conn;


conn.Open();

dbCommand.ExecuteNonQuery();

}

public void ReturnToList(object sender, EventArgs e)
{
Response.Redirect("properties.aspx");
}

}
}


Sorry if this is too clunky etc, but I am getting so p*ssed off with this now that im tempted to rub nectar in my eyes and run into the beehive.

ah.... the beauty of web development eh ? :mad: :(after the executeReader it's best to say:
while(dataReader.Read())
{
databinding
}
dataReader.Close();

but that isn't the problem

Could you say what line the error is generated?right i've slightly changed the code now

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;



namespace dvd
{
public class viewCart : Page
{
public TextBox Name;
public Label Message;
public DataGrid dgdvdList;

public void Page_Load(object sender, EventArgs e)
{
BindDataGrid();
}

public void BindDataGrid()
{

OleDbConnection conn = new OleDbConnection() ;
string connStr;
connStr = "Provider=Microsoft.Jet.OLEDB.4.0;";
connStr += "Data Source= " + Server.MapPath("..\\..\\data\\dvds.mdb");
conn.ConnectionString = connStr;

// Build command object //

string queryString;
queryString = "SELECT dvdTitle, Genre, Price, Qty FROM qtCart WHERE OrderNbr = " + Convert.ToInt32(Session["sOrderNbr"]);
OleDbCommand dbCommand = new OleDbCommand();
dbCommand.CommandText = queryString;
dbCommand.Connection = conn;

// Open the connection //
conn.Open();

// Build and fill the dataReader //
OleDbDataReader dataReader = dbCommand.ExecuteReader();

//Bind the data to the datagrid
dgdvdList.DataSource=dataReader;
dgdvdList.DataBind();

conn.Close();

} // end of BindDataGrid




public void Detail(object sender, DataGridCommandEventArgs e)
{
//Build connection object //
OleDbConnection conn = new OleDbConnection() ;
string connStr;
connStr = "Provider=Microsoft.Jet.OLEDB.4.0;";
connStr += "Data Source= " + Server.MapPath("..\\..\\data\\dvds.mdb");
conn.ConnectionString = connStr;

// Build command object //
string queryString;
queryString = "Delete * from tCart where ProductCode " + Convert.ToInt32(Session["sProductCode"]);
OleDbCommand dbCommand = new OleDbCommand();
dbCommand.CommandText = queryString;
dbCommand.Connection = conn;

// Open the connection //
conn.Open();



dbCommand.ExecuteNonQuery();

}



public void ReturnToList(object sender, EventArgs e)
{
Response.Redirect("properties.aspx");
}

// end of void detail

}
}



And the new error message is this :

Syntax error (missing operator) in query expression 'ProductCode 13'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.OleDb.OleDbException: Syntax error (missing operator) in query expression 'ProductCode 13'.

Im trying to delete the item with ProductCode 13 from my cart, but its having none of it.

grrrrrrrrr.

This is the line thats supposedly given me the error:

Line 84: dbCommand.ExecuteNonQuery();
 
Top