Problems understanding User Control???

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tim::..

    Problems understanding User Control???

    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.NullRefe renceException: Object reference not set
    to an instance of an object.

    Source Error:


    Line 80: 'pnlWait.Visibl e = True
    Line 81: Dim objUpload As UploadCnt
    Line 82: objUpload.Uploa dData()
    Line 83:
    Line 84: Else

    I know that the sub works because if I put it staight into the aspx page it
    works fine but as sson as I put the sub into the User Control it seems to
    stop working...

    Can someone please please HELP...

    Thanks

    ASPX PAGE...

    Imports System.Data.Sql Client
    Imports System.Data
    Imports System.IO

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

    'This call is required by the Web Form Designer.
    <System.diagnos tics.debuggerst epthrough()> Private Sub
    InitializeCompo nent()

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

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

    #End Region
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArg s) 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) & "rightMargi n=" & Chr(34) & "0" &
    Chr(34) & "runat=" & Chr(34) & "server" & Chr(34) & ">"))
    Dim UploadCnt As Control =
    LoadControl("../binFileUpload/UploadCnt.ascx" )
    phWait.Controls .Clear()
    phResults.Contr ols.Clear()
    phUploader.Cont rols.Add(Upload Cnt)

    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.ToS tring() &
    "?Upload=Tr ue"

    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.Contr ols.Clear()
    phRefresh.Contr ols.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.Contr ols.Clear()
    phRefresh.Contr ols.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.asc x")
    phUploader.Cont rols.Clear()
    phResults.Contr ols.Clear()
    phWait.Controls .Add(WaitCnt)

    'pnlUploadFile. Visible = False
    'pnlWait.Visibl e = True
    Dim objUpload As UploadCnt
    objUpload.Uploa dData()

    Else

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

    'pnlUploadFile. Visible = True
    phWait.Controls .Clear()
    phResults.Contr ols.Clear()
    phUploader.Cont rols.Add(Upload Cnt)

    Else
    phUploader.Cont rols.Clear()
    phWait.Controls .Clear()
    Dim ResultsCnt As Control =
    LoadControl("../binFileUpload/ResultsCnt.ascx ")
    phResults.Contr ols.Add(Results Cnt)

    End If
    Dim sRefreshURL As String = Request.Url.ToS tring() &
    "?Upload=Fa lse"
    End If
    End Sub

    End Class


    USER CONTROL...

    Imports System.Data.Sql Client
    Imports System.Data
    Imports System.IO


    Public Class UploadCnt
    Inherits System.Web.UI.U serControl

    #Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnos tics.DebuggerSt epThrough()> Private Sub
    InitializeCompo nent()

    End Sub
    Protected WithEvents phHeader As System.Web.UI.W ebControls.Plac eHolder
    Protected WithEvents phHeaderMenu As System.Web.UI.W ebControls.Plac eHolder
    Protected WithEvents ImageTitle As System.Web.UI.W ebControls.Text Box
    Protected WithEvents Image4 As System.Web.UI.W ebControls.Imag e
    Protected WithEvents fldDescription As System.Web.UI.W ebControls.Text Box
    Protected WithEvents Image3 As System.Web.UI.W ebControls.Imag e
    Protected WithEvents Image1 As System.Web.UI.W ebControls.Imag e
    Protected WithEvents fldOwner As System.Web.UI.W ebControls.Text Box
    Protected WithEvents ddlOffice As System.Web.UI.W ebControls.Drop DownList
    Protected WithEvents Image5 As System.Web.UI.W ebControls.Imag e
    Protected WithEvents lblMessage As System.Web.UI.W ebControls.Labe l
    Protected WithEvents urlUploadedFile s As
    System.Web.UI.W ebControls.Hype rLink
    Protected WithEvents phQuickLinks As System.Web.UI.W ebControls.Plac eHolder
    Protected WithEvents phFooter As System.Web.UI.W ebControls.Plac eHolder
    Protected WithEvents Table2 As System.Web.UI.H tmlControls.Htm lTable
    Protected WithEvents ImageFile As System.Web.UI.H tmlControls.Htm lInputFile
    Protected WithEvents Upload As System.Web.UI.W ebControls.Butt on

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

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

    #End Region

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

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

    End Sub


    Sub UploadData()
    'Dim ImageFile As System.Web.UI.H tmlControls.Htm lInputFile
    Dim imgstream As Stream = ImageFile.Poste dFile.InputStre am
    Dim imgdata(ImageFi le.PostedFile.C ontentLength) As Byte
    imgstream.Read( imgdata, 0, ImageFile.Poste dFile.ContentLe ngth)


    Dim MyConn As New
    SqlConnection(C onfigurationSet tings.AppSettin gs("strConn"))
    Dim cmd As New SqlCommand("fil eUpload", MyConn)
    cmd.CommandType = CommandType.Sto redProcedure

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

    'Dim ImageTitle As System.Web.UI.W ebControls.Text Box
    'Dim fldOwner As System.Web.UI.W ebControls.Text Box
    'Dim fldDescription As System.Web.UI.W ebControls.Text Box
    'Dim ddlOffice As System.Web.UI.W ebControls.Drop DownList

    titleparam.Valu e = ImageTitle.Text
    descriptionpara m.Value = fldDescription. Text
    Ownerparam.Valu e = fldOwner.Text
    Officeparam.Val ue = ddlOffice.Selec tedIndex
    typeparam.Value = ImageFile.Poste dFile.ContentTy pe
    dataparam.Value = imgdata

    cmd.Parameters. Add(titleparam)
    cmd.Parameters. Add(description param)
    cmd.Parameters. Add(Ownerparam)
    cmd.Parameters. Add(Officeparam )
    cmd.Parameters. Add(typeparam)
    cmd.Parameters. Add(dataparam)

    MyConn.Open()
    cmd.ExecuteNonQ uery()
    MyConn.Close()
    End Sub


    End Class
  • garethdjames@gmail.com

    #2
    Re: Problems understanding User Control???

    Dim objUpload As UploadCnt
    objUpload.Uploa dData()

    You are not creating a new instance of the class, so it will be null

    Comment

    • Tim::..

      #3
      Re: Problems understanding User Control???

      OK... But I then get the following issue!

      Do I have to create a new instance of every object in the control... Can
      someone please explain this to me... I'm a little new to this!

      Thanks

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

      Source Error:


      Line 114: Sub UploadData()
      Line 115: 'Dim ImageFile As System.Web.UI.H tmlControls.Htm lInputFile
      Line 116: Dim imgstream As Stream = ImageFile.Poste dFile.InputStre am
      Line 117: Dim imgdata(ImageFi le.PostedFile.C ontentLength) As Byte
      Line 118: imgstream.Read( imgdata, 0,
      ImageFile.Poste dFile.ContentLe ngth)



      "garethdjames@g mail.com" wrote:
      [color=blue]
      > Dim objUpload As UploadCnt
      > objUpload.Uploa dData()
      >
      > You are not creating a new instance of the class, so it will be null
      >
      >[/color]

      Comment

      • garethdjames@gmail.com

        #4
        Re: Problems understanding User Control???

        what line does it fail on?

        Comment

        • Tim::..

          #5
          Re: Problems understanding User Control???

          Line 116!


          Thanks

          Comment

          Working...