Solved

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

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

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…
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…
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, f…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

920 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

18 Experts available now in Live!

Get 1:1 Help Now