How Do I add records to a Listbox control ?

Discussion in 'ASP .Net' started by Gordon, Nov 29, 2003.

  1. Gordon

    Gordon Guest

    Hi;

    I am a novice asp.net developer who would like to create
    a dataset-table and then use the datatable.select cmd. to
    look up a value passed from the web calender control. The
    value(s) would then be used to populate a listbox on a
    web form. I keep getting an error that says that in the
    datatable.SELECT statement i.e. mytable.select("column1 =
    = '" & aVariable & "'"), that the column1 can't be found.
    I can run this program code in a windows program and it
    works fine. What am I missing, code wise, to make the
    code run under ASP.net ?

    Here is the following code that I tried. It is
    called from the date selected event of the web calender
    control.

    Thanks,

    Gordon

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

    Public Sub SetUpDataBinding()

    Dim datConn As String = "Integrated
    Security=SSPI;Initial Catalog=DocsDates;Data Source=
    (local)\vsdotnet"
    Dim cnDocDates As New SqlConnection(datConn)

    Try

    Dim cmdGetCases As New SqlCommand
    ("CMasterSelectCommand", cnDocDates)
    Dim daCases As New SqlDataAdapter("SELECT
    case_id, lname, case_status from CaseMaster", cnDocDates)
    Dim daDocs As New SqlDataAdapter("SELECT
    case_id, document_nme, doc_dte_start, " & _
    "doc_dte_due, comments from CaseDocs where
    case_id in " & _
    "(SELECT case_id from CaseMaster)",
    cnDocDates)
    Dim ds As New DataSet
    daDocs.Fill(dsCases, "tblCaseDocs")
    Session("dsCases") = dsCases

    holdDte = Today


    Catch ex As System.Data.SqlClient.SqlException

    If
    HttpContext.Current.Request.UserHostAddress = "129.0.0.5"
    Then
    Session("CurrentError") = ex.Message
    Else
    Session("CurrentError") = "Error
    processing page."
    End If
    Server.Transfer("ApplicationError.aspx")
    Finally
    ' cnDocDates.Close()
    End Try

    End Sub


    Public Sub ListBoxFill() ((( This called when a date
    is selected on the calendar web control )))))

    Dim myCol As DataColumn
    Dim myRow As DataRow
    Dim currRows() As DataRow


    Dim nRow As DataRow()
    Dim irow As DataRow

    nRow = tblCaseDocs.Select("doc_dte_due = '" &
    holdDte.ToString & "'") ((( I get an error on the web page
    when I click on a date saying that the table column
    [doc_dte_due] isn't found ?? )))

    ListBox1.Items.Clear()

    For Each irow In nRow
    ListBox1.Items.Add(irow("case_id") & " " &
    irow("Document_Nme"))
    Next

    ListBox1.DataBind() (((( Do I need this ? )))

    End Sub
     
    Gordon, Nov 29, 2003
    #1
    1. Advertising

  2. Gordon

    Alvin Bruney Guest

    ListBox1.DataBind() (((( Do I need this ? )))
    No you don't

    A dataset is an in-memory representation of the datasource. A datatable is
    representation of a particular table. So there wouldn't really be a
    dataset-table.

    For the select to work it has to work on the actual name of the column so if
    your select used "column_date" then you would not be allowed to use column1
    you would have to use the "column_date" string to identify the column.

    What you will need to do is something like this, since the dataset is
    already stored in dsCases, just retrieve it and bind it to the listbox,
    there is no need to go back to the database for it.

    //c# code btw
    //pull it out of session and cast it back as a dataset because it is plain
    object
    DataSet ds = (DataSet) Session("dsCases")

    //tell it which field you want to display in the listbox because it doesnt
    know
    ListBox1.DataTextField = "column_name_from_select_query"

    //tell it where the data is
    ListBox1.DataSource = ds;

    //then tell it to display itself
    ListBox1.DataBind()



    --
    Regards,
    Alvin Bruney
    Got Tidbits? Get it here
    www.networkip.net/tidbits
    "Gordon" <> wrote in message
    news:95f001c3b696$cefc0b80$...
    > Hi;
    >
    > I am a novice asp.net developer who would like to create
    > a dataset-table and then use the datatable.select cmd. to
    > look up a value passed from the web calender control. The
    > value(s) would then be used to populate a listbox on a
    > web form. I keep getting an error that says that in the
    > datatable.SELECT statement i.e. mytable.select("column1 =
    > = '" & aVariable & "'"), that the column1 can't be found.
    > I can run this program code in a windows program and it
    > works fine. What am I missing, code wise, to make the
    > code run under ASP.net ?
    >
    > Here is the following code that I tried. It is
    > called from the date selected event of the web calender
    > control.
    >
    > Thanks,
    >
    > Gordon
    >
    > ---------------------------------------------------------
    >
    > Public Sub SetUpDataBinding()
    >
    > Dim datConn As String = "Integrated
    > Security=SSPI;Initial Catalog=DocsDates;Data Source=
    > (local)\vsdotnet"
    > Dim cnDocDates As New SqlConnection(datConn)
    >
    > Try
    >
    > Dim cmdGetCases As New SqlCommand
    > ("CMasterSelectCommand", cnDocDates)
    > Dim daCases As New SqlDataAdapter("SELECT
    > case_id, lname, case_status from CaseMaster", cnDocDates)
    > Dim daDocs As New SqlDataAdapter("SELECT
    > case_id, document_nme, doc_dte_start, " & _
    > "doc_dte_due, comments from CaseDocs where
    > case_id in " & _
    > "(SELECT case_id from CaseMaster)",
    > cnDocDates)
    > Dim ds As New DataSet
    > daDocs.Fill(dsCases, "tblCaseDocs")
    > Session("dsCases") = dsCases
    >
    > holdDte = Today
    >
    >
    > Catch ex As System.Data.SqlClient.SqlException
    >
    > If
    > HttpContext.Current.Request.UserHostAddress = "129.0.0.5"
    > Then
    > Session("CurrentError") = ex.Message
    > Else
    > Session("CurrentError") = "Error
    > processing page."
    > End If
    > Server.Transfer("ApplicationError.aspx")
    > Finally
    > ' cnDocDates.Close()
    > End Try
    >
    > End Sub
    >
    >
    > Public Sub ListBoxFill() ((( This called when a date
    > is selected on the calendar web control )))))
    >
    > Dim myCol As DataColumn
    > Dim myRow As DataRow
    > Dim currRows() As DataRow
    >
    >
    > Dim nRow As DataRow()
    > Dim irow As DataRow
    >
    > nRow = tblCaseDocs.Select("doc_dte_due = '" &
    > holdDte.ToString & "'") ((( I get an error on the web page
    > when I click on a date saying that the table column
    > [doc_dte_due] isn't found ?? )))
    >
    > ListBox1.Items.Clear()
    >
    > For Each irow In nRow
    > ListBox1.Items.Add(irow("case_id") & " " &
    > irow("Document_Nme"))
    > Next
    >
    > ListBox1.DataBind() (((( Do I need this ? )))
    >
    > End Sub
    >
     
    Alvin Bruney, Nov 29, 2003
    #2
    1. Advertising

  3. Gordon

    Gordon Guest

    Thanks for your help Alvin.






    >-----Original Message-----
    >Hi;
    >
    >I am a novice asp.net developer who would like to create
    >a dataset-table and then use the datatable.select cmd. to
    >look up a value passed from the web calender control. The
    >value(s) would then be used to populate a listbox on a
    >web form. I keep getting an error that says that in the
    >datatable.SELECT statement i.e. mytable.select("column1 =
    >= '" & aVariable & "'"), that the column1 can't be

    found.
    >I can run this program code in a windows program and it
    >works fine. What am I missing, code wise, to make the
    >code run under ASP.net ?
    >
    >Here is the following code that I tried. It is
    >called from the date selected event of the web calender
    >control.
    >
    >Thanks,
    >
    >Gordon
    >
    >---------------------------------------------------------
    >
    > Public Sub SetUpDataBinding()
    >
    > Dim datConn As String = "Integrated
    >Security=SSPI;Initial Catalog=DocsDates;Data Source=
    >(local)\vsdotnet"
    > Dim cnDocDates As New SqlConnection(datConn)
    >
    > Try
    >
    > Dim cmdGetCases As New SqlCommand
    >("CMasterSelectCommand", cnDocDates)
    > Dim daCases As New SqlDataAdapter("SELECT
    >case_id, lname, case_status from CaseMaster", cnDocDates)
    > Dim daDocs As New SqlDataAdapter("SELECT
    >case_id, document_nme, doc_dte_start, " & _
    > "doc_dte_due, comments from CaseDocs where
    >case_id in " & _
    > "(SELECT case_id from CaseMaster)",
    >cnDocDates)
    > Dim ds As New DataSet
    > daDocs.Fill(dsCases, "tblCaseDocs")
    > Session("dsCases") = dsCases
    >
    > holdDte = Today
    >
    >
    > Catch ex As System.Data.SqlClient.SqlException
    >
    > If
    >HttpContext.Current.Request.UserHostAddress

    = "129.0.0.5"
    >Then
    > Session("CurrentError") = ex.Message
    > Else
    > Session("CurrentError") = "Error
    >processing page."
    > End If
    > Server.Transfer("ApplicationError.aspx")
    > Finally
    > ' cnDocDates.Close()
    > End Try
    >
    > End Sub
    >
    >
    > Public Sub ListBoxFill() ((( This called when a

    date
    >is selected on the calendar web control )))))
    >
    > Dim myCol As DataColumn
    > Dim myRow As DataRow
    > Dim currRows() As DataRow
    >
    >
    > Dim nRow As DataRow()
    > Dim irow As DataRow
    >
    > nRow = tblCaseDocs.Select("doc_dte_due = '" &
    >holdDte.ToString & "'") ((( I get an error on the web

    page
    >when I click on a date saying that the table column
    >[doc_dte_due] isn't found ?? )))
    >
    > ListBox1.Items.Clear()
    >
    > For Each irow In nRow
    > ListBox1.Items.Add(irow("case_id") & " " &
    >irow("Document_Nme"))
    > Next
    >
    > ListBox1.DataBind() (((( Do I need this ? )))
    >
    > End Sub
    >
    >.
    >
     
    Gordon, Dec 2, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Luke Airig
    Replies:
    0
    Views:
    795
    Luke Airig
    Dec 31, 2003
  2. James Radke
    Replies:
    5
    Views:
    224
    Jacob Yang [MSFT]
    Oct 20, 2003
  3. Dan

    Delete records or update records

    Dan, May 10, 2004, in forum: ASP General
    Replies:
    1
    Views:
    466
    Ray at
    May 10, 2004
  4. Replies:
    3
    Views:
    666
    Anthony Jones
    Nov 2, 2006
  5. mc
    Replies:
    0
    Views:
    235
Loading...

Share This Page