473,463 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

User Control... HELP???

Can someone please help....

I'm having major issues with a user control I'm tring to create!

I an trying to execute a sub called UploadData() from a user control which I
managed to do but for some reason I keep getting the error:

Exception Details: System.NullReferenceException: Object reference not set
to an instance of an object.

Source Error:
Line 114: Sub UploadData()
Line 115: 'Dim ImageFile As System.Web.UI.HtmlControls.HtmlInputFile
Line 116: Dim imgstream As Stream = ImageFile.PostedFile.InputStream
Line 117: Dim imgdata(ImageFile.PostedFile.ContentLength) As Byte
Line 118: imgstream.Read(imgdata, 0,
ImageFile.PostedFile.ContentLength)

Error is on line 116...

I know that the sub works because if I put it staight into the aspx page it
works fine but as soon as I put the sub into the User Control it seems to
stop working as ImageFile.PostedFile.InputStream = nothing...

Can someone please please HELP.... Why does this happen???

Thanks

ASPX PAGE...

Imports System.Data.SqlClient
Imports System.Data
Imports System.IO

Public Class binUpload
Inherits System.Web.UI.Page
#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.diagnostics.debuggerstepthrough()> Private Sub
InitializeComponent()

End Sub
Protected WithEvents Menu1 As skmMenu.Menu
Protected WithEvents phHeader As System.Web.UI.WebControls.PlaceHolder
Protected WithEvents phHeaderMenu As System.Web.UI.WebControls.PlaceHolder
Protected WithEvents phFooter As System.Web.UI.WebControls.PlaceHolder
Protected WithEvents phQuickLinks As System.Web.UI.WebControls.PlaceHolder
Protected WithEvents phRefresh As System.Web.UI.WebControls.PlaceHolder
Protected WithEvents phBody As System.Web.UI.WebControls.PlaceHolder
Protected WithEvents phUploader As System.Web.UI.WebControls.PlaceHolder
Protected WithEvents phWait As System.Web.UI.WebControls.PlaceHolder
Protected WithEvents phResults As System.Web.UI.WebControls.PlaceHolder
'NOTE: The following placeholder declaration is required by the Web Form
Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here

phBody.Controls.Add(New LiteralControl("<body bottomMargin=" &
Chr(34) & "0" & Chr(34) & "leftMargin=" & Chr(34) & "0" & Chr(34) &
"topMargin=" & Chr(34) & "0" & Chr(34) & "rightMargin=" & Chr(34) & "0" &
Chr(34) & "runat=" & Chr(34) & "server" & Chr(34) & ">"))
Dim UploadCnt As Control =
LoadControl("../binFileUpload/UploadCnt.ascx")
phWait.Controls.Clear()
phResults.Controls.Clear()
phUploader.Controls.Add(UploadCnt)

If Page.IsPostBack Then

' user submitted file to be uploaded
' create URL with query string to confirm file upload
' next page will not be a postback, so viewstate will be lost
Dim sRefreshURL As String = Request.Url.ToString() &
"?Upload=True"

If Request.Browser.Browser = "IE" Then
phBody.Controls.Clear()
phBody.Controls.Add(New LiteralControl("<body onload=" &
Chr(34) & "loadTarget(" & Chr(39) & sRefreshURL & Chr(39) & ")" & Chr(59) &
Chr(34) & " bottomMargin=" & Chr(34) & "0" & Chr(34) & " leftMargin=" &
Chr(34) & "0" & Chr(34) & " topMargin=" & Chr(34) & "0" & Chr(34) & "
rightMargin=" & Chr(34) & "0" & Chr(34) & " runat=" & Chr(34) & "server" &
Chr(34) & ">"))

' set META REFRESH as well in case script is disabled
' use long delay so that script can load page first if
possible
phRefresh.Controls.Clear()
phRefresh.Controls.Add(New LiteralControl("<meta
http-equiv=" & Chr(34) & "refresh" & Chr(34) & " content=" & Chr(34) &
"30;url=" & sRefreshURL & Chr(34) & ">"))
Else

' not IE so use META REFRESH to start loading next page
' allow 3 seconds for progress bar image to load

phRefresh.Controls.Clear()
phRefresh.Controls.Add(New LiteralControl("<meta
http-equiv=" & Chr(34) & "Refresh" & Chr(34) & " content=" & Chr(34) &
"3;url=" & sRefreshURL & Chr(34) & ">"))

End If

' hide Upload file controls and show "wait" section
Dim WaitCnt As Control =
LoadControl("../binFileUpload/ProgressCnt.ascx")
phUploader.Controls.Clear()
phResults.Controls.Clear()
phWait.Controls.Add(WaitCnt)

'pnlUploadFile.Visible = False
'pnlWait.Visible = True
Dim objUpload As UploadCnt
objUpload.UploadData()

Else

' get query string
Dim iUpload As String = Request.QueryString("Upload")
If iUpload = "" Then

'pnlUploadFile.Visible = True
phWait.Controls.Clear()
phResults.Controls.Clear()
phUploader.Controls.Add(UploadCnt)

Else
phUploader.Controls.Clear()
phWait.Controls.Clear()
Dim ResultsCnt As Control =
LoadControl("../binFileUpload/ResultsCnt.ascx")
phResults.Controls.Add(ResultsCnt)

End If
Dim sRefreshURL As String = Request.Url.ToString() &
"?Upload=False"
End If
End Sub

End Class
USER CONTROL...

