Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2012-04-10
4
Medium Priority
?
288 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
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
Loops Section Overview
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

782 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