Re: Server Error: Object reference not set
- From: SAL <SAL@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Fri, 6 Oct 2006 13:05:02 -0700
Thanks Baski. I will give it a try.
"Baski" wrote:
.
"SAL" <SAL@discYou are using uploadFile control to access your uploaded
file, you should
use Request.Files as shown below
HttpFileCollection HttpFiles = Request.Files;
for (int i = 0; i < HttpFiles.Count; i ++)
{
HttpPostedFile _HttpPosted = HttpFiles[i];
if ( _HttpPosted.ContentLength > 0 )
{
string lineText = string.Empty;
using (StreamReader sr = new StreamReader(_HttpPosted.InputStream))
{
//here you can use the stream directly and process the file, without saving
it to the server //or save it if that's your rquirement.
while (sr.Peek() >= 0)
{
lineText = sr.ReadLine();
}
}
}
}
Thanks
baski
ussions.microsoft.com> wrote in message
news:3728389F-8ACD-4F30-908F-13EB6F50C554@xxxxxxxxxxxxxxxx
I am getting the following ERROR in my WebApp on line 30:
Server Error in '/TestWebApp' Application.
--------------------------------------------------------------------------------
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about
the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set
to an instance of an object.
Source Error:
Line 28: public void Upload_click(object sender, System.EventArgs e)
Line 29: {
Line 30: if (uploadFile.PostedFile != null)
Line 31: {
Line 32: string test = uploadFile.PostedFile.FileName;
Source File: c:\documents and settings\my
documents\myprojects\web\project\testwebapp\test.aspx.cs Line: 30
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an
object.]
TestWebApp.test.Upload_click(Object sender, EventArgs e) in c:\documents
and settings\my
documents\myprojects\web\project\testwebapp\test.aspx.cs:30
System.Web.UI.WebControls.Button.OnClick(EventArgs e)
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String
eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
System.Web.UI.Page.ProcessRequestMain()
I thought I was doing something wrong in my WebApp, so I created a brand
new
solution to test with I get the same error. Below is all the code for my
test. Basically, all this webpage has is a HTML File Field Control, and a
Web Forms Button Control on it. After you click the File Field and select
your file, then you click the Button which calls Upload_click. When it
reads
if (uploadFile.PostedFile != null) is when I get the error.
I'm new with ASP.net and this seems pretty straight-forward, but I can't
seem to see what I am doing wrong. Can someone tell me what I am doing
wrong?
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace TestWebApp
{
/// <summary>
/// Summary description for test.
/// </summary>
public class test : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Upload;
protected System.Web.UI.HtmlControls.HtmlInputFile uploadFile;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}
public void Upload_click(object sender, System.EventArgs e)
{
if (uploadFile.PostedFile != null)
{
string test = uploadFile.PostedFile.FileName;
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
<%@ Page language="c#" Codebehind="test.aspx.cs" AutoEventWireup="false"
Inherits="TestWebApp.test" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>test</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<INPUT id="uploadFile" type="file">
<asp:Button id="Upload" OnClick="Upload_click" runat="server"
Text="Upload"></asp:Button>
</form>
</body>
</HTML>
Thanks,
- References:
- Server Error: Object reference not set
- From: SAL
- Re: Server Error: Object reference not set
- From: Baski
- Server Error: Object reference not set
- Prev by Date: Re: Dynamic Controls
- Next by Date: ASP 1.1 - StandardOutput from console application
- Previous by thread: Re: Server Error: Object reference not set
- Next by thread: question about inheritance
- Index(es):