redirect on filename is failing

N

news.microsoft.com

Hello All,

im am new to ASP.NET, so bare with me.

i have an application that i am building in stages.

stage1 user picks data required
stage2 file is written to a virutal directory
Stage3 the file is then saved by the user to their hard disk

i am currently in stage 3, i am using a httpresponse.redirect method, this
is throwing up the following error and i don't know how to fix it, can some
body please point me in the right direction? the filename that is am passing
it is as follows data.csv (a better naming convention will be in place when
i can get the file to be downloaded by the user)

Many thanks
Simon Whale

Server Error in '/datacounts' 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 145: sw = Nothing
Line 146:
Line 147: rd.Redirect(file)
Line 148: dr.Close()
Line 149: cn.Close()


Source File: c:\inetpub\wwwroot\datacounts\WebForm1.aspx.vb Line: 147

Stack Trace:


[NullReferenceException: Object reference not set to an instance of an
object.]
datacounts.WebForm1.Button1_Click(Object sender, EventArgs e) in
c:\inetpub\wwwroot\datacounts\WebForm1.aspx.vb:147
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()
 
K

Karl Seguin

couple things

(a) why are you setting sw = nothing, this isn't necesasry
(b) rd is clearly null <-- source of your error...but without knowing
where/what rd is, can't tell you more than that
(c) if you redirect on line 147, the cleanup of line 148 and 149 won't
happen

karl



--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
 
N

news.microsoft.com

thanks Karl,

to answer your question i have included the subrountine that i use for the
export data

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim rd As HttpResponse

Dim file As String = Replace("data" & Format(Now(), "long Date") &
".csv", " ", "#")

Dim location As String = Server.MapPath(file)

Dim sw As StreamWriter = New StreamWriter(location)

cn = New SqlConnection(sqlCon)

Try

cn.Open()

Catch ex As Exception

Console.WriteLine(ex.Message)

End Try

Dim item As ListItem

Dim sql As String

Dim first As Boolean = True

'This Section will generate the SQL Script that will obtain counts for 1
or more

'postcode and return thr group results.

sql = "select top 75000 cli from millenium..outputs where post in ("

For Each item In listPostcodes.Items

If item.Selected Then

If first Then

sql = sql + "'" & item.Text & "'"

first = False

Else

sql = sql + ",'" & item.Text & "'"

End If

End If

Next

sql = sql + ")" '

'execute the select statement that was generated above

Dim cmd As New SqlCommand(sql, cn)

Dim dr As SqlDataReader = cmd.ExecuteReader

Do While dr.Read()

sw.WriteLine(dr.Item("cli"))

Loop

sw.Close()

sw = Nothing

rd.Redirect(file)

dr.Close()

cn.Close()

End Sub

Many thanks for your help

Simon

Karl Seguin said:
couple things

(a) why are you setting sw = nothing, this isn't necesasry
(b) rd is clearly null <-- source of your error...but without knowing
where/what rd is, can't tell you more than that
(c) if you redirect on line 147, the cleanup of line 148 and 149 won't
happen

karl



--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
news.microsoft.com said:
Hello All,

im am new to ASP.NET, so bare with me.

i have an application that i am building in stages.

stage1 user picks data required
stage2 file is written to a virutal directory
Stage3 the file is then saved by the user to their hard disk

i am currently in stage 3, i am using a httpresponse.redirect method,
this is throwing up the following error and i don't know how to fix it,
can some body please point me in the right direction? the filename that
is am passing it is as follows data.csv (a better naming convention will
be in place when i can get the file to be downloaded by the user)

Many thanks
Simon Whale

Server Error in '/datacounts' 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 145: sw = Nothing
Line 146:
Line 147: rd.Redirect(file)
Line 148: dr.Close()
Line 149: cn.Close()


Source File: c:\inetpub\wwwroot\datacounts\WebForm1.aspx.vb Line: 147

Stack Trace:


[NullReferenceException: Object reference not set to an instance of an
object.]
datacounts.WebForm1.Button1_Click(Object sender, EventArgs e) in
c:\inetpub\wwwroot\datacounts\WebForm1.aspx.vb:147
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()
 
K

Karl Seguin

All I needed to see was what rd was...

you have:
Dim rd As HttpResponse

never are you creating a new instance (rs = new HttpResponse) hence it is
obviously null and you get a null reference.

Of course, you can't simply create an HttpResponse. ASP.Net creates one for
you and you should use this one. The page exposes a Response
property....assuming your Button1_Click function resides on a codebehind,
simply do Response.Redirect

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
news.microsoft.com said:
thanks Karl,

to answer your question i have included the subrountine that i use for the
export data

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim rd As HttpResponse

Dim file As String = Replace("data" & Format(Now(), "long Date") &
".csv", " ", "#")

Dim location As String = Server.MapPath(file)

Dim sw As StreamWriter = New StreamWriter(location)

cn = New SqlConnection(sqlCon)

Try

cn.Open()

Catch ex As Exception

Console.WriteLine(ex.Message)

End Try

Dim item As ListItem

Dim sql As String

Dim first As Boolean = True

'This Section will generate the SQL Script that will obtain counts for
1 or more

'postcode and return thr group results.

