Solved

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

Posted on 2012-04-10
4
278 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
Comment Utility
 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
Comment Utility
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 74

Accepted Solution

by:
käµfm³d   👽 earned 250 total points
Comment Utility
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
Comment Utility
Thank you Kaufmed and nishantcomp2512 a combination of both
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now