Imports System.Data.SqlClient
Imports System.Data
Imports System.IO
Public Class UploadCnt
Inherits System.Web.UI.UserControl

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub
Protected WithEvents phHeader As System.Web.UI.WebControls.PlaceHolder
Protected WithEvents phHeaderMenu As System.Web.UI.WebControls.PlaceHolder
Protected WithEvents ImageTitle As System.Web.UI.WebControls.TextBox
Protected WithEvents Image4 As System.Web.UI.WebControls.Image
Protected WithEvents fldDescription As System.Web.UI.WebControls.TextBox
Protected WithEvents Image3 As System.Web.UI.WebControls.Image
Protected WithEvents Image1 As System.Web.UI.WebControls.Image
Protected WithEvents fldOwner As System.Web.UI.WebControls.TextBox
Protected WithEvents ddlOffice As System.Web.UI.WebControls.DropDownList
Protected WithEvents Image5 As System.Web.UI.WebControls.Image
Protected WithEvents lblMessage As System.Web.UI.WebControls.Label
Protected WithEvents urlUploadedFiles As
System.Web.UI.WebControls.HyperLink
Protected WithEvents phQuickLinks As System.Web.UI.WebControls.PlaceHolder
Protected WithEvents phFooter As System.Web.UI.WebControls.PlaceHolder
Protected WithEvents Table2 As System.Web.UI.HtmlControls.HtmlTable
Protected WithEvents ImageFile As System.Web.UI.HtmlControls.HtmlInputFile
Protected WithEvents Upload As System.Web.UI.WebControls.Button

'NOTE: The following placeholder declaration is required by the Web Form
Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here

'Loads user controls
Dim c1 As Control = LoadControl("../userControls/footer.ascx")
phFooter.Controls.Add(c1)
Dim c2 As Control = LoadControl("../userControls/headerMenu.ascx")
phHeaderMenu.Controls.Add(c2)
Dim c3 As Control = LoadControl("../userControls/Header.ascx")
phHeader.Controls.Add(c3)
Dim c4 As Control = LoadControl("../userControls/QuickLinks.ascx")
phQuickLinks.Controls.Add(c4)

End Sub
Sub UploadData()
'Dim ImageFile As System.Web.UI.HtmlControls.HtmlInputFile
Dim imgstream As Stream = ImageFile.PostedFile.InputStream
Dim imgdata(ImageFile.PostedFile.ContentLength) As Byte
imgstream.Read(imgdata, 0, ImageFile.PostedFile.ContentLength)
Dim MyConn As New
SqlConnection(ConfigurationSettings.AppSettings("s trConn"))
Dim cmd As New SqlCommand("fileUpload", MyConn)
cmd.CommandType = CommandType.StoredProcedure

Dim titleparam As New SqlParameter("@fileTitle", SqlDbType.NVarChar,
255)
Dim descriptionparam As New SqlParameter("@fileDescription",
SqlDbType.NVarChar, 255)
Dim Ownerparam As New SqlParameter("@fileOwner", SqlDbType.NVarChar,
255)
Dim Officeparam As New SqlParameter("@officeID", SqlDbType.Int)
Dim typeparam As New SqlParameter("@fileType", SqlDbType.NVarChar,
100)
Dim dataparam As New SqlParameter("@fileData", SqlDbType.Image)

'Dim ImageTitle As System.Web.UI.WebControls.TextBox
'Dim fldOwner As System.Web.UI.WebControls.TextBox
'Dim fldDescription As System.Web.UI.WebControls.TextBox
'Dim ddlOffice As System.Web.UI.WebControls.DropDownList

titleparam.Value = ImageTitle.Text
descriptionparam.Value = fldDescription.Text
Ownerparam.Value = fldOwner.Text
Officeparam.Value = ddlOffice.SelectedIndex
typeparam.Value = ImageFile.PostedFile.ContentType
dataparam.Value = imgdata

cmd.Parameters.Add(titleparam)
cmd.Parameters.Add(descriptionparam)
cmd.Parameters.Add(Ownerparam)
cmd.Parameters.Add(Officeparam)
cmd.Parameters.Add(typeparam)
cmd.Parameters.Add(dataparam)

MyConn.Open()
cmd.ExecuteNonQuery()
MyConn.Close()
End Sub
End Class
Was this post helpful to you?

Why should I rate a post?

Expand AllCollapse All

Nov 19 '05 #1
3 2216
Tim:
NullReference exceptions are typically easy to track down. My guess is
simply that ImageFile isn't a control in your user control. In other words,
there's no:
<input type="file" id="ImageFile" runat="server" /> in yoru ascx
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!)
"Tim::.." <myatix_at_hotmail.com> wrote in message
news:D3**********************************@microsof t.com...
Can someone please help....

I'm having major issues with a user control I'm tring to create!

I an trying to execute a sub called UploadData() from a user control which
I
managed to do but for some reason I keep getting the error:

Exception Details: System.NullReferenceException: Object reference not set
to an instance of an object.

Source Error:
Line 114: Sub UploadData()
Line 115: 'Dim ImageFile As
System.Web.UI.HtmlControls.HtmlInputFile
Line 116: Dim imgstream As Stream =
ImageFile.PostedFile.InputStream
Line 117: Dim imgdata(ImageFile.PostedFile.ContentLength) As Byte
Line 118: imgstream.Read(imgdata, 0,
ImageFile.PostedFile.ContentLength)

Error is on line 116...

I know that the sub works because if I put it staight into the aspx page
it
works fine but as soon as I put the sub into the User Control it seems to
stop working as ImageFile.PostedFile.InputStream = nothing...

