Hey,
I have a database interaction file in vb. net where in I am connecting to database and I am inserting the image files in the SQL server 2005 database.
The Code works fine on my machine but when I uploaded the site , an error occured saying "Object Reference is not set to an instance of an object" at
SaveInspectionO bservationImage s(ByVal dt As DataTable)
The Code for this Function is as follows...
Public Function SaveInspectionO bservationImage s(ByVal dt As DataTable) As Boolean ' Here I am passing the Images which are in datatable.
Dim sqlImageInsertT ransaction As SqlTransaction
Dim blnFlagTransact ion As Boolean = False
sqlConnection = New SqlConnection
sqlConnection.C onnectionString = "server=" & System.Configur ation.Configura tionManager.App Settings("serve r") & ";packet size=4096;user id=" & System.Configur ation.Configura tionManager.App Settings("user" ) & ";password= " & System.Configur ation.Configura tionManager.App Settings("passw ord") & ";initial catalog=" & System.Configur ation.Configura tionManager.App Settings("initi alcatalog") & ";persist security info=" & System.Configur ation.Configura tionManager.App Settings("persi stSecurityInfo" ) & ""
Try
sqlConnection.O pen()
sqlDA = New SqlDataAdapter( )
sqlImageInsertT ransaction = sqlConnection.B eginTransaction ()
For Each dr As DataRow In dt.Rows
Dim myImageBuffer() As Byte
Dim blnFlag As Boolean = False
If FileIO.FileSyst em.FileExists(d r.Item("ImageNa me")) Then
If FileExtensionIs Approved(System .IO.Path.GetExt ension(dr.Item( "ImageName" ))) Then
Dim myStream As New FileStream(dr.I tem("ImageName" ), FileMode.Open, FileAccess.Read )
myImageBuffer = New Byte(myStream.L ength - 1) {}
myStream.Read(m yImageBuffer, 0, CInt(myStream.L ength))
myStream.Close( )
blnFlag = True
End If
End If
sqlDA.SelectCom mand = New SqlCommand("dbo .SaveHighwayIns pectionObservat ionImages", sqlConnection)
sqlDA.SelectCom mand.CommandTyp e = CommandType.Sto redProcedure
sqlDA.SelectCom mand.Transactio n = sqlImageInsertT ransaction
'Add Parameters
Dim paramInspection ObservationChai nageSideID As SqlParameter = New SqlParameter()
paramInspection ObservationChai nageSideID.Para meterName = "@iInspectionOb servationDamage ID"
paramInspection ObservationChai nageSideID.SqlD bType = SqlDbType.BigIn t
paramInspection ObservationChai nageSideID.Dire ction = ParameterDirect ion.Input
paramInspection ObservationChai nageSideID.Valu e = dr.Item("Inspec tion_Observatio n_Damage_ID")
Dim paramImageName As SqlParameter = New SqlParameter()
paramImageName. ParameterName = "@vImageNam e"
paramImageName. SqlDbType = SqlDbType.VarCh ar
paramImageName. Direction = ParameterDirect ion.Input
paramImageName. Value = System.IO.Path. GetFileName(dr. Item("ImageName "))
Dim paramDescriptio n As SqlParameter = New SqlParameter()
paramDescriptio n.ParameterName = "@vDescript ion"
paramDescriptio n.SqlDbType = SqlDbType.VarCh ar
paramDescriptio n.Direction = ParameterDirect ion.Input
paramDescriptio n.Value = dr.Item("Descri ption")
Dim paramObservatio nImage As SqlParameter = New SqlParameter()
paramObservatio nImage.Paramete rName = "@iObservationI mage"
paramObservatio nImage.SqlDbTyp e = SqlDbType.Image
paramObservatio nImage.Directio n = ParameterDirect ion.Input
If blnFlag Then
paramObservatio nImage.Value = myImageBuffer
Else
paramObservatio nImage.Value = DBNull.Value
End If
'cmd.Parameters .Add(paramRetur nValue); // must be added first, parameter 0
sqlDA.SelectCom mand.Parameters .Add(paramInspe ctionObservatio nChainageSideID )
'sqlDA.SelectCo mmand.Parameter s.Add(paramImag esToDelete)
sqlDA.SelectCom mand.Parameters .Add(paramImage Name)
sqlDA.SelectCom mand.Parameters .Add(paramDescr iption)
sqlDA.SelectCom mand.Parameters .Add(paramObser vationImage)
sqlDA.SelectCom mand.ExecuteNon Query()
Next
sqlImageInsertT ransaction.Comm it()
Catch ex As Exception
blnFlagTransact ion = True
If Not sqlConnection Is Nothing Then
sqlConnection.C lose()
End If
Throw ex
Finally
If blnFlagTransact ion Then
If Not sqlConnection Is Nothing Then
sqlDA.SelectCom mand.Transactio n.Rollback()
End If
Else
If Not sqlConnection Is Nothing Then
sqlConnection.C lose()
End If
End If
End Try
Return Not blnFlagTransact ion
End Function
This Code works fine on my local machine but fails when uploaded...
I dont know how to rectify it please help me!
Thanks a lot....
I have a database interaction file in vb. net where in I am connecting to database and I am inserting the image files in the SQL server 2005 database.
The Code works fine on my machine but when I uploaded the site , an error occured saying "Object Reference is not set to an instance of an object" at
SaveInspectionO bservationImage s(ByVal dt As DataTable)
The Code for this Function is as follows...
Public Function SaveInspectionO bservationImage s(ByVal dt As DataTable) As Boolean ' Here I am passing the Images which are in datatable.
Dim sqlImageInsertT ransaction As SqlTransaction
Dim blnFlagTransact ion As Boolean = False
sqlConnection = New SqlConnection
sqlConnection.C onnectionString = "server=" & System.Configur ation.Configura tionManager.App Settings("serve r") & ";packet size=4096;user id=" & System.Configur ation.Configura tionManager.App Settings("user" ) & ";password= " & System.Configur ation.Configura tionManager.App Settings("passw ord") & ";initial catalog=" & System.Configur ation.Configura tionManager.App Settings("initi alcatalog") & ";persist security info=" & System.Configur ation.Configura tionManager.App Settings("persi stSecurityInfo" ) & ""
Try
sqlConnection.O pen()
sqlDA = New SqlDataAdapter( )
sqlImageInsertT ransaction = sqlConnection.B eginTransaction ()
For Each dr As DataRow In dt.Rows
Dim myImageBuffer() As Byte
Dim blnFlag As Boolean = False
If FileIO.FileSyst em.FileExists(d r.Item("ImageNa me")) Then
If FileExtensionIs Approved(System .IO.Path.GetExt ension(dr.Item( "ImageName" ))) Then
Dim myStream As New FileStream(dr.I tem("ImageName" ), FileMode.Open, FileAccess.Read )
myImageBuffer = New Byte(myStream.L ength - 1) {}
myStream.Read(m yImageBuffer, 0, CInt(myStream.L ength))
myStream.Close( )
blnFlag = True
End If
End If
sqlDA.SelectCom mand = New SqlCommand("dbo .SaveHighwayIns pectionObservat ionImages", sqlConnection)
sqlDA.SelectCom mand.CommandTyp e = CommandType.Sto redProcedure
sqlDA.SelectCom mand.Transactio n = sqlImageInsertT ransaction
'Add Parameters
Dim paramInspection ObservationChai nageSideID As SqlParameter = New SqlParameter()
paramInspection ObservationChai nageSideID.Para meterName = "@iInspectionOb servationDamage ID"
paramInspection ObservationChai nageSideID.SqlD bType = SqlDbType.BigIn t
paramInspection ObservationChai nageSideID.Dire ction = ParameterDirect ion.Input
paramInspection ObservationChai nageSideID.Valu e = dr.Item("Inspec tion_Observatio n_Damage_ID")
Dim paramImageName As SqlParameter = New SqlParameter()
paramImageName. ParameterName = "@vImageNam e"
paramImageName. SqlDbType = SqlDbType.VarCh ar
paramImageName. Direction = ParameterDirect ion.Input
paramImageName. Value = System.IO.Path. GetFileName(dr. Item("ImageName "))
Dim paramDescriptio n As SqlParameter = New SqlParameter()
paramDescriptio n.ParameterName = "@vDescript ion"
paramDescriptio n.SqlDbType = SqlDbType.VarCh ar
paramDescriptio n.Direction = ParameterDirect ion.Input
paramDescriptio n.Value = dr.Item("Descri ption")
Dim paramObservatio nImage As SqlParameter = New SqlParameter()
paramObservatio nImage.Paramete rName = "@iObservationI mage"
paramObservatio nImage.SqlDbTyp e = SqlDbType.Image
paramObservatio nImage.Directio n = ParameterDirect ion.Input
If blnFlag Then
paramObservatio nImage.Value = myImageBuffer
Else
paramObservatio nImage.Value = DBNull.Value
End If
'cmd.Parameters .Add(paramRetur nValue); // must be added first, parameter 0
sqlDA.SelectCom mand.Parameters .Add(paramInspe ctionObservatio nChainageSideID )
'sqlDA.SelectCo mmand.Parameter s.Add(paramImag esToDelete)
sqlDA.SelectCom mand.Parameters .Add(paramImage Name)
sqlDA.SelectCom mand.Parameters .Add(paramDescr iption)
sqlDA.SelectCom mand.Parameters .Add(paramObser vationImage)
sqlDA.SelectCom mand.ExecuteNon Query()
Next
sqlImageInsertT ransaction.Comm it()
Catch ex As Exception
blnFlagTransact ion = True
If Not sqlConnection Is Nothing Then
sqlConnection.C lose()
End If
Throw ex
Finally
If blnFlagTransact ion Then
If Not sqlConnection Is Nothing Then
sqlDA.SelectCom mand.Transactio n.Rollback()
End If
Else
If Not sqlConnection Is Nothing Then
sqlConnection.C lose()
End If
End If
End Try
Return Not blnFlagTransact ion
End Function
This Code works fine on my local machine but fails when uploaded...
I dont know how to rectify it please help me!
Thanks a lot....