Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Error trying to do "if this and this then this" in VB.net

Posted on 2012-04-10
4
Medium Priority
?
287 Views
Last Modified: 2012-04-10
I  am getting an error on my compounbd if comparer nand I cant get it straight - can anyone help
 Try

                    Dim COUNT As Integer
                    Dim connString As String = "server=SERVER;initial catalog=apps;persist security info=False;user id=USER;password=PASS"
                    Dim conn As New SqlConnection(connString)
                    conn.Open()
                    Dim command As New SqlCommand
                    Dim strSQL As String = "SELECT * FROM TABLE WHERE GROUPID ='" & GUID & "' AND AttributeID ='" & DDLVALUE & "' AND AttributeValue = '" & checkedvalue & "'"
                    Dim da As New SqlDataAdapter(strSQL, conn)
                    Dim o As Object = command.ExecuteScalar()
                    If ((o <> Nothing) & (o <> DBNull.Value)) Then <---here is the error

                        COUNT = 1
                    Else
                        strSQL = "INSERT INTO TABLE (GROUPID,AttributeID,AttributeValue) Values ('" & GUID & "','" & DDLVALUE & "','" & checkedvalue & "') SELECT SCOPE_IDENTITY()"
                        o = command.ExecuteScalar()

                        COUNT = 0
                    End If
                Catch ex As Exception
                    Response.Write(ex.Message)
                End Try
            End If
        Next

Open in new window

0
Comment
Question by:r3nder
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 14

Assisted Solution

by:nishant joshi
nishant joshi earned 1000 total points
ID: 37829591
 Try

                    Dim COUNT As Integer
                    Dim connString As String = "server=SERVER;initial catalog=apps;persist security info=False;user id=USER;password=PASS"
                    Dim conn As New SqlConnection(connString)
                    conn.Open()
                    Dim command As New SqlCommand
                    Dim strSQL As String = "SELECT * FROM TABLE WHERE GROUPID ='" & GUID & "' AND AttributeID ='" & DDLVALUE & "' AND AttributeValue = '" & checkedvalue & "'"
                    Dim da As New SqlDataAdapter(strSQL, conn)
                    Dim o As Object = command.ExecuteScalar()
                    If ((o <> Nothing) AND (o <> DBNull.Value)) Then <---here is the error

                        COUNT = 1
                    Else
                        strSQL = "INSERT INTO TABLE (GROUPID,AttributeID,AttributeValue) Values ('" & GUID & "','" & DDLVALUE & "','" & checkedvalue & "') SELECT SCOPE_IDENTITY()"
                        o = command.ExecuteScalar()

                        COUNT = 0
                    End If
                Catch ex As Exception
                    Response.Write(ex.Message)
                End Try
            End If
        Next

Open in new window


check i have change in condition.
thanks.
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 37829640
A check for nothing should be sufficient, i.e
If o Is Nothing Then '<---here was the error
    COUNT = 1
Else
    strSQL = "INSERT INTO TABLE (GROUPID,AttributeID,AttributeValue) Values ('" & Guid & "','" & DDLVALUE & "','" & checkedvalue & "') SELECT SCOPE_IDENTITY()"
     o = command.ExecuteScalar()
     COUNT = 0
End If

Open in new window

0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 1000 total points
ID: 37829656
I might also suggest using AndAlso rather than simply And. The difference being that AndAlso is short-circuited--meaning if the first part of the expression is false, then the whole expression can never be true, so there is no need to test the second condition. And is not short-circuited, and it will test both conditions regardless of the truth of either part of the expression.
0
 
LVL 6

Author Closing Comment

by:r3nder
ID: 37830470
Thank you Kaufmed and nishantcomp2512 a combination of both
0

Featured Post

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

704 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