Solved

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

Posted on 2012-04-10
4
282 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 250 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 250 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

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:…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

776 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