Can someone please please HELP.... Why does this happen???

Thanks

ASPX PAGE...

Imports System.Data.SqlClient
Imports System.Data
Imports System.IO

Public Class binUpload
Inherits System.Web.UI.Page
#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.diagnostics.debuggerstepthrough()> Private Sub
InitializeComponent()

End Sub
Protected WithEvents Menu1 As skmMenu.Menu
Protected WithEvents phHeader As System.Web.UI.WebControls.PlaceHolder
Protected WithEvents phHeaderMenu As
System.Web.UI.WebControls.PlaceHolder
Protected WithEvents phFooter As System.Web.UI.WebControls.PlaceHolder
Protected WithEvents phQuickLinks As
System.Web.UI.WebControls.PlaceHolder
Protected WithEvents phRefresh As System.Web.UI.WebControls.PlaceHolder
Protected WithEvents phBody As System.Web.UI.WebControls.PlaceHolder
Protected WithEvents phUploader As
System.Web.UI.WebControls.PlaceHolder
Protected WithEvents phWait As System.Web.UI.WebControls.PlaceHolder
Protected WithEvents phResults As System.Web.UI.WebControls.PlaceHolder
'NOTE: The following placeholder declaration is required by the Web
Form
Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here

phBody.Controls.Add(New LiteralControl("<body bottomMargin=" &
Chr(34) & "0" & Chr(34) & "leftMargin=" & Chr(34) & "0" & Chr(34) &
"topMargin=" & Chr(34) & "0" & Chr(34) & "rightMargin=" & Chr(34) & "0" &
Chr(34) & "runat=" & Chr(34) & "server" & Chr(34) & ">"))
Dim UploadCnt As Control =
LoadControl("../binFileUpload/UploadCnt.ascx")
phWait.Controls.Clear()
phResults.Controls.Clear()
phUploader.Controls.Add(UploadCnt)

If Page.IsPostBack Then

' user submitted file to be uploaded
' create URL with query string to confirm file upload
' next page will not be a postback, so viewstate will be lost
Dim sRefreshURL As String = Request.Url.ToString() &
"?Upload=True"

If Request.Browser.Browser = "IE" Then
phBody.Controls.Clear()
phBody.Controls.Add(New LiteralControl("<body onload=" &
Chr(34) & "loadTarget(" & Chr(39) & sRefreshURL & Chr(39) & ")" & Chr(59)
&
Chr(34) & " bottomMargin=" & Chr(34) & "0" & Chr(34) & " leftMargin=" &
Chr(34) & "0" & Chr(34) & " topMargin=" & Chr(34) & "0" & Chr(34) & "
rightMargin=" & Chr(34) & "0" & Chr(34) & " runat=" & Chr(34) & "server" &
Chr(34) & ">"))

' set META REFRESH as well in case script is disabled
' use long delay so that script can load page first if
possible
phRefresh.Controls.Clear()
phRefresh.Controls.Add(New LiteralControl("<meta
http-equiv=" & Chr(34) & "refresh" & Chr(34) & " content=" & Chr(34) &
"30;url=" & sRefreshURL & Chr(34) & ">"))
Else

' not IE so use META REFRESH to start loading next page
' allow 3 seconds for progress bar image to load

phRefresh.Controls.Clear()
phRefresh.Controls.Add(New LiteralControl("<meta
http-equiv=" & Chr(34) & "Refresh" & Chr(34) & " content=" & Chr(34) &
"3;url=" & sRefreshURL & Chr(34) & ">"))

End If

' hide Upload file controls and show "wait" section
Dim WaitCnt As Control =
LoadControl("../binFileUpload/ProgressCnt.ascx")
phUploader.Controls.Clear()
phResults.Controls.Clear()
phWait.Controls.Add(WaitCnt)

'pnlUploadFile.Visible = False
'pnlWait.Visible = True
Dim objUpload As UploadCnt
objUpload.UploadData()

Else

' get query string
Dim iUpload As String = Request.QueryString("Upload")
If iUpload = "" Then

'pnlUploadFile.Visible = True
phWait.Controls.Clear()
phResults.Controls.Clear()
phUploader.Controls.Add(UploadCnt)

Else
phUploader.Controls.Clear()
phWait.Controls.Clear()
Dim ResultsCnt As Control =
LoadControl("../binFileUpload/ResultsCnt.ascx")
phResults.Controls.Add(ResultsCnt)

End If
Dim sRefreshURL As String = Request.Url.ToString() &
"?Upload=False"
End If
End Sub

End Class
USER CONTROL...

Imports System.Data.SqlClient
Imports System.Data
Imports System.IO
Public Class UploadCnt
Inherits System.Web.UI.UserControl

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub
Protected WithEvents phHeader As System.Web.UI.WebControls.PlaceHolder
Protected WithEvents phHeaderMenu As
System.Web.UI.WebControls.PlaceHolder
Protected WithEvents ImageTitle As System.Web.UI.WebControls.TextBox
Protected WithEvents Image4 As System.Web.UI.WebControls.Image
Protected WithEvents fldDescription As
System.Web.UI.WebControls.TextBox
Protected WithEvents Image3 As System.Web.UI.WebControls.Image
Protected WithEvents Image1 As System.Web.UI.WebControls.Image
Protected WithEvents fldOwner As System.Web.UI.WebControls.TextBox
Protected WithEvents ddlOffice As
System.Web.UI.WebControls.DropDownList
Protected WithEvents Image5 As System.Web.UI.WebControls.Image
Protected WithEvents lblMessage As System.Web.UI.WebControls.Label
Protected WithEvents urlUploadedFiles As
System.Web.UI.WebControls.HyperLink
Protected WithEvents phQuickLinks As
System.Web.UI.WebControls.PlaceHolder
Protected WithEvents phFooter As System.Web.UI.WebControls.PlaceHolder
Protected WithEvents Table2 As System.Web.UI.HtmlControls.HtmlTable
Protected WithEvents ImageFile As
System.Web.UI.HtmlControls.HtmlInputFile
Protected WithEvents Upload As System.Web.UI.WebControls.Button

