 |
| 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
|
|
|
|

August 20th, 2004, 06:13 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2004
Posts: 245
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|
|

August 20th, 2004, 07:45 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
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
|
|

August 20th, 2004, 08:21 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2004
Posts: 245
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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???
|
|

August 20th, 2004, 09:08 AM
|
|
Friend of Wrox
|
|
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
|
|
why u dont use @@rowcount
u can use it through a SP using outputparameters.
--------------------------------------------
Mehdi.:)
|
|

August 20th, 2004, 09:15 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
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
|
|

August 20th, 2004, 09:31 AM
|
|
Friend of Wrox
|
|
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
|
|
I dont know the meaning of original poster if something Brian mentioned use
this boolean statement
Code:
objCommand.ExecuteScalar().Equals(null)
--------------------------------------------
Mehdi.:)
|
|

August 20th, 2004, 01:36 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
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
|
|

August 20th, 2004, 01:51 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
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
|
|

August 20th, 2004, 03:50 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2004
Posts: 245
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
bmains,
What does (email='{0}') mean?
I understand you are finding email using strUsername.
|
|
 |