Saturday, March 24, 2012

object reference not set to an instance of an object

I keep on getting this error

Server Error in '/' Application.

Object reference not set to an instance of an object.

Description:Anunhandled exception occurred during the execution of the current webrequest. Please review the stack trace for more information about theerror and where it originated in the code.
Exception Details:System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:

Line 30: dim stremail as string = "markmil2002@dotnet.itags.org.yahoo.com"
Line 31:
Line 32: intid = objpass.retrieveuserid(tbEmail.text)
Line 33:
Line 34: If intID = 0 Then


Source File:c:\inetpub\wwwroot\Forgot.aspx Line:32
Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.]
ASP.forgot_aspx.retrievePass(Object sender, EventArgs e) in c:\inetpub\wwwroot\Forgot.aspx:32
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +57
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain() +1277



Version Information: Microsoft .NET Framework Version:1.1.4322.573; ASP.NET Version:1.1.4322.573
AndI know it means that tbemail isnt declared, but it is, I have arunat="server" in the tag as shown below in my code. So am Iwrong about something?

<Code>
<%@dotnet.itags.org. Page Language="vb" debug="true" %>
<%@dotnet.itags.org. Register TagPrefix="LCPL" Tagname="Header" src="http://pics.10026.com/?src=header.ascx" %>
<%@dotnet.itags.org. Register TagPrefix="LCPL" Tagname="Footer" src="http://pics.10026.com/?src=Footer.ascx" %>
<%@dotnet.itags.org. Import Namespace="System" %>
<%@dotnet.itags.org. Import Namespace="System.data" %>
<%@dotnet.itags.org. Import Namespace="System.data.oledb" %>

<script runat="server">
Sub Page_Load(sender as object, e as eventargs)


End SUb

Private Function sendnonMail(byVal fromAccount As String, ByVal toAccount as String, byVal title as String, byVal msg As String)
Dim mm as New system.Web.Mail.MailMessage()
mm.to = toAccount

mm.From = FRomAccount
mm.body = msg
mm.bodyFormat = System.Web.Mail.MailFormat.text
System.Web.Mail.SmtpMail.SmtpServer = "smtp-server.hawaii.rr.com"
System.Web.Mail.SmtpMail.Send(mm)

End Function

Sub retrievePass(sender as object, e as eventargs)
dim objpassworddetails as lcpl.passworddetails
dim objPass as lcpl.passwordretrieve
dim intid as integer
dim stremail as string = "markmil2002@dotnet.itags.org.yahoo.com"

intid = objpass.retrieveuserid(tbEmail.text)

If intID = 0 Then
lblMessage.Text = "<font color='red'>Invalid email address</Font>"
Else
dim strusername as string = objpass.retrieveusername(intID)
dim strPassword as string = objPass.retrievepassword(strusername, intid)

Dim toAccount as String
Dim FromAccount as String
Dim Title As String
Dim msg as String

Title = "username and Password "
msg = "Username: " & strusername & " Password: " & strpassword
fromAccount = "lcplunderground@dotnet.itags.org.gumballmail.com"
toAccount = tbEmail.Text


Call sendnonmail(fromAccount, toAccount, title, msg)

response.redirect("login.aspx?pass=1&email=" & tbemail.text)

End If

End Sub
</script>

<HTML>
<HEAD>
<LINK rel="stylesheet" type="text/css" name="Style" href="http://links.10026.com/?link=css.css">
<TITLE>
</TITLE>
</HEAD>
<BODY>
<LCPL:Header runat="server" />
<form runat = "server">
<table align="center" colspan="2">
<tr>
<td colspan="2" align="Center"><asp:label id="lblMEssage" runat="server" />
<tr>
<td><asp:label id="lblemail" text="E-mail address" runat="server" />
</td>
<td><asp:textbox id="tbEmail" text="" runat="server" />
</td>
</tr>
<tr>
<td colspan="2" Align="right">
<asp:button id="btnsubmit" Text="Retrieve Password" onclick="retrievePass" runat="server" />
</td>
</tr>
</table>
</Form>

</BODY>
<LCPL:footer runat="server" />
</HTML>
</code>
And here is my code behind
<Code>
Imports system
Imports System.Data
Imports System.Data.Oledb

