Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a problem as show above. I have a productdetail.aspx and a addtocart btn in it.
when i click add to cart btn i have this error.

this is my DAL
C#
public DataSet GetShoppingCart(string userID)
{

    SqlConnection conn;
    StringBuilder sql;
    SqlDataAdapter da;
    DataSet PCData;

    conn = dbConn.GetConnection();
    PCData = new DataSet();
    sql = new StringBuilder();
    sql.AppendLine("SELECT * FROM BQ_ShoppingCart");
    sql.AppendLine("WHERE userID = @userID");

    try
    {
        conn.Open();
        da = new SqlDataAdapter(sql.ToString(), conn);
        da.SelectCommand.Parameters.AddWithValue("@userID", userID);
        da.Fill(PCData);
    }

    catch (Exception ex)
    {
        errMsg = ex.Message;

    }

    finally
    {
        conn.Close();

    }

    return PCData;
}

MY BLL
C#
[System.ComponentModel.DataObjectMethodAttribute
(System.ComponentModel.DataObjectMethodType.Select, true)]
public DataSet GetShoppingCart(string userID)
{
    BQ_DAL dlSc = new BQ_DAL();
    return dlSc.GetShoppingCart(userID);
}
Posted
Updated 17-Jun-13 17:47pm
v2
Comments
Christian Graus 17-Jun-13 23:40pm    
Where do you get this error ? Your code is a real mess, I have to say. Why do you need two lines on a string builder to build some basic SQL ? What is a SQLDataAdapter ? If you're writing SQL, why not just run it directly ?
NooobieCoder 17-Jun-13 23:43pm    
I get this error when i click on my addtoCart btn and there a insert method in my btn
Christian Graus 17-Jun-13 23:51pm    
So, in code you did not post ? How can we help you then ?
Thanks7872 17-Jun-13 23:58pm    
Make some good coding practices as christian said,no need to use two lines on string builder as you did.Just use parameterized queries. I will be enough for this type of simple SQL.

1 solution

Hi
If you are trace your program you can see your StringBuilder output is a wrong because you have not blink between first slq.AdppendLine and second sql.AppendLine so your output is a:
SELECT * FROM BQ_ShoppingCartWHERE userID = @userID

Where clause is a wrong so you need a blink at the end of first sql.AppendLine how ever you don`t need to the StringBuilder for this purpose and I don`t know what you used it?
SQL
sql.AppendLine("SELECT * FROM BQ_ShoppingCart ");
sql.AppendLine("WHERE userID = @userID");


You have really easy way for making query string same as:

C#
String sqlQuery = "SELECT * FROM BQ_ShoppingCart WHERE userID = @userID";


Best Regards.
 
Share this answer
 
v4
Comments
_Amy 18-Jun-13 0:17am    
No, it's not. Please see : http://postimg.org/image/3ovzkd1nl/[^]
Also check : MSDN : StringBuilder.AppendLine Method[^]
--Amit
Aydin Homay 18-Jun-13 0:22am    
Could you write the two string builder and check the ToString() method output ? I think you should think about it again ;) you are Downvoted me !!!!!!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900