Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.1
|
ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.1 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 20th, 2004, 06:13 AM
Friend of Wrox
 
Join Date: Jun 2004
Posts: 245
Thanks: 0
Thanked 0 Times in 0 Posts
Default Try Catch Question with Parameters

When I enter a email address that doesn't exist in database I get message that there is a duplicate email.

I used labels and DataAdapter confirm that the correct email is being pulled from database. When I do this everything is fine. When the email doesn't exist in database, it responses that email doesn't exist. But not when I use Try Catch statement.

I think there is something wrong with Try Catch statement.

I have been trying to figure this out for 3 hours.
I can't. What is wrong? Thanks.




        Public Sub btnRegister_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRegister.Click
        If IsValid Then
            Dim myCONN As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings("C onnectionString"))

            Dim sSql As String = "SELECT Email FROM Member WHERE Email = @Email"
            Dim selectCmd As New SqlCommand(sSql, myCONN)

            selectCmd.CommandType = CommandType.Text
            selectCmd.Connection = myCONN

            selectCmd.Parameters.Add("@Email", SqlDbType.NVarChar, 100)
            selectCmd.Parameters("@Email").Value = txtEmail.Text



            bDuplicate = True
            Try
                myCONN.Open()
                selectCmd.ExecuteNonQuery()
                myCONN.Close()
            Catch ex As Exception
                bDuplicate = False
                Response.Write("No duplicate")
            Finally
                If myCONN.State = ConnectionState.Open Then
                    myCONN.Close()
                End If

            End Try

            If bDuplicate = True Then
                Response.Write("duplicate")
            End If

            If bDuplicate = False Then
                Response.Write("No duplicate")
            End If

        End If

    End Sub

Thank you.


 
Old August 20th, 2004, 07:45 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Hey,

You shouldn't use a query to find that out, you should use ExecuteReader(); ExecuteNonQuery() returns no results, which you want to find the results. Add code to execute the reader and check the email field for null, to determine duplicates. Or use a DataAdapter.

Brian
 
Old August 20th, 2004, 08:21 AM
Friend of Wrox
 
Join Date: Jun 2004
Posts: 245
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you Brian for your reply but I am not trying to get result back to see if email is null. I already did that to test out the connection and sql statement. They are both correct.

The Try Catch statement was written so that if the select statement found the matching email then it means Duplicate email exists.
If select statement fail then it would throw exception which means Duplicate email doesn't exist.

I got this from a book. Is this Try Catch statement wrong???

 
Old August 20th, 2004, 09:08 AM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

why u dont use @@rowcount
u can use it through a SP using outputparameters.


--------------------------------------------
Mehdi.:)
 
Old August 20th, 2004, 09:15 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

No, that's right. However, if no email comes back, it doesn't fail, it just returns no rows of data. You actually need to test the email return value to verify that. The only way catch will catch an error is if there is an error with the query, or one that returns from the database.

Brian
 
Old August 20th, 2004, 09:31 AM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

I dont know the meaning of original poster if something Brian mentioned use
this boolean statement
Code:
objCommand.ExecuteScalar().Equals(null)
--------------------------------------------
Mehdi.:)
 
Old August 20th, 2004, 01:36 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Hey,

There are several ways to do it, it's just the current way, no error should be thrown in that manner, because the query is OK, and no error should be caused (unless the DB is unavailable or an architecture issue, or the column or table is not spelled correctly).

Brian
 
Old August 20th, 2004, 01:51 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Here's what you should do:

Function DBAuthenticate(ByVal strUsername As String, ByVal strPassword As String) As Integer
        Dim bResult As Boolean = False
        Dim objConn As New OleDbConnection(ConfigurationSettings.AppSettings( "db"))
        Dim strSQL As String
        Dim strGoodPassword As String
        Dim objCommand As New OleDbCommand

        objCommand.Connection = objConn
        strSQL = String.Format("SELECT p_w FROM database1 WHERE (email='{0}');", strUsername)
        objCommand.CommandText = strSQL
        objCommand.CommandType = CommandType.Text

        objConn.Open()
        strGoodPassword = CType(objCommand.ExecuteScalar, String)
        objConn.Close()

        If Not strGoodPassword Is Nothing Then
            If strGoodPassword = strPassword Then
                bResult = True
            Else
                lblMessage.Text = "Invalid Login!"
                lblMessage.Text &= " If you are not a member please click the above link to register."
            End If
        Else
            lblMessage.Text = "Invalid Login!"
            lblMessage.Text &= " If you are not a member please click the above link to register."
        End If

        Return bResult
    End Function

 
Old August 20th, 2004, 03:50 PM
Friend of Wrox
 
Join Date: Jun 2004
Posts: 245
Thanks: 0
Thanked 0 Times in 0 Posts
Default

bmains,
What does (email='{0}') mean?
I understand you are finding email using strUsername.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Try...Catch lowell VB.NET 3 July 23rd, 2007 06:35 AM
Try Catch not working?? dparsons ASP.NET 1.0 and 1.1 Professional 3 September 18th, 2006 06:54 PM
Try and Catch? mujju PHP How-To 2 January 20th, 2005 12:27 PM
TRY CATCH THROW question savoym C# 1 July 1st, 2004 03:54 PM
Catch Problem dag VB.NET 2002/2003 Basics 1 December 8th, 2003 09:28 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.