tags:

views:

265

answers:

2

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 + @""") ";
+1  A: 

You say cookie and cookie2 are strings... what about m_CategoryID?

have you tried

ldsCustomerLinks.Where = ldsCustomerLinks.Where + " && CategoryID == " + m_CategoryID.ToString();

OK - I was suspicious of the lack of quotes for CustomerNumber in the 'Where' string, but not confident enough to post it as a suggestion (having a bad SO day!)

So to answer the second question:

the type mismatch comes within the database, where CustomerNumber (I am inferring) is a string, but you are building a query with a number, like

CustomerID == 312 && CustomerNumber == 45654789 && CategoryID == 3

where you should be saying

CustomerID == 312 && CustomerNumber == "45654789" && CategoryID == 3

and to get the quotes within a string (which is enclosed by quotes) you need to use the """ syntax.

IanR
No, that was working correctly before. What it would do is bring up the customer, for that category, and it worked as it should. Now I'm attempting to narrow it further by when the user selects a customer number from a ddl i have it should show only that number from the categories.
Justen
Oh, and the m_CategoryID is an int
Justen
Ah I see. Thank you. So why include the '@' symbol?
Justen
@ means 'use the following text exactly, without escaping' and is particulary useful for specifying things like file paths - e.g. @"C:\Folder1" instead of "C:\\Folder1" :)
IanR
Ah gotcha, thanks.
Justen
+1  A: 

Forgive me, without much knowledge about this LINQ usage my answer might be totally off.

This question seems to be related though and suggests that you parse/convert the (quoted in that case!) value.

Benjamin Podszun
Nope, casting it in that way using int.Parse yields "No property or field 'int' exists in type 'CustomerLink'"
Justen
Er, nvm. It does work with his last post I didn't see. Can someone explain why this line works when my old one didnt? if ( cookie2 != null ) ldsCustomerLinks.Where += @"
Justen
Included this question in my OP for easier viewing.
Justen