'NOTE: The following placeholder declaration is required by the Web
Form
Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here

'Loads user controls
Dim c1 As Control = LoadControl("../userControls/footer.ascx")
phFooter.Controls.Add(c1)
Dim c2 As Control = LoadControl("../userControls/headerMenu.ascx")
phHeaderMenu.Controls.Add(c2)
Dim c3 As Control = LoadControl("../userControls/Header.ascx")
phHeader.Controls.Add(c3)
Dim c4 As Control = LoadControl("../userControls/QuickLinks.ascx")
phQuickLinks.Controls.Add(c4)

End Sub
Sub UploadData()
'Dim ImageFile As System.Web.UI.HtmlControls.HtmlInputFile
Dim imgstream As Stream = ImageFile.PostedFile.InputStream
Dim imgdata(ImageFile.PostedFile.ContentLength) As Byte
imgstream.Read(imgdata, 0, ImageFile.PostedFile.ContentLength)
Dim MyConn As New
SqlConnection(ConfigurationSettings.AppSettings("s trConn"))
Dim cmd As New SqlCommand("fileUpload", MyConn)
cmd.CommandType = CommandType.StoredProcedure

Dim titleparam As New SqlParameter("@fileTitle",
SqlDbType.NVarChar,
255)
Dim descriptionparam As New SqlParameter("@fileDescription",
SqlDbType.NVarChar, 255)
Dim Ownerparam As New SqlParameter("@fileOwner",
SqlDbType.NVarChar,
255)
Dim Officeparam As New SqlParameter("@officeID", SqlDbType.Int)
Dim typeparam As New SqlParameter("@fileType", SqlDbType.NVarChar,
100)
Dim dataparam As New SqlParameter("@fileData", SqlDbType.Image)

'Dim ImageTitle As System.Web.UI.WebControls.TextBox
'Dim fldOwner As System.Web.UI.WebControls.TextBox
'Dim fldDescription As System.Web.UI.WebControls.TextBox
'Dim ddlOffice As System.Web.UI.WebControls.DropDownList

titleparam.Value = ImageTitle.Text
descriptionparam.Value = fldDescription.Text
Ownerparam.Value = fldOwner.Text
Officeparam.Value = ddlOffice.SelectedIndex
typeparam.Value = ImageFile.PostedFile.ContentType
dataparam.Value = imgdata

cmd.Parameters.Add(titleparam)
cmd.Parameters.Add(descriptionparam)
cmd.Parameters.Add(Ownerparam)
cmd.Parameters.Add(Officeparam)
cmd.Parameters.Add(typeparam)
cmd.Parameters.Add(dataparam)

MyConn.Open()
cmd.ExecuteNonQuery()
MyConn.Close()
End Sub
End Class
Was this post helpful to you?

Why should I rate a post?

Expand AllCollapse All

Nov 19 '05 #2
Hi Karl,

Thanks for the reply... The thing is the control is in the ASCX file!

The other strange thing is if I take the code from the control and put it
straight into the page it works file!

Any other ideas.... I'm stuck....

"Karl Seguin" wrote:
Tim:
NullReference exceptions are typically easy to track down. My guess is
simply that ImageFile isn't a control in your user control. In other words,
there's no:
<input type="file" id="ImageFile" runat="server" /> in yoru ascx
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!)
"Tim::.." <myatix_at_hotmail.com> wrote in message
news:D3**********************************@microsof t.com...
Can someone please help....

I'm having major issues with a user control I'm tring to create!

I an trying to execute a sub called UploadData() from a user control which
I
managed to do but for some reason I keep getting the error:

Exception Details: System.NullReferenceException: Object reference not set
to an instance of an object.

Source Error:
Line 114: Sub UploadData()
Line 115: 'Dim ImageFile As
System.Web.UI.HtmlControls.HtmlInputFile
Line 116: Dim imgstream As Stream =
ImageFile.PostedFile.InputStream
Line 117: Dim imgdata(ImageFile.PostedFile.ContentLength) As Byte
Line 118: imgstream.Read(imgdata, 0,
ImageFile.PostedFile.ContentLength)

Error is on line 116...

I know that the sub works because if I put it staight into the aspx page
it
works fine but as soon as I put the sub into the User Control it seems to
stop working as ImageFile.PostedFile.InputStream = nothing...

Can someone please please HELP.... Why does this happen???

Thanks

ASPX PAGE...

Imports System.Data.SqlClient
Imports System.Data
Imports System.IO

Public Class binUpload
Inherits System.Web.UI.Page
#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.diagnostics.debuggerstepthrough()> Private Sub
InitializeComponent()

