Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 exeception error ?

Author  Topic 

jhermiz

3564 Posts

Posted - 2004-09-15 : 16:01:30
Heres the vb.net code section I am working on...


<%@ Page Language="VB" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<%@ import Namespace="System.Web.Mail" %>

<script runat="server">


Sub Button_Click(sender As Object, e As EventArgs)
Dim objMailMessage As MailMessage
Dim conMyData As SqlConnection
Dim cmdSelect As SqlCommand
Dim dtrLogin As SqlDataReader

If txtEmail.Text = "" Then
lblMessage.Text = "Please enter your e-mail address!"
exit sub
End if

'get information from Login table
'try and make a connection
Try
conMyData = New SqlConnection( ConfigurationSettings.AppSettings("strConn") )
cmdSelect = New SqlCommand( "select_login_info_forgot_pwd", conMyData )
cmdSelect.CommandType = CommandType.StoredProcedure
cmdSelect.Parameters.Add( "@EmailAddress", txtEmail.Text )
conMyData.Open()
cmdSelect.ExecuteNonQuery()
dtrLogin = cmdSelect.ExecuteReader()
'catch any exceptions that might be thrown
Catch e as Exception
Response.Write("An Error Occurred: " & e.toString())
'clean up and close resources
Finally
conMyData.Close()
End Try

if dtrLogin.HasRows then
objMailMessage = New MailMessage
objMailMessage.To = txtEmail.Text
objMailMessage.Subject = "IMS Login Request"
objMailMessage.Body = "Hello " & dtrLogin("Login") & "," & vbCrlf
objMailMessage.Body = objMailMessage.Body & "Your password to enter the IMS system is: " & dtrLogin("Password") & vbCrlf & vbCrlf
objMailMessage.Body = objMailMessage.Body & "This is an automatic notification from the IMS system, please do NOT reply to this e-mail!" & vbCrlf & "Thank You"
SmtpMail.Send(objMailMessage)

lblMessage.Text = "E-mail Sent Successfully - Thank You!"
else
lblMessage.Text = "E-mail address not found in database!"
end if

End Sub


</script>


Basically it pulls the password and sends it as an email to the end user..but it errors right at Catch e as Exception...

saying:
Compiler Error Message: BC30616: Variable 'e' hides a variable in an enclosing block.

Source Error:



Line 29: dtrLogin = cmdSelect.ExecuteReader()
Line 30: 'catch any exceptions that might be thrown
Line 31: Catch e as Exception
Line 32: Response.Write("An Error Occurred: " & e.toString())
Line 33: 'clean up and close resources



Am I doing something wrong before that? Can anyone verify my code as well...I'm rather new with this .net..not sure if I called the sproc correctly and whether or not I need to apply ExecuteReader.
I called executereader so that it stores the data from the returned procedure so i can put it in the email.

Thanks,
jon



Jon
www.web-impulse.com

Can you dig it: http://www.thecenturoncompany.com/jhermiz/blog/

DustinMichaels
Constraint Violating Yak Guru

464 Posts

Posted - 2004-09-15 : 16:16:05
Do you have a variable called e at class scope?

I think this is a scope error.

Dustin Michael
Go to Top of Page

jhermiz

3564 Posts

Posted - 2004-09-15 : 16:18:32
quote:
Originally posted by DustinMichaels

Do you have a variable called e at class scope?

I think this is a scope error.

Dustin Michael



Well the thing is its an exception...Catch e as Exception is the declaration isnt it ? If not how come my original login page works..
check this from my original login page:


<%@ Page Language="VB" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>

<script runat="server">

Sub Page_Load
Dim strLinkPath As String


If Not IsPostBack Then
strLinkPath = String.Format( "Register/Register.aspx?ReturnUrl={0}", _
Request.Params( "ReturnUrl" ) )
lnkRegister.NavigateUrl = String.Format( strLinkPath )
End If
End Sub

Sub Button_Click( s As Object, e As EventArgs )
lblMessage.Text = ""
If IsValid Then
If DBAuthenticate( txtUsername.Text, txtPassword.Text ) > 0 Then
FormsAuthentication.RedirectFromLoginPage( txtUsername.Text, False )
End If
End If
End Sub

Function DBAuthenticate( strUsername As String, strPassword As String ) As Integer
Dim conMyData As SqlConnection
Dim cmdSelect As SqlCommand
Dim parmReturnValue As SqlParameter
Dim intResult As Integer

'try and make a connection
Try
conMyData = New SqlConnection( ConfigurationSettings.AppSettings("strConn") )
cmdSelect = New SqlCommand( "DBAuthenticate", conMyData )
cmdSelect.CommandType = CommandType.StoredProcedure
parmReturnValue = cmdSelect.Parameters.Add( "RETURN_VALUE", SqlDbType.Int )
parmReturnValue.Direction = ParameterDirection.ReturnValue
cmdSelect.Parameters.Add( "@username", strUsername )
cmdSelect.Parameters.Add( "@password", strPassword )
conMyData.Open()
cmdSelect.ExecuteNonQuery()
intResult = cmdSelect.Parameters( "RETURN_VALUE" ).Value
'catch any exceptions that might be thrown
Catch e as Exception
Response.Write("An Error Occurred: " & e.toString())
'clean up and close resources
Finally
conMyData.Close()
End Try

If intResult < 0 Then
If intResult = -1 Then
lblMessage.Text = "Username Not Registered!"
Else
lblMessage.Text = "Invalid Password!"
End If
End If
Return intResult
End Function

</script>


???

Jon
www.web-impulse.com

Can you dig it: http://www.thecenturoncompany.com/jhermiz/blog/
Go to Top of Page

