Click here to Skip to main content
15,890,825 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
SQL
If

                If sCon.sqlReader("cityID").ToString() <> "" Then
                    cboCity.SelectedValue = sCon.sqlReader("cityID")
                End If
                If sCon.sqlReader("StateID").ToString() <> "" Then
                    cboState.SelectedValue = sCon.sqlReader("stateID")
                End If
                If sCon.sqlReader("countryID").ToString() <> "" Then
                    cboCountry.SelectedValue = sCon.sqlReader("countryID")
                End If
            End If
        End If
Posted
Comments
Sergey Alexandrovich Kryukov 9-May-14 0:54am    
The question is: why?
—SA
Maciej Los 9-May-14 15:59pm    
I'd like to ask another question: What do you want to achieve?
Sergey Alexandrovich Kryukov 9-May-14 16:31pm    
The OP's question makes some sense; I answered; please see. The only problem is that OP should have consulted the syntax documentation without asking any questions. It would be much faster. :-)
—SA
José Amílcar Casimiro 9-May-14 15:04pm    
Your code presents no problem.
What you should take into account is that the code is written for humans to understand.
The code needs to be maintained over time, and the most effective is that of code solves the problem for which it was created and that can be maintained easily.
Sergey Alexandrovich Kryukov 9-May-14 16:31pm    
Nevertheless, the question makes some sense; I answered; please see. The only problem is that OP should have consulted the syntax documentation without asking any questions. It would be much faster. :-)
—SA

Please see the syntax BNF for VB.NET "if": http://msdn.microsoft.com/en-us/library/752y8abs.aspx[^].

The single-line syntax is available, please see.

[EDIT]

Do you know what is absolutely the worse in your question? The use of "cityID" and so on, repeated twice in each case. This is a big no-no. Please try to understand why you should never ever do such things.

Second worse is repeating nearly the same "if" statement two times. I call it not programming at all, something opposite to programming. Please see:
http://en.wikipedia.org/wiki/Don%27t_repeat_yourself[^].

—SA
 
Share this answer
 
v3
Comments
Maciej Los 9-May-14 16:46pm    
5ed!
What about my answer?
Sergey Alexandrovich Kryukov 9-May-14 16:50pm    
Thank you, Maciej.
—SA
José Amílcar Casimiro 10-May-14 3:49am    
5! Good Answer.
Sergey Alexandrovich Kryukov 10-May-14 19:04pm    
Thank you, José Amílcar.
—SA
[no name] 10-May-14 13:10pm    
Nevertheless learned again,so my 5++
Not sure what single line means... but i think i got what you want to achieve.

If you want to loop through the collection of fields...

VB
Dim i As Integer = 0 
Do 
    If sCon.sqlReader.Item(i).Value.ToString()<>String.Empty Then 
        Me.Controls("cbo" & Left(sCon.sqlReader.Item(i).ColumnName, Len(sCon.sqlReader.Item(i).ColumnName) - 2)) = sCon.sqlReader.Item(i)
        Exit Do
    End If
    i= i + 1
Loop Until sCon.sqlReader.Item(i).Value.ToString()=String.Empty
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 9-May-14 16:57pm    
Well, voted 4. The whole idea to add the alternative is good. Using "" instead of string.Empty is bad. Besides, we don't know if OP wants all columns in sequence. If not, if could be traversing the array of names of columns.
—SA
Maciej Los 9-May-14 17:03pm    
4 is fair enough ;)
Am i blind this evening or what?
Thank you, Sergey ;)

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