End Sub
Protected WithEvents Menu1 As skmMenu.Menu
Protected WithEvents phHeader As System.Web.UI.WebControls.PlaceHolder
Protected WithEvents phHeaderMenu As
System.Web.UI.WebControls.PlaceHolder
Protected WithEvents phFooter As System.Web.UI.WebControls.PlaceHolder
Protected WithEvents phQuickLinks As
System.Web.UI.WebControls.PlaceHolder
Protected WithEvents phRefresh As System.Web.UI.WebControls.PlaceHolder
Protected WithEvents phBody As System.Web.UI.WebControls.PlaceHolder
Protected WithEvents phUploader As
System.Web.UI.WebControls.PlaceHolder
Protected WithEvents phWait As System.Web.UI.WebControls.PlaceHolder
Protected WithEvents phResults As System.Web.UI.WebControls.PlaceHolder
'NOTE: The following placeholder declaration is required by the Web
Form
Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here

phBody.Controls.Add(New LiteralControl("<body bottomMargin=" &
Chr(34) & "0" & Chr(34) & "leftMargin=" & Chr(34) & "0" & Chr(34) &
"topMargin=" & Chr(34) & "0" & Chr(34) & "rightMargin=" & Chr(34) & "0" &
Chr(34) & "runat=" & Chr(34) & "server" & Chr(34) & ">"))
Dim UploadCnt As Control =
LoadControl("../binFileUpload/UploadCnt.ascx")
phWait.Controls.Clear()
phResults.Controls.Clear()
phUploader.Controls.Add(UploadCnt)

If Page.IsPostBack Then

' user submitted file to be uploaded
' create URL with query string to confirm file upload
' next page will not be a postback, so viewstate will be lost
Dim sRefreshURL As String = Request.Url.ToString() &
"?Upload=True"

If Request.Browser.Browser = "IE" Then
phBody.Controls.Clear()
phBody.Controls.Add(New LiteralControl("<body onload=" &
Chr(34) & "loadTarget(" & Chr(39) & sRefreshURL & Chr(39) & ")" & Chr(59)
&
Chr(34) & " bottomMargin=" & Chr(34) & "0" & Chr(34) & " leftMargin=" &
Chr(34) & "0" & Chr(34) & " topMargin=" & Chr(34) & "0" & Chr(34) & "
rightMargin=" & Chr(34) & "0" & Chr(34) & " runat=" & Chr(34) & "server" &
Chr(34) & ">"))

' set META REFRESH as well in case script is disabled
' use long delay so that script can load page first if
possible
phRefresh.Controls.Clear()
phRefresh.Controls.Add(New LiteralControl("<meta
http-equiv=" & Chr(34) & "refresh" & Chr(34) & " content=" & Chr(34) &
"30;url=" & sRefreshURL & Chr(34) & ">"))
Else

' not IE so use META REFRESH to start loading next page
' allow 3 seconds for progress bar image to load

phRefresh.Controls.Clear()
phRefresh.Controls.Add(New LiteralControl("<meta
http-equiv=" & Chr(34) & "Refresh" & Chr(34) & " content=" & Chr(34) &
"3;url=" & sRefreshURL & Chr(34) & ">"))

End If

' hide Upload file controls and show "wait" section
Dim WaitCnt As Control =
LoadControl("../binFileUpload/ProgressCnt.ascx")
phUploader.Controls.Clear()
phResults.Controls.Clear()
phWait.Controls.Add(WaitCnt)

'pnlUploadFile.Visible = False
'pnlWait.Visible = True
Dim objUpload As UploadCnt
objUpload.UploadData()

Else

' get query string
Dim iUpload As String = Request.QueryString("Upload")
If iUpload = "" Then

'pnlUploadFile.Visible = True
phWait.Controls.Clear()
phResults.Controls.Clear()
phUploader.Controls.Add(UploadCnt)

Else
phUploader.Controls.Clear()
phWait.Controls.Clear()
Dim ResultsCnt As Control =
LoadControl("../binFileUpload/ResultsCnt.ascx")
phResults.Controls.Add(ResultsCnt)

End If
Dim sRefreshURL As String = Request.Url.ToString() &
"?Upload=False"
End If
End Sub

End Class
USER CONTROL...

Imports System.Data.SqlClient
Imports System.Data
Imports System.IO
Public Class UploadCnt
Inherits System.Web.UI.UserControl

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub
Protected WithEvents phHeader As System.Web.UI.WebControls.PlaceHolder
Protected WithEvents phHeaderMenu As
System.Web.UI.WebControls.PlaceHolder
Protected WithEvents ImageTitle As System.Web.UI.WebControls.TextBox
Protected WithEvents Image4 As System.Web.UI.WebControls.Image
Protected WithEvents fldDescription As
System.Web.UI.WebControls.TextBox
Protected WithEvents Image3 As System.Web.UI.WebControls.Image
Protected WithEvents Image1 As System.Web.UI.WebControls.Image
Protected WithEvents fldOwner As System.Web.UI.WebControls.TextBox
Protected WithEvents ddlOffice As
System.Web.UI.WebControls.DropDownList
Protected WithEvents Image5 As System.Web.UI.WebControls.Image
Protected WithEvents lblMessage As System.Web.UI.WebControls.Label
Protected WithEvents urlUploadedFiles As
System.Web.UI.WebControls.HyperLink
Protected WithEvents phQuickLinks As
System.Web.UI.WebControls.PlaceHolder
Protected WithEvents phFooter As System.Web.UI.WebControls.PlaceHolder
Protected WithEvents Table2 As System.Web.UI.HtmlControls.HtmlTable
Protected WithEvents ImageFile As
System.Web.UI.HtmlControls.HtmlInputFile
Protected WithEvents Upload As System.Web.UI.WebControls.Button

'NOTE: The following placeholder declaration is required by the Web
Form
Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here

