Checking if a Record Exsists
Checking if a Record Exsists
Checking if a Record Exsists
Checking if a Record Exsists
Checking if a Record Exsists
Checking if a Record Exsists Checking if a Record Exsists Checking if a Record Exsists Checking if a Record Exsists Checking if a Record Exsists Checking if a Record Exsists Checking if a Record Exsists Checking if a Record Exsists
Checking if a Record Exsists Checking if a Record Exsists
Checking if a Record Exsists
Go Back  Xtreme Visual Basic Talk > > > Checking if a Record Exsists


Reply
 
Thread Tools Display Modes
  #1  
Old 11-08-2011, 03:05 PM
jaden2001 jaden2001 is offline
Junior Contributor
 
Join Date: Apr 2005
Location: Albany, GA
Posts: 235
Default Checking if a Record Exsists


I am having a ton of trouble trying to check if a record exists. I continue to get a data mismatch on my command.ExecuteScalar(). If anyone can help it would be much appreciated.

Code:
Protected Sub btnRegister_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnRegister.Click
        Dim fileName As String = Server.MapPath("dataPath.txt")
        Dim objStreamReader As StreamReader
        objStreamReader = File.OpenText(fileName)
        Dim contents As String = objStreamReader.ReadToEnd()

        Dim connectionString As String
        Dim strQuery As String


        connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & contents
        strQuery = "SELECT count(*) FROM [Users] WHERE userEmail=" & "'" & txtEmailAddress.Text & "'" & " AND courseID=" & "'" & ddlCourseID.SelectedValue.ToString & "'"

        Dim count As Integer
        Using conn As New OleDb.OleDbConnection(connectionString)
            conn.Open()

            Using command As New OleDb.OleDbCommand(strQuery, conn)

                count = Convert.ToInt32(command.ExecuteScalar())
                conn.Close()
            End Using
        End Using

        If count > 0 Then
            Response.Redirect("http://www.google.com", False)
        Else
            Dim emailValidated As Boolean = checkEmail(txtEmailAddress)
            If emailValidated = True Then
                Dim outlookValue As String = ddlCourseID.SelectedValue.ToString
                SqlDataSource3.Insert()
                Call AddOutlookAppointment(outlookValue)
                Response.Redirect("registrationComplete.aspx", False)
            Else
            End If
        End If


    End Sub
Thanks,
__________________
Da Vinci - "Sleep is but a waste of time"
Reply With Quote
  #2  
Old 11-11-2011, 07:03 AM
Orca44 Orca44 is offline
Centurion
 
Join Date: Nov 2004
Location: Cape Town, South Africa
Posts: 124
Default

Have you tested whether the value returned by the ExecuteScalar might be 'nothing'?
I know count() shouldn't return a null, but it might be a good idea to see what is
being returned by the ExecuteScalar.
Reply With Quote
  #3  
Old 11-11-2011, 10:04 AM
DrPunk's Avatar
DrPunkChecking if a Record Exsists DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,403
Default

Wouldn't courseID field be an integer field and therefore the parameter value doesn't need to be inside inverted commas?

Code:
"SELECT count(*) FROM [Users] WHERE userEmail=" & "'" & txtEmailAddress.Text & "'" & " AND courseID=" & "'" & ddlCourseID.SelectedValue.ToString & "'"
__________________
There are no computers in heaven!
Reply With Quote
  #4  
Old 11-14-2011, 05:38 PM
jaden2001 jaden2001 is offline
Junior Contributor
 
Join Date: Apr 2005
Location: Albany, GA
Posts: 235
Default

Thanks DrPunk,

I forgot about that with integer values. I had been starring for days thinking something was completely wrong. I appreciate all the contributions! Thanks Happy Programming!
__________________
Da Vinci - "Sleep is but a waste of time"
Reply With Quote
  #5  
Old 11-15-2011, 01:45 AM
jwshepherd jwshepherd is offline
Freshman
 
Join Date: May 2009
Location: West Texas
Posts: 45
Default

Just to point out, The email address is a text field and should be cleaned before being used in a sql query. This is how sql injection attacks occur. Using a count field is not so much of an issue, but a select query without cleaning a text field can be a disaster.
Reply With Quote
  #6  
Old 12-31-2011, 12:32 AM
cigarette cigarette is offline
Newcomer
 
Join Date: Mar 2010
Location: Pinas
Posts: 8
Default

Use IF Condition

here's a snippet
[CODE]rs.Open("SELECT * FROM TABLE WHERE FIELDNAME1 LIKE'" & TEXTBOX1.Text & "' AND FIELDNAME2 NOT LIKE '" & "0.00" & "'", db, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockOptimistic)
If Not rs.EOF Then
Messagebox.show("I win.")
ElseIf rs.EOF Then
Messagebox.show("I didn't win.")
End If
rs.Close()[\CODE]
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Checking if a Record Exsists
Checking if a Record Exsists
Checking if a Record Exsists Checking if a Record Exsists
Checking if a Record Exsists
Checking if a Record Exsists
Checking if a Record Exsists Checking if a Record Exsists Checking if a Record Exsists Checking if a Record Exsists Checking if a Record Exsists Checking if a Record Exsists Checking if a Record Exsists
Checking if a Record Exsists
Checking if a Record Exsists
 
Checking if a Record Exsists
Checking if a Record Exsists
 
-->