Listbox controls

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Seok Bee

    #1

    Listbox controls

    Dear Experts,

    I am having 1 listbox control in my web form and trying to retrieve the
    items and load into it. I want to list out those uploaded files into the
    database and list out in the listbox control. My code is as follow:

    -------------------------------------------
    Protected Sub btnUpload_Click (ByVal sender As Object, ByVal e As
    System.EventArg s) Handles btnUpload.Click
    litMsgBox.Text = ""
    If FileUpload1.Has File Then
    'Upload Attachment to Server
    If Not FileUpload1.Fil eContent Is Nothing Then
    'perform validation logic before updating the database -
    i.e. if file size is
    With FileUpload1.Fil eContent
    Dim abyContent(CTyp e(.Length, Integer)) As Byte
    'allocate buffer for file data
    .Read(abyConten t, 0, .Length)

    'Update the attach filename into database
    InsertFile(File Upload1.FileNam e, abyContent)

    'Update the attach filename into server folder
    SaveFile(FileUp load1.FileName)

    'btnUpload.Enab led = False
    'btnDelete.Visi ble = True
    'strAllAttachFi les = strAllAttachFil es &
    FileUpload1.Fil eName & ","

    'txtRequest.Tex t = strAllAttachFil es
    End With
    End If

    Else
    strMsgBox = "<script language='Javas cript'>alert("" No File to be
    uploaded"")"
    strMsgBox = strMsgBox & Chr(60) & "/script>"
    litMsgBox.Text = strMsgBox
    End If
    LoadAttachments (intRequestID)

    End Sub

    Private Sub InsertFile(ByVa l strFilename As String, ByVal abyContent As
    Byte())
    'Dim oConnection As New OleDbConnection (strConnectionS tring)
    'Dim intRequestID As Integer
    Try
    DatabaseConnect ion()
    intRequestID = Int(txtReqNo.Te xt)
    Dim strFileUploadQu ery As String = "INSERT INTO Attachments
    (Attachments, FileContent, RequestID) VALUES (?,?," & intRequestID & ")"
    Dim oCommand As New OleDbCommand(st rFileUploadQuer y, dbConnection)
    Dim oParameter As OleDbParameter = Nothing
    oParameter = New OleDbParameter( "?", OleDbType.VarCh ar)
    oParameter.Valu e = strFilename
    oParameter.Dire ction = ParameterDirect ion.Input
    oCommand.Parame ters.Add(oParam eter)

    oParameter = New OleDbParameter( "?", OleDbType.VarBi nary)
    oParameter.Valu e = abyContent
    oParameter.Dire ction = ParameterDirect ion.Input
    oCommand.Parame ters.Add(oParam eter)

    oCommand.Comman dTimeout = 120
    oCommand.Comman dType = CommandType.Tex t

    oCommand.Execut eNonQuery()


    Catch ex As Exception
    Throw ex

    Finally
    DatabaseDisconn ection()
    LoadAttachments (intRequestID)
    End Try
    End Sub

    Public Sub LoadAttachments (ByVal intReqID As Integer)
    Try
    DatabaseConnect ion()
    Dim strSQLFileAttac h As String = "SELECT * FROM Attachments
    WHERE RequestID = " & intReqID

    Dim FileAttachComma nd As New
    Data.OleDb.OleD bCommand(strSQL FileAttach, dbConnection)
    Dim FileAttachReade r As Data.OleDb.OleD bDataReader =
    FileAttachComma nd.ExecuteReade r()
    lstAttachments. Items.Clear()
    FileAttachReade r.Read()

    lstAttachments. DataValueField = "Attachment ID"
    lstAttachments. DataTextField = "Attachment s"
    lstAttachments. DataSource = FileAttachReade r
    lstAttachments. DataBind()

    FileAttachReade r.Close()

    Catch ex As Exception
    TextBox1.Text = ex.Message
    Finally
    DatabaseDisconn ection()
    End Try

    End Sub

    --------------------------------------------------

    My problem is when I retrieve the data, some how the 1st row of the data
    does not appear. May I know where and how to get this problem solve?

    Many thanks in advance.

    Regards,
    SB

  • Chris

    #2
    Re: Listbox controls

    Seok Bee wrote:[color=blue]
    > Dear Experts,
    >
    > I am having 1 listbox control in my web form and trying to retrieve the
    > items and load into it. I want to list out those uploaded files into the
    > database and list out in the listbox control. My code is as follow:
    >
    > -------------------------------------------
    > Protected Sub btnUpload_Click (ByVal sender As Object, ByVal e As
    > System.EventArg s) Handles btnUpload.Click
    > litMsgBox.Text = ""
    > If FileUpload1.Has File Then
    > 'Upload Attachment to Server
    > If Not FileUpload1.Fil eContent Is Nothing Then
    > 'perform validation logic before updating the database -
    > i.e. if file size is
    > With FileUpload1.Fil eContent
    > Dim abyContent(CTyp e(.Length, Integer)) As Byte
    > 'allocate buffer for file data
    > .Read(abyConten t, 0, .Length)
    >
    > 'Update the attach filename into database
    > InsertFile(File Upload1.FileNam e, abyContent)
    >
    > 'Update the attach filename into server folder
    > SaveFile(FileUp load1.FileName)
    >
    > 'btnUpload.Enab led = False
    > 'btnDelete.Visi ble = True
    > 'strAllAttachFi les = strAllAttachFil es &
    > FileUpload1.Fil eName & ","
    >
    > 'txtRequest.Tex t = strAllAttachFil es
    > End With
    > End If
    >
    > Else
    > strMsgBox = "<script language='Javas cript'>alert("" No File to be
    > uploaded"")"
    > strMsgBox = strMsgBox & Chr(60) & "/script>"
    > litMsgBox.Text = strMsgBox
    > End If
    > LoadAttachments (intRequestID)
    >
    > End Sub
    >
    > Private Sub InsertFile(ByVa l strFilename As String, ByVal abyContent As
    > Byte())
    > 'Dim oConnection As New OleDbConnection (strConnectionS tring)
    > 'Dim intRequestID As Integer
    > Try
    > DatabaseConnect ion()
    > intRequestID = Int(txtReqNo.Te xt)
    > Dim strFileUploadQu ery As String = "INSERT INTO Attachments
    > (Attachments, FileContent, RequestID) VALUES (?,?," & intRequestID & ")"
    > Dim oCommand As New OleDbCommand(st rFileUploadQuer y, dbConnection)
    > Dim oParameter As OleDbParameter = Nothing
    > oParameter = New OleDbParameter( "?", OleDbType.VarCh ar)
    > oParameter.Valu e = strFilename
    > oParameter.Dire ction = ParameterDirect ion.Input
    > oCommand.Parame ters.Add(oParam eter)
    >
    > oParameter = New OleDbParameter( "?", OleDbType.VarBi nary)
    > oParameter.Valu e = abyContent
    > oParameter.Dire ction = ParameterDirect ion.Input
    > oCommand.Parame ters.Add(oParam eter)
    >
    > oCommand.Comman dTimeout = 120
    > oCommand.Comman dType = CommandType.Tex t
    >
    > oCommand.Execut eNonQuery()
    >
    >
    > Catch ex As Exception
    > Throw ex
    >
    > Finally
    > DatabaseDisconn ection()
    > LoadAttachments (intRequestID)
    > End Try
    > End Sub
    >
    > Public Sub LoadAttachments (ByVal intReqID As Integer)
    > Try
    > DatabaseConnect ion()
    > Dim strSQLFileAttac h As String = "SELECT * FROM Attachments
    > WHERE RequestID = " & intReqID
    >
    > Dim FileAttachComma nd As New
    > Data.OleDb.OleD bCommand(strSQL FileAttach, dbConnection)
    > Dim FileAttachReade r As Data.OleDb.OleD bDataReader =
    > FileAttachComma nd.ExecuteReade r()
    > lstAttachments. Items.Clear()
    > FileAttachReade r.Read()
    >
    > lstAttachments. DataValueField = "Attachment ID"
    > lstAttachments. DataTextField = "Attachment s"
    > lstAttachments. DataSource = FileAttachReade r
    > lstAttachments. DataBind()
    >
    > FileAttachReade r.Close()
    >
    > Catch ex As Exception
    > TextBox1.Text = ex.Message
    > Finally
    > DatabaseDisconn ection()
    > End Try
    >
    > End Sub
    >
    > --------------------------------------------------
    >
    > My problem is when I retrieve the data, some how the 1st row of the data
    > does not appear. May I know where and how to get this problem solve?
    >
    > Many thanks in advance.
    >
    > Regards,
    > SB
    >[/color]

    I don't have the methods in front of me, but you need to set the
    selected index to whichever item you want shown after you do the databind.

    Chris

    Comment

    • Terry Burns

      #3
      Re: Listbox controls

      dropdownList.Se lectedIndex=0


      --
      Terry Burns

      "Seok Bee" <seokbee@yahoo. com> wrote in message
      news:97457CBB-5C7F-44E1-B657-DA3BD262F3F2@mi crosoft.com...[color=blue]
      > Dear Experts,
      >
      > I am having 1 listbox control in my web form and trying to retrieve the
      > items and load into it. I want to list out those uploaded files into the
      > database and list out in the listbox control. My code is as follow:
      >
      > -------------------------------------------
      > Protected Sub btnUpload_Click (ByVal sender As Object, ByVal e As
      > System.EventArg s) Handles btnUpload.Click
      > litMsgBox.Text = ""
      > If FileUpload1.Has File Then
      > 'Upload Attachment to Server
      > If Not FileUpload1.Fil eContent Is Nothing Then
      > 'perform validation logic before updating the database -
      > i.e. if file size is
      > With FileUpload1.Fil eContent
      > Dim abyContent(CTyp e(.Length, Integer)) As Byte
      > 'allocate buffer for file data
      > .Read(abyConten t, 0, .Length)
      >
      > 'Update the attach filename into database
      > InsertFile(File Upload1.FileNam e, abyContent)
      >
      > 'Update the attach filename into server folder
      > SaveFile(FileUp load1.FileName)
      >
      > 'btnUpload.Enab led = False
      > 'btnDelete.Visi ble = True
      > 'strAllAttachFi les = strAllAttachFil es &
      > FileUpload1.Fil eName & ","
      >
      > 'txtRequest.Tex t = strAllAttachFil es
      > End With
      > End If
      >
      > Else
      > strMsgBox = "<script language='Javas cript'>alert("" No File to
      > be
      > uploaded"")"
      > strMsgBox = strMsgBox & Chr(60) & "/script>"
      > litMsgBox.Text = strMsgBox
      > End If
      > LoadAttachments (intRequestID)
      >
      > End Sub
      >
      > Private Sub InsertFile(ByVa l strFilename As String, ByVal abyContent As
      > Byte())
      > 'Dim oConnection As New OleDbConnection (strConnectionS tring)
      > 'Dim intRequestID As Integer
      > Try
      > DatabaseConnect ion()
      > intRequestID = Int(txtReqNo.Te xt)
      > Dim strFileUploadQu ery As String = "INSERT INTO Attachments
      > (Attachments, FileContent, RequestID) VALUES (?,?," & intRequestID & ")"
      > Dim oCommand As New OleDbCommand(st rFileUploadQuer y,
      > dbConnection)
      > Dim oParameter As OleDbParameter = Nothing
      > oParameter = New OleDbParameter( "?", OleDbType.VarCh ar)
      > oParameter.Valu e = strFilename
      > oParameter.Dire ction = ParameterDirect ion.Input
      > oCommand.Parame ters.Add(oParam eter)
      >
      > oParameter = New OleDbParameter( "?", OleDbType.VarBi nary)
      > oParameter.Valu e = abyContent
      > oParameter.Dire ction = ParameterDirect ion.Input
      > oCommand.Parame ters.Add(oParam eter)
      >
      > oCommand.Comman dTimeout = 120
      > oCommand.Comman dType = CommandType.Tex t
      >
      > oCommand.Execut eNonQuery()
      >
      >
      > Catch ex As Exception
      > Throw ex
      >
      > Finally
      > DatabaseDisconn ection()
      > LoadAttachments (intRequestID)
      > End Try
      > End Sub
      >
      > Public Sub LoadAttachments (ByVal intReqID As Integer)
      > Try
      > DatabaseConnect ion()
      > Dim strSQLFileAttac h As String = "SELECT * FROM Attachments
      > WHERE RequestID = " & intReqID
      >
      > Dim FileAttachComma nd As New
      > Data.OleDb.OleD bCommand(strSQL FileAttach, dbConnection)
      > Dim FileAttachReade r As Data.OleDb.OleD bDataReader =
      > FileAttachComma nd.ExecuteReade r()
      > lstAttachments. Items.Clear()
      > FileAttachReade r.Read()
      >
      > lstAttachments. DataValueField = "Attachment ID"
      > lstAttachments. DataTextField = "Attachment s"
      > lstAttachments. DataSource = FileAttachReade r
      > lstAttachments. DataBind()
      >
      > FileAttachReade r.Close()
      >
      > Catch ex As Exception
      > TextBox1.Text = ex.Message
      > Finally
      > DatabaseDisconn ection()
      > End Try
      >
      > End Sub
      >
      > --------------------------------------------------
      >
      > My problem is when I retrieve the data, some how the 1st row of the data
      > does not appear. May I know where and how to get this problem solve?
      >
      > Many thanks in advance.
      >
      > Regards,
      > SB
      >[/color]


      Comment

      • Seok Bee

        #4
        Re: Listbox controls

        Thanks Chris and Terry for your replies.

        However, my problem is still unable to resolve when I put in the following
        statement in my code:

        lstAttachments. SelectedIndex=0

        When the first time I upload the file into the database, the SELECT
        statement is not able to retrieve the 1st file I uploaded. Then I went on to
        upload the 2nd file and so on, the SELECT statement able to retrieve and
        display the record(s) from 2nd file onward into the listbox control.
        So I would like to know, what is the reason being such the way. In another
        webform, I used the DataSource and bound it to the listbox control, it able
        to view/list all the records into the control box. Please advise...

        Thanks in advance.

        Regards,
        SB

        "Terry Burns" wrote:
        [color=blue]
        > dropdownList.Se lectedIndex=0
        >
        >
        > --
        > Terry Burns
        > http://TrainingOn.net
        > "Seok Bee" <seokbee@yahoo. com> wrote in message
        > news:97457CBB-5C7F-44E1-B657-DA3BD262F3F2@mi crosoft.com...[color=green]
        > > Dear Experts,
        > >
        > > I am having 1 listbox control in my web form and trying to retrieve the
        > > items and load into it. I want to list out those uploaded files into the
        > > database and list out in the listbox control. My code is as follow:
        > >
        > > -------------------------------------------
        > > Protected Sub btnUpload_Click (ByVal sender As Object, ByVal e As
        > > System.EventArg s) Handles btnUpload.Click
        > > litMsgBox.Text = ""
        > > If FileUpload1.Has File Then
        > > 'Upload Attachment to Server
        > > If Not FileUpload1.Fil eContent Is Nothing Then
        > > 'perform validation logic before updating the database -
        > > i.e. if file size is
        > > With FileUpload1.Fil eContent
        > > Dim abyContent(CTyp e(.Length, Integer)) As Byte
        > > 'allocate buffer for file data
        > > .Read(abyConten t, 0, .Length)
        > >
        > > 'Update the attach filename into database
        > > InsertFile(File Upload1.FileNam e, abyContent)
        > >
        > > 'Update the attach filename into server folder
        > > SaveFile(FileUp load1.FileName)
        > >
        > > 'btnUpload.Enab led = False
        > > 'btnDelete.Visi ble = True
        > > 'strAllAttachFi les = strAllAttachFil es &
        > > FileUpload1.Fil eName & ","
        > >
        > > 'txtRequest.Tex t = strAllAttachFil es
        > > End With
        > > End If
        > >
        > > Else
        > > strMsgBox = "<script language='Javas cript'>alert("" No File to
        > > be
        > > uploaded"")"
        > > strMsgBox = strMsgBox & Chr(60) & "/script>"
        > > litMsgBox.Text = strMsgBox
        > > End If
        > > LoadAttachments (intRequestID)
        > >
        > > End Sub
        > >
        > > Private Sub InsertFile(ByVa l strFilename As String, ByVal abyContent As
        > > Byte())
        > > 'Dim oConnection As New OleDbConnection (strConnectionS tring)
        > > 'Dim intRequestID As Integer
        > > Try
        > > DatabaseConnect ion()
        > > intRequestID = Int(txtReqNo.Te xt)
        > > Dim strFileUploadQu ery As String = "INSERT INTO Attachments
        > > (Attachments, FileContent, RequestID) VALUES (?,?," & intRequestID & ")"
        > > Dim oCommand As New OleDbCommand(st rFileUploadQuer y,
        > > dbConnection)
        > > Dim oParameter As OleDbParameter = Nothing
        > > oParameter = New OleDbParameter( "?", OleDbType.VarCh ar)
        > > oParameter.Valu e = strFilename
        > > oParameter.Dire ction = ParameterDirect ion.Input
        > > oCommand.Parame ters.Add(oParam eter)
        > >
        > > oParameter = New OleDbParameter( "?", OleDbType.VarBi nary)
        > > oParameter.Valu e = abyContent
        > > oParameter.Dire ction = ParameterDirect ion.Input
        > > oCommand.Parame ters.Add(oParam eter)
        > >
        > > oCommand.Comman dTimeout = 120
        > > oCommand.Comman dType = CommandType.Tex t
        > >
        > > oCommand.Execut eNonQuery()
        > >
        > >
        > > Catch ex As Exception
        > > Throw ex
        > >
        > > Finally
        > > DatabaseDisconn ection()
        > > LoadAttachments (intRequestID)
        > > End Try
        > > End Sub
        > >
        > > Public Sub LoadAttachments (ByVal intReqID As Integer)
        > > Try
        > > DatabaseConnect ion()
        > > Dim strSQLFileAttac h As String = "SELECT * FROM Attachments
        > > WHERE RequestID = " & intReqID
        > >
        > > Dim FileAttachComma nd As New
        > > Data.OleDb.OleD bCommand(strSQL FileAttach, dbConnection)
        > > Dim FileAttachReade r As Data.OleDb.OleD bDataReader =
        > > FileAttachComma nd.ExecuteReade r()
        > > lstAttachments. Items.Clear()
        > > FileAttachReade r.Read()
        > >
        > > lstAttachments. DataValueField = "Attachment ID"
        > > lstAttachments. DataTextField = "Attachment s"
        > > lstAttachments. DataSource = FileAttachReade r
        > > lstAttachments. DataBind()
        > >
        > > FileAttachReade r.Close()
        > >
        > > Catch ex As Exception
        > > TextBox1.Text = ex.Message
        > > Finally
        > > DatabaseDisconn ection()
        > > End Try
        > >
        > > End Sub
        > >
        > > --------------------------------------------------
        > >
        > > My problem is when I retrieve the data, some how the 1st row of the data
        > > does not appear. May I know where and how to get this problem solve?
        > >
        > > Many thanks in advance.
        > >
        > > Regards,
        > > SB
        > >[/color]
        >
        >
        >[/color]

        Comment

        • Fr15ky

          #5
          Re: Listbox controls

          I am not seeing anyplace where the ListBox's Index is being checked.
          Could it be that it is defaulting to 1 instead of to 0?

          When I have had these type of troubles, it seems to be common that I am
          not checking the 0 'row' of the List.

          Mark Dahl

          Seok Bee wrote:[color=blue]
          > My problem is when I retrieve the data, some how the 1st row of the data
          > does not appear. May I know where and how to get this problem solve?
          >
          > Many thanks in advance.
          >
          > Regards,
          > SB[/color]

          Comment

          Working...