'Loads user controls
Dim c1 As Control = LoadControl("../userControls/footer.ascx")
phFooter.Controls.Add(c1)
Dim c2 As Control = LoadControl("../userControls/headerMenu.ascx")
phHeaderMenu.Controls.Add(c2)
Dim c3 As Control = LoadControl("../userControls/Header.ascx")
phHeader.Controls.Add(c3)
Dim c4 As Control = LoadControl("../userControls/QuickLinks.ascx")
phQuickLinks.Controls.Add(c4)

End Sub
Sub UploadData()
'Dim ImageFile As System.Web.UI.HtmlControls.HtmlInputFile
Dim imgstream As Stream = ImageFile.PostedFile.InputStream
Dim imgdata(ImageFile.PostedFile.ContentLength) As Byte
imgstream.Read(imgdata, 0, ImageFile.PostedFile.ContentLength)
Dim MyConn As New
SqlConnection(ConfigurationSettings.AppSettings("s trConn"))
Dim cmd As New SqlCommand("fileUpload", MyConn)
cmd.CommandType = CommandType.StoredProcedure

Dim titleparam As New SqlParameter("@fileTitle",
SqlDbType.NVarChar,
255)
Dim descriptionparam As New SqlParameter("@fileDescription",
SqlDbType.NVarChar, 255)
Dim Ownerparam As New SqlParameter("@fileOwner",
SqlDbType.NVarChar,
255)
Dim Officeparam As New SqlParameter("@officeID", SqlDbType.Int)
Dim typeparam As New SqlParameter("@fileType", SqlDbType.NVarChar,
100)
Dim dataparam As New SqlParameter("@fileData", SqlDbType.Image)

'Dim ImageTitle As System.Web.UI.WebControls.TextBox
'Dim fldOwner As System.Web.UI.WebControls.TextBox
'Dim fldDescription As System.Web.UI.WebControls.TextBox
'Dim ddlOffice As System.Web.UI.WebControls.DropDownList

titleparam.Value = ImageTitle.Text
descriptionparam.Value = fldDescription.Text
Ownerparam.Value = fldOwner.Text
Officeparam.Value = ddlOffice.SelectedIndex
typeparam.Value = ImageFile.PostedFile.ContentType
dataparam.Value = imgdata

cmd.Parameters.Add(titleparam)
cmd.Parameters.Add(descriptionparam)
cmd.Parameters.Add(Ownerparam)
cmd.Parameters.Add(Officeparam)
cmd.Parameters.Add(typeparam)
cmd.Parameters.Add(dataparam)

Nov 19 '05 #3
Tim:
I'm going to have to stick to my first recommendation. You must be taking
more than the sub and also moving the ascx code? Why don't you just try to
debug and see if ImageFile is a nothing?

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!)
"Tim::.." <myatix_at_hotmail.com> wrote in message
news:E5**********************************@microsof t.com...
Hi Karl,

Thanks for the reply... The thing is the control is in the ASCX file!

The other strange thing is if I take the code from the control and put it
straight into the page it works file!

Any other ideas.... I'm stuck....

