I'm trying to dynamically set my where statements for a linq datasource, and it was working fine until I try to add another constraint.
This code works here:
private void SetDataSourceWhereStatement()
{
HttpCookie cookie = Request.Cookies[ "CustomerID" ];
if ( cookie == null ) //set data source where statement to default
ldsCustomerLinks.Where = "CustomerID == -1";
else ldsCustomerLinks.Where = "CustomerID == " + cookie.Value; //set data source where statement
ldsCustomerLinks.Where = ldsCustomerLinks.Where + " && CategoryID == " + m_CategoryID;
ldsCustomerLinks.DataBind();
}
However, when I also try and add a customer number, I get the error. This is the code I'm attempting to use:
private void SetDataSourceWhereStatement()
{
HttpCookie cookie = Request.Cookies[ "CustomerID" ];
HttpCookie cookie2 = Request.Cookies[ "CustomerNumber" ];
if ( cookie == null ) //set data source where statement to default
ldsCustomerLinks.Where = "CustomerID == -1";
else ldsCustomerLinks.Where = "CustomerID == " + cookie.Value; //set data source where statement
if ( cookie2 != null )
ldsCustomerLinks.Where += " && CustomerNumber == " + cookie2.Value;
// else ldsCustomerLinks.Where += " && CustomerNumber >= 0";
ldsCustomerLinks.Where = ldsCustomerLinks.Where + " && CategoryID == " + m_CategoryID;
ldsCustomerLinks.DataBind();
}
Both cookie and cookie2 values are strings. I've tried converting it using int.parse, int32.parse, using the value's .toString() methods and trying everything all over. I don't understand what's wrong.
EDIT::
So I got my answer, from one of the below posts, but I do not understand, could someone please explain why my original code did not work but the revised does?
Old code:
if ( cookie2 != null )
ldsCustomerLinks.Where += " && CustomerNumber == " + cookie2.Value;
New code:
if ( cookie2 != null )
ldsCustomerLinks.Where += @" && CustomerNumber == (""" + cookie2.Value + @""") ";