Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Dear Sir,
While Loop in C# is working properly, but in SQL is not. Please check it ...
Code in C# :
C#
int w = crno + 1;
                while (w>0)
                {
                    string C = AccessUnique("select " + ColSr + " from " + TName + " where " + ColSr + "='" + w.ToString() + "' AND FY_Code='" + FY + "'");
                    if (C != null)
                    {
                        w++;
                    }
                    else if (C == null)
                    {
                        CRNO = Convert.ToString(w);
                        break;
                    }
                }

Syntax in SQL :
SQL
set @w = @RecNoN;
    while (@w>0)
        begin
            select @C = Rec_No from PaymentDetails where FY_Code=@FYCode and Rec_No=@w
            if (@C is not null)
                begin
                    set @w =@w+1
                end
            else if (@C is null)
                begin
                    set @RecNo = @w
                    BREAK;
                end;
            BREAK;
        end;
Posted
Comments
ZurdoDev 29-Jun-15 9:27am    
1. What do you mean it isn't working?
2. Debug it to see what is happening.
Suvendu Shekhar Giri 29-Jun-15 9:45am    
Why have you put a unconditional BREAK in the loop. That will end the loop so your loop will not be executed more than once. I suspect, it can be the cause for your problem.
LebneizTech 29-Jun-15 10:09am    
OK, but I had set condition ... else if (@C is null) then it Break.
Please correct it.
Suvendu Shekhar Giri 29-Jun-15 15:19pm    
What about the second BREAK statement?
Richard Deeming 29-Jun-15 9:52am    
Your code is vulnerable to SQL Injection[^].

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

1 solution

In your C# code, the variable C is scoped within the while loop. Each time the loop executes, the variable will be reset to its default value of null.

In SQL, the variable is scoped to the batch. It will retain its value from the previous execution of the loop. When the SELECT statement doesn't match any rows, the value of the variable will not be updated.

To solve the problem, reset the variable before the SELECT statement:
SQL
WHILE @W > 0
BEGIN
    SET @C = Null;
    SELECT @C = Rec_No FROM PaymentDetails WHERE FY_Code = @FYCode And Rec_No = @w;
    
    If @C Is Not Null
    BEGIN
        SET @w = @w + 1;
    END
    Else -- No need to test here - if the code gets here, @C is null by definition
    BEGIN
        SET @RecNo = @w;
        BREAK;
    END;
END;


As I mentioned in the comments, your C# code is vulnerable to SQL Injection[^]. You need to get rid of the string concatenation and use a parameterized query instead.
 
Share this answer
 

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



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