?
Solved

To check an if condition in vb.net

Posted on 2009-04-20
12
Medium Priority
?
743 Views
Last Modified: 2012-05-06
Hi
qry= qry &...........
I have a query continuation in vb.net as below:
If qry = qry & "(table1.a = test)" Then
do this
End if
where ---   test is an word and table1.attribute1=test in the condition above
but this is not working fine. its not checking the condition properly. I think i am doing something wonrg the way I am writing it. Please help



0
Comment
Question by:passion420
  • 6
  • 6
12 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24187134
What you wrote says if the value stored in qry equals the value stored in query concatenated with the text "(table1.a = test)", which is like saying, if 1 equals 12--it will never happen.

Aside from that, I am not sure what you are trying to do. Could you explain a little further?
0
 

Author Comment

by:passion420
ID: 24187172
I means to say that test is a letter so the table1.attribute should be equal to that letter. Was just trying to clarify, if test should be enclosed in 'test'  ?? Please check below what am I doing.

the condition
f qry = qry & "(table1.a = test)" Then
do this
End if
is not checking that, I think will need to alter that
0
 

Author Comment

by:passion420
ID: 24187180
sorry thats If* in above comment:
If qry = qry & "(table1.a = test)" Then
do this
End if

What do i need to change here so that it works. Thanks
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24187208
Let's say you have

    Dim qry As String = "TEXT"

and this if block

    If qry = qry & "(table1.a = test)" Then
        'do this
    End If

then what you are really saying is

    If "TEXT" = "TEXT(table1.a = test)" Then
        'do this
    End If

This will never evaluate to true. What are you storing in qry?
0
 

Author Comment

by:passion420
ID: 24187346
This is how my query is right now:
qry = "select  isnull(table1.a, '') as check , "

        If dropdown1.SelectedValue <> "-1" Then
            qry = qry & "isnull(""b"", '') as ""b"","
        End If

        qry = qry & " isnull(table2.b, '') as check2,"

       

        If dropdown1.SelectedValue <> "-1" Then
            qry = qry & "Isnull(Sum(cast(c as float)),0) as check3"
        Else
----------this is where i have inludede the above condition as:
If qry = qry & "(table1.a = test)" Then
do this
else

End if

                                 End If

where.........
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24187398
What is the "do this" part of you if statement?
0
 

Author Comment

by:passion420
ID: 24187885
here is the do this part :)
if.....above  condition.....then
 qry = qry & "Isnull(Sum(cast(x * y) + table2.z),0)*2 as someattribute
  Else
                qry = qry & some calcularion
end if
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24188138
I believe you are looking to do something like the following (but I'm not completely sure):
qry = "select  isnull(table1.a, '') as check , "
 
        If dropdown1.SelectedValue <> "-1" Then
            qry = qry & "isnull(""b"", '') as ""b"","
        End If
 
        qry = qry & " isnull(table2.b, '') as check2,"
 
       
 
        If dropdown1.SelectedValue <> "-1" Then
            qry = qry & "Isnull(Sum(cast(c as float)),0) as check3"
        Else
 
            qry = qry & "case when table1.a = test then Isnull(Sum(cast(x * y) + table2.z),0)*2 else some calcularion)"
        End if

Open in new window

0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 24188150
Correction (you need to fill in the "some calculation" part):
qry = "select  isnull(table1.a, '') as check , "
 
        If dropdown1.SelectedValue <> "-1" Then
            qry = qry & "isnull(""b"", '') as ""b"","
        End If
 
        qry = qry & " isnull(table2.b, '') as check2,"
 
       
 
        If dropdown1.SelectedValue <> "-1" Then
            qry = qry & "Isnull(Sum(cast(c as float)),0) as check3"
        Else
 
            qry = qry & "case when table1.a = test then Isnull(Sum(cast(x * y) + table2.z),0)*2 else some calcularion end)"
        End if

Open in new window

0
 

Author Comment

by:passion420
ID: 24188256
Yes thats wat i have right now as u said above, below is what is wat exactly i m trying to do:

If dropdown1.SelectedValue <> "-1" Then
            qry = qry & "Isnull(Sum(cast(c as float)),0) as check3"
        Else
----I need to have if condition here as:
 -----multiply by 2  statement below should execute only when the following condition is followed
---this is the condition.............if(table2. attribute="test") then the first statement below should execute otherwise the else part

            qry = qry & "case when table1.a = test then Isnull(Sum(cast(x * y) + table2.z),0)*2
 else
            qry = qry & "case when table1.a = test then Isnull(Sum(cast(x * y) + table2.z),0)
        End if


But I need to include a condition where if(table2.attribute3="test") in the above condition as. the problem is how to include the if(table2. attribute="test")  condition above if write simply like this.....it do not recognizes the table so i was trying somehting like this :

If qry = qry & "(table1.a = test)" Then
do this
End if

which is wrong i think....Thanks !!
0
 

Author Comment

by:passion420
ID: 24188591
Sorry about the question above, i got wat you said in the last comment:
can you tell me how to write the query you told me above, I am trying something based on yr comment as:

qry = qry & "case when table2.attribute =test then "

               qry = qry & "case when table1.a = test then Isnull(Sum(cast(x * y) + table2.z),0)*2
 else
            qry = qry & "case when table1.a = test then Isnull(Sum(cast(x * y) + table2.z),0)
        End if

can you suggest how to write this condition: qry = qry & "case when table2.attribute =test then "

I need to replace when with if or something?? Thanks a lot.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24190936
I think you are mixing the concepts of VB code and SQL code. From what I understand, you are trying to create a SQL query in your VB code--which is fine. However, you can't write if conditions in your VB code that will modify your query based on something in the database. You have to write the if conditions within your query, which is what I was trying to do with the "case/when/else" statement.

The "case/when/else" is like an if statement except that it is executed in SQL when you submit the query to the server. So line 15 in my last post says,

if table1.a = test then
    Isnull(Sum(cast(x * y) + table2.z),0)*2
else
    some calculation 'which you haven't filled in yet
end if
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Integration Management Part 2
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month14 days, 4 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question