"Karl Seguin" wrote:
Tim:
NullReference exceptions are typically easy to track down. My guess is
simply that ImageFile isn't a control in your user control. In other
words,
there's no:
<input type="file" id="ImageFile" runat="server" /> in yoru ascx
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!)
"Tim::.." <myatix_at_hotmail.com> wrote in message
news:D3**********************************@microsof t.com...
> Can someone please help....
>
> I'm having major issues with a user control I'm tring to create!
>
> I an trying to execute a sub called UploadData() from a user control
> which
> I
> managed to do but for some reason I keep getting the error:
>
> Exception Details: System.NullReferenceException: Object reference not
> set
> to an instance of an object.
>
> Source Error:
>
>
> Line 114: Sub UploadData()
> Line 115: 'Dim ImageFile As
> System.Web.UI.HtmlControls.HtmlInputFile
> Line 116: Dim imgstream As Stream =
> ImageFile.PostedFile.InputStream
> Line 117: Dim imgdata(ImageFile.PostedFile.ContentLength) As
> Byte
> Line 118: imgstream.Read(imgdata, 0,
> ImageFile.PostedFile.ContentLength)
>
> Error is on line 116...
>
> I know that the sub works because if I put it staight into the aspx
> page
> it
> works fine but as soon as I put the sub into the User Control it seems
> to
> stop working as ImageFile.PostedFile.InputStream = nothing...
>
> Can someone please please HELP.... Why does this happen???
>
> Thanks
>
> ASPX PAGE...
>
> Imports System.Data.SqlClient
> Imports System.Data
> Imports System.IO
>
> Public Class binUpload
> Inherits System.Web.UI.Page
> #Region " Web Form Designer Generated Code "
>
> 'This call is required by the Web Form Designer.
> <System.diagnostics.debuggerstepthrough()> Private Sub
> InitializeComponent()
>
> End Sub
> Protected WithEvents Menu1 As skmMenu.Menu
> Protected WithEvents phHeader As
> System.Web.UI.WebControls.PlaceHolder
> Protected WithEvents phHeaderMenu As
> System.Web.UI.WebControls.PlaceHolder
> Protected WithEvents phFooter As
> System.Web.UI.WebControls.PlaceHolder
> Protected WithEvents phQuickLinks As
> System.Web.UI.WebControls.PlaceHolder
> Protected WithEvents phRefresh As
> System.Web.UI.WebControls.PlaceHolder
> Protected WithEvents phBody As System.Web.UI.WebControls.PlaceHolder
> Protected WithEvents phUploader As
> System.Web.UI.WebControls.PlaceHolder
> Protected WithEvents phWait As System.Web.UI.WebControls.PlaceHolder
> Protected WithEvents phResults As
> System.Web.UI.WebControls.PlaceHolder
> 'NOTE: The following placeholder declaration is required by the Web
> Form
> Designer.
> 'Do not delete or move it.
> Private designerPlaceholderDeclaration As System.Object
>
> Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles MyBase.Init
> 'CODEGEN: This method call is required by the Web Form Designer
> 'Do not modify it using the code editor.
> InitializeComponent()
> End Sub
>
> #End Region
> Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles MyBase.Load
> 'Put user code to initialize the page here
>
> phBody.Controls.Add(New LiteralControl("<body bottomMargin=" &
> Chr(34) & "0" & Chr(34) & "leftMargin=" & Chr(34) & "0" & Chr(34) &
> "topMargin=" & Chr(34) & "0" & Chr(34) & "rightMargin=" & Chr(34) & "0"
> &
> Chr(34) & "runat=" & Chr(34) & "server" & Chr(34) & ">"))
> Dim UploadCnt As Control =
> LoadControl("../binFileUpload/UploadCnt.ascx")
> phWait.Controls.Clear()
> phResults.Controls.Clear()
> phUploader.Controls.Add(UploadCnt)
>
> If Page.IsPostBack Then
>
> ' user submitted file to be uploaded
> ' create URL with query string to confirm file upload
> ' next page will not be a postback, so viewstate will be
> lost
> Dim sRefreshURL As String = Request.Url.ToString() &
> "?Upload=True"
>
> If Request.Browser.Browser = "IE" Then
> phBody.Controls.Clear()
> phBody.Controls.Add(New LiteralControl("<body onload=" &
> Chr(34) & "loadTarget(" & Chr(39) & sRefreshURL & Chr(39) & ")" &
> Chr(59)
> &
> Chr(34) & " bottomMargin=" & Chr(34) & "0" & Chr(34) & " leftMargin=" &
> Chr(34) & "0" & Chr(34) & " topMargin=" & Chr(34) & "0" & Chr(34) & "
> rightMargin=" & Chr(34) & "0" & Chr(34) & " runat=" & Chr(34) &
> "server" &
> Chr(34) & ">"))
>
> ' set META REFRESH as well in case script is disabled
> ' use long delay so that script can load page first if
> possible
> phRefresh.Controls.Clear()
> phRefresh.Controls.Add(New LiteralControl("<meta
> http-equiv=" & Chr(34) & "refresh" & Chr(34) & " content=" & Chr(34) &
> "30;url=" & sRefreshURL & Chr(34) & ">"))
> Else
>
> ' not IE so use META REFRESH to start loading next page
> ' allow 3 seconds for progress bar image to load
>
> phRefresh.Controls.Clear()
> phRefresh.Controls.Add(New LiteralControl("<meta
> http-equiv=" & Chr(34) & "Refresh" & Chr(34) & " content=" & Chr(34) &
> "3;url=" & sRefreshURL & Chr(34) & ">"))
>
> End If
>
> ' hide Upload file controls and show "wait" section
> Dim WaitCnt As Control =
> LoadControl("../binFileUpload/ProgressCnt.ascx")
> phUploader.Controls.Clear()
> phResults.Controls.Clear()
> phWait.Controls.Add(WaitCnt)
>
> 'pnlUploadFile.Visible = False
> 'pnlWait.Visible = True
> Dim objUpload As UploadCnt
> objUpload.UploadData()
>
> Else
>
> ' get query string
> Dim iUpload As String = Request.QueryString("Upload")
> If iUpload = "" Then
>
> 'pnlUploadFile.Visible = True
> phWait.Controls.Clear()
> phResults.Controls.Clear()
> phUploader.Controls.Add(UploadCnt)
>
> Else
> phUploader.Controls.Clear()
> phWait.Controls.Clear()
> Dim ResultsCnt As Control =
> LoadControl("../binFileUpload/ResultsCnt.ascx")
> phResults.Controls.Add(ResultsCnt)
>
> End If
> Dim sRefreshURL As String = Request.Url.ToString() &
> "?Upload=False"
> End If
> End Sub
>
> End Class
>
>
> USER CONTROL...
>
> Imports System.Data.SqlClient
> Imports System.Data
> Imports System.IO
>
>
> Public Class UploadCnt
> Inherits System.Web.UI.UserControl
>
> #Region " Web Form Designer Generated Code "
>
> 'This call is required by the Web Form Designer.
> <System.Diagnostics.DebuggerStepThrough()> Private Sub
> InitializeComponent()
>
> End Sub
> Protected WithEvents phHeader As
> System.Web.UI.WebControls.PlaceHolder
> Protected WithEvents phHeaderMenu As
> System.Web.UI.WebControls.PlaceHolder
> Protected WithEvents ImageTitle As System.Web.UI.WebControls.TextBox
> Protected WithEvents Image4 As System.Web.UI.WebControls.Image
> Protected WithEvents fldDescription As
> System.Web.UI.WebControls.TextBox
> Protected WithEvents Image3 As System.Web.UI.WebControls.Image
> Protected WithEvents Image1 As System.Web.UI.WebControls.Image
> Protected WithEvents fldOwner As System.Web.UI.WebControls.TextBox
> Protected WithEvents ddlOffice As
> System.Web.UI.WebControls.DropDownList
> Protected WithEvents Image5 As System.Web.UI.WebControls.Image
> Protected WithEvents lblMessage As System.Web.UI.WebControls.Label
> Protected WithEvents urlUploadedFiles As
> System.Web.UI.WebControls.HyperLink
> Protected WithEvents phQuickLinks As
> System.Web.UI.WebControls.PlaceHolder
> Protected WithEvents phFooter As
> System.Web.UI.WebControls.PlaceHolder
> Protected WithEvents Table2 As System.Web.UI.HtmlControls.HtmlTable
> Protected WithEvents ImageFile As
> System.Web.UI.HtmlControls.HtmlInputFile
> Protected WithEvents Upload As System.Web.UI.WebControls.Button
>
> 'NOTE: The following placeholder declaration is required by the Web
> Form
> Designer.
> 'Do not delete or move it.
> Private designerPlaceholderDeclaration As System.Object
>
> Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles MyBase.Init
> 'CODEGEN: This method call is required by the Web Form Designer
> 'Do not modify it using the code editor.
> InitializeComponent()
> End Sub
>
> #End Region
>
> Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles MyBase.Load
> 'Put user code to initialize the page here
>
> 'Loads user controls
> Dim c1 As Control = LoadControl("../userControls/footer.ascx")
> phFooter.Controls.Add(c1)
> Dim c2 As Control =
> LoadControl("../userControls/headerMenu.ascx")
> phHeaderMenu.Controls.Add(c2)
> Dim c3 As Control = LoadControl("../userControls/Header.ascx")
> phHeader.Controls.Add(c3)
> Dim c4 As Control =
> LoadControl("../userControls/QuickLinks.ascx")
> phQuickLinks.Controls.Add(c4)
>
> End Sub
>
>
> Sub UploadData()
> 'Dim ImageFile As System.Web.UI.HtmlControls.HtmlInputFile
> Dim imgstream As Stream = ImageFile.PostedFile.InputStream
> Dim imgdata(ImageFile.PostedFile.ContentLength) As Byte
> imgstream.Read(imgdata, 0, ImageFile.PostedFile.ContentLength)
>
>
> Dim MyConn As New
> SqlConnection(ConfigurationSettings.AppSettings("s trConn"))
> Dim cmd As New SqlCommand("fileUpload", MyConn)
> cmd.CommandType = CommandType.StoredProcedure
>
> Dim titleparam As New SqlParameter("@fileTitle",
> SqlDbType.NVarChar,
> 255)
> Dim descriptionparam As New SqlParameter("@fileDescription",
> SqlDbType.NVarChar, 255)
> Dim Ownerparam As New SqlParameter("@fileOwner",
> SqlDbType.NVarChar,
> 255)
> Dim Officeparam As New SqlParameter("@officeID", SqlDbType.Int)
> Dim typeparam As New SqlParameter("@fileType",
> SqlDbType.NVarChar,
> 100)
> Dim dataparam As New SqlParameter("@fileData", SqlDbType.Image)
>
> 'Dim ImageTitle As System.Web.UI.WebControls.TextBox
> 'Dim fldOwner As System.Web.UI.WebControls.TextBox
> 'Dim fldDescription As System.Web.UI.WebControls.TextBox
> 'Dim ddlOffice As System.Web.UI.WebControls.DropDownList
>
> titleparam.Value = ImageTitle.Text
> descriptionparam.Value = fldDescription.Text
> Ownerparam.Value = fldOwner.Text
> Officeparam.Value = ddlOffice.SelectedIndex
> typeparam.Value = ImageFile.PostedFile.ContentType
> dataparam.Value = imgdata
>
> cmd.Parameters.Add(titleparam)
> cmd.Parameters.Add(descriptionparam)
> cmd.Parameters.Add(Ownerparam)
> cmd.Parameters.Add(Officeparam)
> cmd.Parameters.Add(typeparam)
> cmd.Parameters.Add(dataparam)
>

