i see this error any time i try to up load photo to my database.
"Object reference not set to an instance of an object" what is possible wrong with my codes?
###############################################
Imports System.Data.SqlClient
Imports System.Data
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Try 'Checking the file in fileupload
If FileUpload1.HasFile Then
Dim imageSize As Byte()
imageSize = New Byte(FileUpload1.PostedFile.ContentLength - 1) {}
Dim postfile As HttpPostedFile = FileUpload1.PostedFile()
postfile.InputStream.Read(imageSize, 0, CInt(FileUpload1.PostedFile.ContentLength))
' Create SQL Connection
Dim campusplus As New SqlConnection()
campusplus.ConnectionString = ConfigurationManager.ConnectionStrings("campusplus").ConnectionString
' Create SQL Command
'Checking file type
If postfile.ContentType.ToLower() = "image/gif" Then
Dim cmd As New SqlCommand()
cmd.CommandText = "INSERT INTO upload(fileid,ImageName,Image) VALUES (@fileid,@ImageName,@Image)"
cmd.CommandType = CommandType.Text
cmd.Connection = campusplus
Dim ImageName As New SqlParameter("@ImageName", SqlDbType.VarChar, 50)
ImageName.Value = TextBox1.Text.ToString
cmd.Parameters.Add(ImageName)
Dim imagelenth As New SqlParameter("@Image", SqlDbType.Image, imageSize.Length)
imagelenth.Value = imageSize
cmd.Parameters.Add(imagelenth)
campusplus.Open()
Dim result As Integer = cmd.ExecuteNonQuery()
campusplus.Close()
Else
Label1.Text = "Its not a gif image"
End If
Else
Label1.Text = "please select the file to uplaod"
End If
Catch ex As Exception
Response.Write(ex.Message)
End Try
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
End Class
That error means one of your fields is null and the database is expecting something to be input, most likely an integer. Do a check for null variable and if the value is null or empty, present value as dbnull and database should take it.
you can also use sql profiler to exam the exact sql statement that is being execute on the sql side if you are using a EF or some other mapper. This might help you to quickly narrow down what field is being left empty.
Also if you are using in page sql that you have not accidentally miss spelled one of the names of the sql parameters This would cause one of the parameters to not being populated even though you are providing an appropriate value.
Nana Adu
Member
24 Points
49 Posts
what is possible wrong with my codes
Feb 24, 2012 06:09 PM|LINK
i see this error any time i try to up load photo to my database.
"Object reference not set to an instance of an object" what is possible wrong with my codes?
###############################################
Imports System.Data.SqlClient Imports System.Data Partial Class _Default Inherits System.Web.UI.Page Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Try 'Checking the file in fileupload If FileUpload1.HasFile Then Dim imageSize As Byte() imageSize = New Byte(FileUpload1.PostedFile.ContentLength - 1) {} Dim postfile As HttpPostedFile = FileUpload1.PostedFile() postfile.InputStream.Read(imageSize, 0, CInt(FileUpload1.PostedFile.ContentLength)) ' Create SQL Connection Dim campusplus As New SqlConnection() campusplus.ConnectionString = ConfigurationManager.ConnectionStrings("campusplus").ConnectionString ' Create SQL Command 'Checking file type If postfile.ContentType.ToLower() = "image/gif" Then Dim cmd As New SqlCommand() cmd.CommandText = "INSERT INTO upload(fileid,ImageName,Image) VALUES (@fileid,@ImageName,@Image)" cmd.CommandType = CommandType.Text cmd.Connection = campusplus Dim ImageName As New SqlParameter("@ImageName", SqlDbType.VarChar, 50) ImageName.Value = TextBox1.Text.ToString cmd.Parameters.Add(ImageName) Dim imagelenth As New SqlParameter("@Image", SqlDbType.Image, imageSize.Length) imagelenth.Value = imageSize cmd.Parameters.Add(imagelenth) campusplus.Open() Dim result As Integer = cmd.ExecuteNonQuery() campusplus.Close() Else Label1.Text = "Its not a gif image" End If Else Label1.Text = "please select the file to uplaod" End If Catch ex As Exception Response.Write(ex.Message) End Try End Sub Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load End Sub End Class###########################################################
this how my db looks
bbcompent1
All-Star
32992 Points
8508 Posts
Moderator
Re: what is possible wrong with my codes
Feb 24, 2012 06:12 PM|LINK
That error means one of your fields is null and the database is expecting something to be input, most likely an integer. Do a check for null variable and if the value is null or empty, present value as dbnull and database should take it.
skcookie
Member
534 Points
138 Posts
Re: what is possible wrong with my codes
Feb 24, 2012 06:30 PM|LINK
you can also use sql profiler to exam the exact sql statement that is being execute on the sql side if you are using a EF or some other mapper. This might help you to quickly narrow down what field is being left empty.
Also if you are using in page sql that you have not accidentally miss spelled one of the names of the sql parameters This would cause one of the parameters to not being populated even though you are providing an appropriate value.
Patrick P.
Microsoft Certified Professional
Remember to mark as answer where appropriate!
kushalrdalal
Contributor
7130 Points
1272 Posts
Re: what is possible wrong with my codes
Feb 24, 2012 06:37 PM|LINK
If you will post your code it would be easier to find the problem.
My Blog
LinkedIn Profile
Nana Adu
Member
24 Points
49 Posts
Re: what is possible wrong with my codes
Feb 29, 2012 11:52 AM|LINK
i've posted my codes
can you help me now?
vpraveenkuma...
Member
53 Points
56 Posts
Re: what is possible wrong with my codes
Feb 29, 2012 11:58 AM|LINK
please add your page in top it means above the button click event i think one object missing for one class file.......
P_IT
Participant
882 Points
222 Posts
Re: what is possible wrong with my codes
Feb 29, 2012 12:01 PM|LINK
Hi as per bbcompent1 says it means DB is not getting any one value.
add breakpoing and check wheter it shows an error .
P_IT
harshabharat...
Member
50 Points
10 Posts
Re: what is possible wrong with my codes
Feb 29, 2012 12:05 PM|LINK
Are you using Update Panel with the FileUpload ?
skcookie
Member
534 Points
138 Posts
Re: what is possible wrong with my codes
Mar 01, 2012 04:20 AM|LINK
from the code you posted you have defined the parameter "@fileid" in the sql statement but never populate it in the code.
Patrick P.
Microsoft Certified Professional
Remember to mark as answer where appropriate!