Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 This Foreach loop may be off

Author  Topic 

AdamWest
Constraint Violating Yak Guru

360 Posts

Posted - 2011-03-05 : 23:53:35
the 'AND is always coming out as the first word that would make query fail. But why is 'AND' hitting the first spot in the query string?



if (docs.Length > 0)

{



docQueryString += " AND ( ";

int count = 0;

foreach (DocumentType docT in docs)

{

docQueryString += "[" + docT.DocumentTypeLabel + "]=\"" + invNum + "\"";

count++;

if (count < docs.Length)

// changed OR to AND Here for testing.

docQueryString += " AND ";

}

docQueryString += " ) ";



}

/* }*/

pk_bohra
Master Smack Fu Yak Hacker

1182 Posts

Posted - 2011-03-06 : 05:04:03
Can you provide the output for docQueryString
Go to Top of Page

mmarovic
Aged Yak Warrior

518 Posts

Posted - 2011-03-06 : 05:46:57
maybe because docQueryString is empty before the execution of
docQueryString += " AND ( ";


Mirko

My blog: http://mirko-marovic-eng.blogspot.com/
Go to Top of Page

AdamWest
Constraint Violating Yak Guru

360 Posts

Posted - 2011-03-06 : 12:47:34
here is the string
docQueryString " AND ( [QUOTE_]=\"82820\" OR [INVOICE_]=\"82820\" ) " string

i changed to OR but its same issue

quote:
Originally posted by mmarovic

maybe because docQueryString is empty before the execution of
docQueryString += " AND ( ";


Mirko

My blog: http://mirko-marovic-eng.blogspot.com/

Go to Top of Page

AdamWest
Constraint Violating Yak Guru

360 Posts

Posted - 2011-03-06 : 13:11:24
quote:
Originally posted by AdamWest

here is the string
docQueryString " AND ( [QUOTE_]=\"82820\" OR [INVOICE_]=\"82820\" ) " string

i changed to OR but its same issue, here is the define:

string docQueryString = string.Empty;

quote:
Originally posted by mmarovic

maybe because docQueryString is empty before the execution of
docQueryString += " AND ( ";


Mirko

My blog: http://mirko-marovic-eng.blogspot.com/



Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-03-06 : 13:23:18
Adam, I think mmarovic meant the first instance of the "AND" as shown below in red.
quote:
Originally posted by AdamWest

the 'AND is always coming out as the first word that would make query fail. But why is 'AND' hitting the first spot in the query string?



if (docs.Length > 0)

{



docQueryString += " AND ( ";

int count = 0;

foreach (DocumentType docT in docs)

{

docQueryString += "[" + docT.DocumentTypeLabel + "]=\"" + invNum + "\"";

count++;

if (count < docs.Length)

// changed OR to AND Here for testing.

docQueryString += " AND ";

}

docQueryString += " ) ";



}

/* }*/



Go to Top of Page
   

- Advertisement -