Nov 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Tim Thomas | last post by:
Hi, I am very new to .NET and am in the process of building my first web application. I will briefly describe what i am trying to achieve: I have a system where suppliers register their...
13
by: Lloyd Sheen | last post by:
I have now spent 5 hours on google/msdn looking for something useful in the creation of user controls for asp.net. The VS 2003 has very limited support for things such as absolute positioning of...
6
by: martin | last post by:
Hi, I am a web page and a web user control. My web user control is placed in my web page using the following directive <%@ Register TagPrefix="uc1" TagName="Header"...
6
by: grist2mill | last post by:
I want to create a standard tool bar that appears on all pages that is a control. The toolbar has a button 'New'. What I wolud like when the user clicks on 'New' depends on the page they are on. I...
5
by: Dave | last post by:
(Second Try) Greetings, I have an asp.net web application that utilizes a User Control at the top of the page. My User Control contains a Server Control menu which will require postbacks. ...
7
by: Smithers | last post by:
I have a non trivial ASP.NET Web application that implements its navigation system via a user control (menu.ascx) placed on every page. It is important to note that the user control that hosts...
4
by: thomson | last post by:
Hi all, i do have a user control with 4 buttons, and all the events are firing properly, My problem is that i need to right an event handler in the user control, which gets fired after a...
8
by: Paul Bromley | last post by:
I am trying to get somewhere with user controls and have come up against an obstacle with using relative paths. I have a dataset in my user control, and have tried to enter the path with the...
8
by: fernandezr | last post by:
I would like to use a user control as a template inside a repeater. Some of the fields in the control should be hidden depending on whether or not there is data. I'm still a ASP .Net newbie so the...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.