DustinMichaels
Constraint Violating Yak Guru

464 Posts

Posted - 2004-09-15 : 16:24:00
I see the problem now.

The function Button_Click passes in a variable called e.

In your try catch block your using a variable called e.

Try using this code...


<%@ Page Language="VB" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<%@ import Namespace="System.Web.Mail" %>

<script runat="server">


Sub Button_Click(sender As Object, e As EventArgs)
Dim objMailMessage As MailMessage
Dim conMyData As SqlConnection
Dim cmdSelect As SqlCommand
Dim dtrLogin As SqlDataReader

If txtEmail.Text = "" Then
lblMessage.Text = "Please enter your e-mail address!"
exit sub
End if

'get information from Login table
'try and make a connection
Try
conMyData = New SqlConnection( ConfigurationSettings.AppSettings("strConn") )
cmdSelect = New SqlCommand( "select_login_info_forgot_pwd", conMyData )
cmdSelect.CommandType = CommandType.StoredProcedure
cmdSelect.Parameters.Add( "@EmailAddress", txtEmail.Text )
conMyData.Open()
cmdSelect.ExecuteNonQuery()
dtrLogin = cmdSelect.ExecuteReader()
'catch any exceptions that might be thrown
Catch ex as Exception
Response.Write("An Error Occurred: " & ex.toString())
'clean up and close resources
Finally
conMyData.Close()
End Try

if dtrLogin.HasRows then
objMailMessage = New MailMessage
objMailMessage.To = txtEmail.Text
objMailMessage.Subject = "IMS Login Request"
objMailMessage.Body = "Hello " & dtrLogin("Login") & "," & vbCrlf
objMailMessage.Body = objMailMessage.Body & "Your password to enter the IMS system is: " & dtrLogin("Password") & vbCrlf & vbCrlf
objMailMessage.Body = objMailMessage.Body & "This is an automatic notification from the IMS system, please do NOT reply to this e-mail!" & vbCrlf & "Thank You"
SmtpMail.Send(objMailMessage)

lblMessage.Text = "E-mail Sent Successfully - Thank You!"
else
lblMessage.Text = "E-mail address not found in database!"
end if

End Sub


</script>



Go to Top of Page

jhermiz

3564 Posts

Posted - 2004-09-15 : 16:31:31
Great eye...totally over looked that Dustin.

Hey Dustin,

Now if I submit I get:

Exception Details: System.InvalidOperationException: Invalid attempt to HasRows when reader is closed.

Source Error:


Line 35: End Try
Line 36:
Line 37: if dtrLogin.HasRows then
Line 38: objMailMessage = New MailMessage
Line 39: objMailMessage.To = txtEmail.Text


This is my lack of understanding on when these readers are good and open...if I close my connection in the finally clause does that clause the dtrLogin reader ??

Jon
www.web-impulse.com

Can you dig it: http://www.thecenturoncompany.com/jhermiz/blog/
Go to Top of Page

DustinMichaels
Constraint Violating Yak Guru

464 Posts

Posted - 2004-09-15 : 16:35:29
You need to include where you checking to see if the reader has rows in the try-catch-finally block.

Right now it is closing the database connection before it gets to the part where it checks to see if the reader has rows.
Go to Top of Page

jhermiz

3564 Posts

Posted - 2004-09-15 : 16:36:50
quote:
Originally posted by DustinMichaels

You need to include where you checking to see if the reader has rows in the try-catch-finally block.

Right now it is closing the database connection before it gets to the part where it checks to see if the reader has rows.



but isnt that bad practice? I thought it in theory you should grab the data..and close connection immediately..maybe it is mroe different for online apps ?

Jon
www.web-impulse.com

Can you dig it: http://www.thecenturoncompany.com/jhermiz/blog/
Go to Top of Page

DustinMichaels
Constraint Violating Yak Guru

464 Posts

Posted - 2004-09-15 : 16:41:40
The reader needs an active database connection to pull the data. If you close the database connection then the reader cannot get at the data. Once your done accessing the reader you should try to close the database connection immediately.
Go to Top of Page

jhermiz

3564 Posts

Posted - 2004-09-15 : 16:45:01
:(

THey just dont stop...
An Error Occurred: System.InvalidOperationException: Invalid attempt to read when no data is present. at System.Data.SqlClient.SqlDataReader.PrepareRecord(Int32 i) at System.Data.SqlClient.SqlDataReader.GetValue(Int32 i) at System.Data.SqlClient.SqlDataReader.get_Item(String name) at ASP.forgotpwd_aspx.Button_Click(Object sender, EventArgs e) in C:\Inetpub\wwwroot\IMS\forgotpwd.aspx:line 35

Jon
www.web-impulse.com

Can you dig it: http://www.thecenturoncompany.com/jhermiz/blog/
Go to Top of Page

DustinMichaels
Constraint Violating Yak Guru

464 Posts

Posted - 2004-09-15 : 16:52:00
You need to open the reader before you can access its data.

You can replace the line

if dtrLogin.HasRows then


with

if dtrLogin.Read() then
Go to Top of Page

jhermiz

3564 Posts

Posted - 2004-09-15 : 17:00:27
Dustin,

I want to thank you for being patient and helpful, my code has ran and it looks to be complete...now I must talk to the admins and figure out why the email was never sent :)

God Bless,



Jon
www.web-impulse.com

Can you dig it: http://www.thecenturoncompany.com/jhermiz/blog/
Go to Top of Page

DustinMichaels
Constraint Violating Yak Guru

464 Posts

Posted - 2004-09-15 : 18:08:55
Your welcome.

Good luck with your asp.net career.

Dustin Michaels
Go to Top of Page
   

- Advertisement -