Monday, March 26, 2012

Object reference not set to an instance of an object

I am getting an error object reference not set to an instance of an object, can anyone tell me what is causing this? I am new to asp.net and I am lost.

Thanks in advance

Code Behind:

Public Class addfile
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub
Protected WithEvents submit As System.Web.UI.WebControls.Button
Protected WithEvents upfile As System.Web.UI.HtmlControls.HtmlInputFile
Protected WithEvents Menu1 As ComponentArt.Web.UI.Menu

'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub submit_Command(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs) Handles submit.Command
If upfile.Value <> "" Then
Server.ScriptTimeout = 900
Dim i As Integer = 0
Dim Conn As New SqlConnection(ConfigurationSettings.AppSettings("*COMMENTED OUT**ConnectionString"))
Dim Cmd As New SqlCommand("INSERT INTO mj_documents (name, franchiseid) VALUES (@dotnet.itags.org.name, @dotnet.itags.org.fid); SELECT SCOPE_IDENTITY() AS id", Conn)
Dim dr As SqlDataReader
Cmd.Parameters.Add("@dotnet.itags.org.name", Right(upfile.PostedFile.FileName, Len(upfile.PostedFile.FileName) - InStrRev(upfile.PostedFile.FileName, "\")))
Cmd.Parameters.Add("@dotnet.itags.org.fid", Session("fid"))
Conn.Open()
dr = Cmd.ExecuteReader(Cmd.CloseConnection)
If dr.Read Then
upfile.PostedFile.SaveAs(ConfigurationSettings.AppSettings("docspot") & dr("id"))
End If
dr.Close()
Response.Redirect("index.aspx")
End If
End Sub
End Class

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.]
addfile.submit_Command(Object sender, CommandEventArgs e)
System.Web.UI.WebControls.Button.OnCommand(CommandEventArgs e) +110
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +116
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain() +1330Place your code in Try...catch blocks. Catch the exception and display ex.Message when the error occurs. Also, try stepping through your code to see on what line this error is occurring. It's hard to tell just by glancing at your code and a stack trace.

0 comments:

Post a Comment