Last post Jul 25, 2010 02:42 PM by blue1011
Jul 25, 2010 10:08 AM|blue1011|LINK
I'm creating a shopping cart from scratch. I've created a catalog of various products and item detail pages which are linked to the database via a "SqlDataSource". Now I'm trying to install an add to cart button, and here's where I'm encountering problems...
In the codebehind of the page I have the button event set up and working, it changes a label to say the item has been added. I add a MySQL connection using the namespace below:
public void AddtoCart()
// Check for cookie
if(Request.Cookies["mycart"] != null)
int cartnum = Server.HtmlEncode(Request.Cookies["mycart"]["cartnum"]);
else // If no cookie is found...
// Generate random cart number
int cartnum = Random.next(1000,9999);
// Create new cookie
Response.Cookies["mycart"]["cartnum"] = cartnum;
Response.Cookies["mycart"].Expires = DateTime.Now.AddDays(1);
// Get Product ID from URL
int prodID = Request.QueryString("ProductID");
string strProvider = "Server=myserver; Port=1234; Database=mydatabase; Uid=mydatabase; Pwd='password'";
int qty = MyTextBox.Text;
MySqlConnection mysqlCon = new MySqlConnection(strProvider);
string insertSQL = "INSERT INTO ShoppingCart (cartnum, productid, qty) VALUES('" + cartnum + ", " + prodID + ", " + qty + "')";
MySqlCommand mysqlCmd = new MySqlCommand(insertSQL,mysqlCon);
When I build it, it says that it can't locate the mysql namespaces and gives me an CS0246 error four times. The files who which it is referring are in the bin folder of the project and I have the files linked to the project via the web.config file.
I thought maybe it just needs to be uploaded, but when I upload the files as is, the button doesn't do anything - the button will make the label display whatever was in the last successful build.
I don't know what I'm doing wrong, any ideas?
Jul 25, 2010 02:42 PM|blue1011|LINK
If I can't get the page to connect to the database, can I call upon the existing connection that was made via the web.config file?
The following is the connection string in the web.config file:
<add name="sqlDataMaster" connectionString="server=myserver;User Id=myusername;password=mypassword;database=mydatabase"
In the .aspx file I successful link a datalist to the database with the following code:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:sqlDataMaster %>"
ProviderName="<%$ ConnectionStrings:sqlDataMaster.ProviderName %>"
SelectCommand="SELECT * FROM Products WHERE (Products.productid = @productid)">
<asp:QueryStringParameter Name="productID" QueryStringField="ProductID" Type="Int32" />