-1

My problem now is about how to use "not equal" in SQL. Here is my code:

Dim cmd As New SqlClient.SqlCommand
    With cmd
        .CommandText = "SELECT [Position],[Partylist],[Fullname],[Lvl],[Votes] FROM tbl_cands WHERE [Department] = '" & elem & "'AND [PositionID]=" & pres & ""
        .CommandType = CommandType.Text
        .CommandTimeout = 30
        .Connection = conn
    End With
    Dim dt As New DataTable
    dt.Load(cmd.ExecuteReader)

I need not to display fullname that is not equivalent to None.

4
  • This code looks like it could open you to SQL injection hacks. Consider using parameterized techniques instead. Commented Nov 6, 2013 at 7:00
  • Do you mean just adding AND [Fullname] <> 'None' to your query? Commented Nov 6, 2013 at 7:09
  • yes, @Szymon exactly Commented Nov 6, 2013 at 7:23
  • I've posted it as the answer then Commented Nov 6, 2013 at 7:29

2 Answers 2

1

If you are checking for NULL then the code is:

AND [Fullname] IS NOT NULL

If you want to check the value isn't a zero length string:

AND [Fullname] <> ''

If you want to check it isn't a specific value that denotes no data:

AND [Fullname] <> 'None'

But in any case, I strongly recommend you read the following link and learn how to use parameterization for your SqlCommand object. If you don't escape strings correctly you leave yourself vulnerable to injection hacks:

How to use parameters "@" in an SQL command in VB

Sign up to request clarification or add additional context in comments.

Comments

1

All you have to do is to add that to your query:

AND [Fullname] <> 'None'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.