sql = "select top 75000 cli from millenium..outputs where post in ("

For Each item In listPostcodes.Items

If item.Selected Then

If first Then

sql = sql + "'" & item.Text & "'"

first = False

Else

sql = sql + ",'" & item.Text & "'"

End If

End If

Next

sql = sql + ")" '

'execute the select statement that was generated above

Dim cmd As New SqlCommand(sql, cn)

Dim dr As SqlDataReader = cmd.ExecuteReader

Do While dr.Read()

sw.WriteLine(dr.Item("cli"))

Loop

sw.Close()

sw = Nothing

rd.Redirect(file)

dr.Close()

cn.Close()

End Sub

Many thanks for your help

Simon

Karl Seguin said:
couple things

(a) why are you setting sw = nothing, this isn't necesasry
(b) rd is clearly null <-- source of your error...but without knowing
where/what rd is, can't tell you more than that
(c) if you redirect on line 147, the cleanup of line 148 and 149 won't
happen

karl



--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
news.microsoft.com said:
Hello All,

im am new to ASP.NET, so bare with me.

i have an application that i am building in stages.

stage1 user picks data required
stage2 file is written to a virutal directory
Stage3 the file is then saved by the user to their hard disk

i am currently in stage 3, i am using a httpresponse.redirect method,
this is throwing up the following error and i don't know how to fix it,
can some body please point me in the right direction? the filename that
is am passing it is as follows data.csv (a better naming convention will
be in place when i can get the file to be downloaded by the user)

Many thanks
Simon Whale

Server Error in '/datacounts' 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 145: sw = Nothing
Line 146:
Line 147: rd.Redirect(file)
Line 148: dr.Close()
Line 149: cn.Close()


Source File: c:\inetpub\wwwroot\datacounts\WebForm1.aspx.vb Line: 147

Stack Trace:


[NullReferenceException: Object reference not set to an instance of an
object.]
datacounts.WebForm1.Button1_Click(Object sender, EventArgs e) in
c:\inetpub\wwwroot\datacounts\WebForm1.aspx.vb:147
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()
 
N

news.microsoft.com

thanks Karl

it works

Simon
Karl Seguin said:
All I needed to see was what rd was...

you have:
Dim rd As HttpResponse

never are you creating a new instance (rs = new HttpResponse) hence it is
obviously null and you get a null reference.

Of course, you can't simply create an HttpResponse. ASP.Net creates one
for you and you should use this one. The page exposes a Response
property....assuming your Button1_Click function resides on a codebehind,
simply do Response.Redirect

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
news.microsoft.com said:
thanks Karl,

to answer your question i have included the subrountine that i use for
the export data

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim rd As HttpResponse

Dim file As String = Replace("data" & Format(Now(), "long Date") &
".csv", " ", "#")

Dim location As String = Server.MapPath(file)

Dim sw As StreamWriter = New StreamWriter(location)

cn = New SqlConnection(sqlCon)

Try

cn.Open()

Catch ex As Exception

Console.WriteLine(ex.Message)

End Try

Dim item As ListItem

Dim sql As String

Dim first As Boolean = True

'This Section will generate the SQL Script that will obtain counts for
1 or more

'postcode and return thr group results.

sql = "select top 75000 cli from millenium..outputs where post in ("

For Each item In listPostcodes.Items

If item.Selected Then

If first Then

sql = sql + "'" & item.Text & "'"

first = False

Else

sql = sql + ",'" & item.Text & "'"

End If

End If

Next

sql = sql + ")" '

'execute the select statement that was generated above

Dim cmd As New SqlCommand(sql, cn)

Dim dr As SqlDataReader = cmd.ExecuteReader

Do While dr.Read()

sw.WriteLine(dr.Item("cli"))

Loop

sw.Close()

sw = Nothing

rd.Redirect(file)

dr.Close()

cn.Close()

End Sub

Many thanks for your help

Simon

Karl Seguin said:
couple things

(a) why are you setting sw = nothing, this isn't necesasry
(b) rd is clearly null <-- source of your error...but without knowing
where/what rd is, can't tell you more than that
(c) if you redirect on line 147, the cleanup of line 148 and 149 won't
happen

karl



--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
Hello All,

im am new to ASP.NET, so bare with me.

i have an application that i am building in stages.

stage1 user picks data required
stage2 file is written to a virutal directory
Stage3 the file is then saved by the user to their hard disk

i am currently in stage 3, i am using a httpresponse.redirect method,
this is throwing up the following error and i don't know how to fix it,
can some body please point me in the right direction? the filename that
is am passing it is as follows data.csv (a better naming convention
will be in place when i can get the file to be downloaded by the user)

Many thanks
Simon Whale

Server Error in '/datacounts' 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 145: sw = Nothing
Line 146:
Line 147: rd.Redirect(file)
Line 148: dr.Close()
Line 149: cn.Close()


Source File: c:\inetpub\wwwroot\datacounts\WebForm1.aspx.vb Line:
147

Stack Trace:


[NullReferenceException: Object reference not set to an instance of an
object.]
datacounts.WebForm1.Button1_Click(Object sender, EventArgs e) in
c:\inetpub\wwwroot\datacounts\WebForm1.aspx.vb:147
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()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top