i am able to upload files to database and also i am able to get files from database but on clicking download file i am getting error---->
Object reference not set to an instance of an object.
and here is DownloadHandler.ashx code which i am using
<%@ WebHandler Language="VB" Class="DownloadHandler" %>
Imports System
Imports System.Web
Imports System.Data
Imports System.IO
Imports System.Collections.Generic
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Collections
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports DownloadHandler
Imports System.Linq
Public Class DownloadHandler
Implements IHttpHandler
Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
Dim fileId As Integer = 0
Dim fileName As String = ""
Dim num As String = ""
If context.Request.QueryString("Id") IsNot Nothing Then
fileId = Convert.ToInt32(context.Request.QueryString("Id").ToString())
End If
If context.Request.QueryString("filename") IsNot Nothing Then
fileName = context.Request.QueryString("filename").ToString()
End If
If context.Request.QueryString("NUM") IsNot Nothing Then
num = context.Request.QueryString("NUM").ToString()
End If
Dim gf As New GetFile()
gf.FileName = fileName
gf.Id = fileId
gf.NUM = num
Dim ds As DataSet = gf.GetFileFromDB()
Dim bytes As [Byte]() = DirectCast(ds.Tables("FilesData").Rows(0).Item(2), [Byte]())
Try
context.Response.Clear()
context.Response.AddHeader("Content-Disposition", "inline;attachment; filename=""" & fileName & """")
context.Response.AddHeader("Content-Length", bytes.Length.ToString())
context.Response.ContentType = "application/octet-stream"
context.Response.BinaryWrite(bytes)
context.Response.Flush()
Catch ex As Exception
context.Response.ContentType = "text/plain"
context.Response.Write(ex.Message)
Finally
context.Response.[End]()
End Try
End Sub
Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
Return True
End Get
End Property
End Class
I reviewed your code and i feel it is good. But i can suggest you to print the stacktrace or inner exception detail from where the exception throwed originally. Just write it in the response. I think it will be a easy way to reach the black line.
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 45: Dim ds As DataSet = gf.GetFileFromDB()
Line 46:
Line 47: Dim bytes As [Byte]() = DirectCast(ds.Tables("FilesData").Rows(0).Item(2), [Byte]()) Line 48: Try
Line 49: context.Response.Clear()
Source File: C:\Documents and Settings\INTERNS\My Documents\Visual Studio 2005\WebSites\WebSite7\DownloadHandler.ashx Line:
47
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
DownloadHandler.ProcessRequest(HttpContext context) in C:\Documents and Settings\INTERNS\My Documents\Visual Studio 2005\WebSites\WebSite7\DownloadHandler.ashx:47
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +391
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +76
hi, ensure are you referring proper index (i.e 2) to read byte array value, and check whether it has proper value. anyhow you can avoid this error by checking for nothing
If ds.Tables("FilesData").Rows(0).Item(2) IsNot Nothing Then
Dim bytes As [Byte]() = DirectCast(ds.Tables("FilesData").Rows(0).Item(2), [Byte]())
Try
context.Response.Clear()
context.Response.AddHeader("Content-Disposition", "inline;attachment; filename=""" & fileName & """")
context.Response.AddHeader("Content-Length", bytes.Length.ToString())
context.Response.ContentType = "application/octet-stream"
context.Response.BinaryWrite(bytes)
context.Response.Flush()
Catch ex As Exception
context.Response.ContentType = "text/plain"
context.Response.Write(ex.Message)
Finally
context.Response.[End]()
End Try
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.IndexOutOfRangeException: There is no row at position 0.
Source Error:
Line 44:
Line 45: Dim ds As DataSet = gf.GetFileFromDB()
Line 46: If ds.Tables(0).Rows(0).Item("Data") IsNot Nothing Then Line 47: Dim bytes As [Byte]() = DirectCast(ds.Tables(0).Rows(0).Item("Data"), [Byte]())
Line 48: Try
Source File: C:\Documents and Settings\INTERNS\My Documents\Visual Studio 2005\WebSites\WebSite7\DownloadHandler.ashx Line:
46
Stack Trace:
[IndexOutOfRangeException: There is no row at position 0.]
System.Data.RBTree`1.GetNodeByIndex(Int32 userIndex) +115
System.Data.RBTree`1.get_Item(Int32 index) +20
System.Data.DataRowCollection.get_Item(Int32 index) +12
DownloadHandler.ProcessRequest(HttpContext context) in C:\Documents and Settings\INTERNS\My Documents\Visual Studio 2005\WebSites\WebSite7\DownloadHandler.ashx:46
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +391
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +76
its still not working. it is still giving an error saying there is no row at position 0..
this is the getfile code
Imports Microsoft.VisualBasic
Imports System.Linq
Imports System.Web
Imports System.Data
Imports System.Data.SqlClient
Public Class GetFile
Public Property Id() As Integer
Get
Return m_Id
End Get
Set(ByVal value As Integer)
m_Id = Value
End Set
End Property
Private m_Id As Integer
Public Property FileName() As String
Get
Return m_FileName
End Get
Set(ByVal value As String)
m_FileName = Value
End Set
End Property
Private m_FileName As String
Public Property NUM() As String
Get
Return m_NUM
End Get
Set(ByVal value As String)
m_NUM = value
End Set
End Property
Private m_NUM As String
Public Function GetFileFromDB() As DataSet
Dim ds As New DataSet()
Dim sda As New SqlDataAdapter()
Using conn As New SqlConnection("Data Source=10.179.26.2;Initial Catalog=PPEP;user id=scala;pwd=scala")
Dim cmd As New SqlCommand()
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "GetFileProc"
cmd.Connection = conn
cmd.Parameters.AddWithValue("@Id", Id)
cmd.Parameters.AddWithValue("@FileName", FileName)
cmd.Parameters.AddWithValue("@NUM", NUM)
Try
conn.Open()
sda.SelectCommand = cmd
sda.Fill(ds)
Return ds
Catch ex As Exception
Return Nothing
Finally
sda.Dispose()
conn.Close()
cmd.Dispose()
conn.Dispose()
End Try
End Using
End Function
End Class
As you said there is no row at position 0. I suspect that there is no data within DataTable. You could check the SQL statement, to see if there is no data return from SQL Server.
We focus on various troubleshooting plan and solution on IIS web platform and distributed applications. Please contact us at:
priya_ami
Member
29 Points
91 Posts
error in DownloadHandler.ashx
Jul 04, 2012 11:35 AM|LINK
i am able to upload files to database and also i am able to get files from database but on clicking download file i am getting error---->
Object reference not set to an instance of an object.
and here is DownloadHandler.ashx code which i am using
<%@ WebHandler Language="VB" Class="DownloadHandler" %> Imports System Imports System.Web Imports System.Data Imports System.IO Imports System.Collections.Generic Imports System.Data.SqlClient Imports System.Configuration Imports System.Collections Imports System.Web.Security Imports System.Web.UI Imports System.Web.UI.WebControls Imports System.Web.UI.WebControls.WebParts Imports System.Web.UI.HtmlControls Imports DownloadHandler Imports System.Linq Public Class DownloadHandler Implements IHttpHandler Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest Dim fileId As Integer = 0 Dim fileName As String = "" Dim num As String = "" If context.Request.QueryString("Id") IsNot Nothing Then fileId = Convert.ToInt32(context.Request.QueryString("Id").ToString()) End If If context.Request.QueryString("filename") IsNot Nothing Then fileName = context.Request.QueryString("filename").ToString() End If If context.Request.QueryString("NUM") IsNot Nothing Then num = context.Request.QueryString("NUM").ToString() End If Dim gf As New GetFile() gf.FileName = fileName gf.Id = fileId gf.NUM = num Dim ds As DataSet = gf.GetFileFromDB() Dim bytes As [Byte]() = DirectCast(ds.Tables("FilesData").Rows(0).Item(2), [Byte]()) Try context.Response.Clear() context.Response.AddHeader("Content-Disposition", "inline;attachment; filename=""" & fileName & """") context.Response.AddHeader("Content-Length", bytes.Length.ToString()) context.Response.ContentType = "application/octet-stream" context.Response.BinaryWrite(bytes) context.Response.Flush() Catch ex As Exception context.Response.ContentType = "text/plain" context.Response.Write(ex.Message) Finally context.Response.[End]() End Try End Sub Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable Get Return True End Get End Property End Classplease help me...
hariharakris...
Member
76 Points
32 Posts
Re: error in DownloadHandler.ashx
Jul 04, 2012 11:52 AM|LINK
Hi Priya,
I reviewed your code and i feel it is good. But i can suggest you to print the stacktrace or inner exception detail from where the exception throwed originally. Just write it in the response. I think it will be a easy way to reach the black line.
priya_ami
Member
29 Points
91 Posts
Re: error in DownloadHandler.ashx
Jul 04, 2012 11:55 AM|LINK
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 45: Dim ds As DataSet = gf.GetFileFromDB() Line 46: Line 47: Dim bytes As [Byte]() = DirectCast(ds.Tables("FilesData").Rows(0).Item(2), [Byte]()) Line 48: Try Line 49: context.Response.Clear()Source File: C:\Documents and Settings\INTERNS\My Documents\Visual Studio 2005\WebSites\WebSite7\DownloadHandler.ashx Line: 47
Stack Trace:
karthicks
All-Star
31378 Points
5422 Posts
Re: error in DownloadHandler.ashx
Jul 04, 2012 12:00 PM|LINK
hi, ensure are you referring proper index (i.e 2) to read byte array value, and check whether it has proper value. anyhow you can avoid this error by checking for nothing
If ds.Tables("FilesData").Rows(0).Item(2) IsNot Nothing Then
Dim bytes As [Byte]() = DirectCast(ds.Tables("FilesData").Rows(0).Item(2), [Byte]())
Try
context.Response.Clear()
context.Response.AddHeader("Content-Disposition", "inline;attachment; filename=""" & fileName & """")
context.Response.AddHeader("Content-Length", bytes.Length.ToString())
context.Response.ContentType = "application/octet-stream"
context.Response.BinaryWrite(bytes)
context.Response.Flush()
Catch ex As Exception
context.Response.ContentType = "text/plain"
context.Response.Write(ex.Message)
Finally
context.Response.[End]()
End Try
End If
Karthick S
hariharakris...
Member
76 Points
32 Posts
Re: error in DownloadHandler.ashx
Jul 04, 2012 12:02 PM|LINK
Yes, I guessed so.
Can you please ensure that the
1. Row(0).Item(2) has records in it. and
2. the table "FilesData" is present in the Dataset and
3. the downloaded file has the actual content that you have uploaded?
priya_ami
Member
29 Points
91 Posts
Re: error in DownloadHandler.ashx
Jul 04, 2012 12:07 PM|LINK
i am now getting this error---->
There is no row at position 0.
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.IndexOutOfRangeException: There is no row at position 0.
Source Error:
Line 44: Line 45: Dim ds As DataSet = gf.GetFileFromDB() Line 46: If ds.Tables(0).Rows(0).Item("Data") IsNot Nothing Then Line 47: Dim bytes As [Byte]() = DirectCast(ds.Tables(0).Rows(0).Item("Data"), [Byte]()) Line 48: TrySource File: C:\Documents and Settings\INTERNS\My Documents\Visual Studio 2005\WebSites\WebSite7\DownloadHandler.ashx Line: 46
Stack Trace:
hariharakris...
Member
76 Points
32 Posts
Re: error in DownloadHandler.ashx
Jul 04, 2012 12:09 PM|LINK
So check your GetFile() whether it gives a file or not! just have a tea. good luck!
karthicks
All-Star
31378 Points
5422 Posts
Re: error in DownloadHandler.ashx
Jul 04, 2012 12:21 PM|LINK
hi, i hope the below code is returning empty dataset, so check GetFileFromDB() method
Dim ds As DataSet = gf.GetFileFromDB() and change if condition like below
If ds.Tables.Count >0 Then
If ds.Tables("FilesData").Rows(0).Item(2) IsNot Nothing Then
Dim bytes As [Byte]() = DirectCast(ds.Tables("FilesData").Rows(0).Item(2), [Byte]())
Try
context.Response.Clear()
context.Response.AddHeader("Content-Disposition", "inline;attachment; filename=""" & fileName & """")
context.Response.AddHeader("Content-Length", bytes.Length.ToString())
context.Response.ContentType = "application/octet-stream"
context.Response.BinaryWrite(bytes)
context.Response.Flush()
Catch ex As Exception
context.Response.ContentType = "text/plain"
context.Response.Write(ex.Message)
Finally
context.Response.[End]()
End Try
End If
End If
mark my both the replies as answered one
Karthick S
priya_ami
Member
29 Points
91 Posts
Re: error in DownloadHandler.ashx
Jul 05, 2012 05:59 AM|LINK
its still not working. it is still giving an error saying there is no row at position 0..
this is the getfile code
Imports Microsoft.VisualBasic Imports System.Linq Imports System.Web Imports System.Data Imports System.Data.SqlClient Public Class GetFile Public Property Id() As Integer Get Return m_Id End Get Set(ByVal value As Integer) m_Id = Value End Set End Property Private m_Id As Integer Public Property FileName() As String Get Return m_FileName End Get Set(ByVal value As String) m_FileName = Value End Set End Property Private m_FileName As String Public Property NUM() As String Get Return m_NUM End Get Set(ByVal value As String) m_NUM = value End Set End Property Private m_NUM As String Public Function GetFileFromDB() As DataSet Dim ds As New DataSet() Dim sda As New SqlDataAdapter() Using conn As New SqlConnection("Data Source=10.179.26.2;Initial Catalog=PPEP;user id=scala;pwd=scala") Dim cmd As New SqlCommand() cmd.CommandType = CommandType.StoredProcedure cmd.CommandText = "GetFileProc" cmd.Connection = conn cmd.Parameters.AddWithValue("@Id", Id) cmd.Parameters.AddWithValue("@FileName", FileName) cmd.Parameters.AddWithValue("@NUM", NUM) Try conn.Open() sda.SelectCommand = cmd sda.Fill(ds) Return ds Catch ex As Exception Return Nothing Finally sda.Dispose() conn.Close() cmd.Dispose() conn.Dispose() End Try End Using End Function End Classgtscdsi
Member
250 Points
50 Posts
Re: error in DownloadHandler.ashx
Jul 12, 2012 09:46 AM|LINK
Hi Priya,
As you said there is no row at position 0. I suspect that there is no data within DataTable. You could check the SQL statement, to see if there is no data return from SQL Server.
http://blogs.msdn.com/b/asiatech/