Namespace LCPL
Public class passwordretrieve
PrivateobjConn as new oledbConnection("Provider=sqloledb;DataSource=MICROSOF-54052B\cpl;Initial Catalog=user;User Id=sa;Password=")

Public Function retrieveuserid(email as String) as integer
dim intid as integer
Dim objcmd as new oledbCommand("SpgetUserID", objConn)
objcmd.CommandType = Commandtype.StoredProcedure

Dim objParam as New oledbParameter("@dotnet.itags.org.email", oledbtype.BigInt)
objParam.Value = email
ObjCmd.Parameters.Add(objParam)


Try
objConn.Open
intID = objCmd.ExecuteScalar
objConn.Close
Catch e as Exception
throw e
End Try

if intid.ToString = "" Then
return 0
end if
Return intid


End Function


Public Function retrieveusername(userid as string) as string
dim strusername as string
Dim objcmd as new oledbCommand("SpgetUserID", objConn)
objcmd.CommandType = Commandtype.StoredProcedure

Dim objParam as New oledbParameter("@dotnet.itags.org.userid", oledbtype.varChar)
objParam.Value = userid
ObjCmd.Parameters.Add(objParam)


Try
objConn.Open
strusername = objCmd.ExecuteScalar
objConn.Close
Catch e as Exception
throw e
End Try


Return strusername


End Function

Public Function retrievepassword(username as string, userid as string) as string
dim strpassword as string
Dim objcmd as new oledbCommand("SpgetUserID", objConn)
objcmd.CommandType = Commandtype.StoredProcedure

Dim objParam as New oledbParameter("@dotnet.itags.org.userid", oledbtype.varChar)
objParam.Value = userid
ObjCmd.Parameters.Add(objParam)

objParam = new oledbParameter("@dotnet.itags.org.username", oledbType.varChar)
objParam.Value = username
objcmd.Parameters.add(objParam)

Try
objConn.Open
strpassword = objCmd.ExecuteScalar
objConn.Close
Catch e as Exception
throw e
End Try


Return strpassword


End Function

End Class
end namespace
</code>

markmil2002 wrote:

AndI know it means that tbemail isnt declared, but it is, I have arunat="server" in the tag as shown below in my code. So am Iwrong about something?

That is not the object that is the problem. Have a look at this part of your code:
dim objpassworddetails as lcpl.passworddetails
dim objPass as lcpl.passwordretrieve
dim intid as integer
dim stremail as string = "markmil2002@.yahoo.com"
intid = objpass.retrieveuserid(tbEmail.text)
You have defined objPass as being of type lcpl.passwordretrieve, but youhave never actually created an instance of this type. To fix this, youwill need to add a line similar to the following:
dim objpassworddetails as lcpl.passworddetails
dim objPass as lcpl.passwordretrieve
objPass = New lcpl.passwordretrieve()
dim intid as integer
dim stremail as string = "markmil2002@.yahoo.com"
intid = objpass.retrieveuserid(tbEmail.text)
That extra line may work as is, or it may not. It depends on how yourpasswordretrieve class is constructed. If you don't know what I mean bythat, please ask.


Well I tried your suggestion, and although it made sense to me, and sounded like a good idea, it did not work, so back to the drawing board I guess, lol.
For the sake of debugging, try this code:
dim objpassworddetails as lcpl.passworddetails
dim objPass as lcpl.passwordretrieve
objPass = New lcpl.passwordretrieve()
dim intid as integer
dim stremail as string = "markmil2002@.yahoo.com"
If objPass Is Nothing Then
Response.Write("objPass is Nothing.")
Response.End()
Else If tbEmail Is Nothing Then
Response.Write("tbEmail is Nothing.")
Respoonse.End()
Else
Response.Write("objPass and tbEmail both valid.")
Try
intid = objpass.retrieveuserid(tbEmail.text)
Response.Write("Assignment of intid succeeded.")
Catch
Response.Write("Assignment of intid failed.")
End Try
Response.End()
End If
Request this page through your browser, and see which response you get. You can then pinpoint the problem.

0 comments:

Post a Comment