can u populate a database table into html table or list and how?

Comments

  • [b][red]This message was edited by raymcd at 2003-4-23 3:17:13[/red][/b][hr]
    : Thanks
    :
    Easiest would be to bind a Datatable to a DataGrid. Create a Connection object, and create a DataAdapter that uses that Connection.
    Create a dataset, and fill from the DataAdapter(DataAdapter.Fill(DataSet)). Once the Dataset has been filled from the dataadapter you can bind a datagrid to a table in the tables collection of the dataset. Simplest would be Datagrid.datasource = DataSet.Tables(0)

    -Ray


  • : Thanks
    :
    Yes u can. However, with HTML table and list u have to do it manually. The easiest solution for both is to use a Repeater and bind it to a DataReader. For this example im going to assume your using a non SQL Server database and the code-behind model. The ficticious database table (Names) will have 3 columns (ID, FirstName, LastName.)

    In The .ASPX Page:
    [code]


    ...



    ID First Name Last Name
    <%# Container.DataItem("ID") %> <%# Container.DataItem("FirstName") %> <%# Container.DataItem("LastName") %>


    ...


    [/code]

    In The vb file (im assuming your doing this with vb) ...

    [code]
    ...

    Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) _
    Handles MyBase.Load

    If Not Page.IsPostBack Then
    Dim objDr As OleDbDataReader = Me.GetNames()
    With rptNames
    .DataSource = objDr
    .DataBind()
    End With
    objDr.Close
    End If
    End Sub

    Private Function GetNames() As OleDbDataReader
    Dim sql As String = "SELECT * FROM Names"

    Dim objCon As New OleDbConnection(connectionString)
    Dim objCmd As New OleDbCommand(sql, objCon)

    objCon.Open
    Return ExecuteReader(CommandBehavior.CloseConnection)
    End Function

    ...
    [/code]

    You can use a repeater to create a list as well:

    An alternate .ASPX Page:
    [code]


    ...



    • <%# Container.DataItem("LastName") & ", " & Container.DataItem("FirstName") %>




    ...


    [/code]

    All in all, if you need to generate a repetative pattern of HTML, a Repeater is the way to go. Of course some web-controls automatically generate the repetative pattern for you. Using a Repeater to build a select options list below would be redundant because the ListBox and DropDownList control perform this duty already.

    Redundant code ...
    [code]







    <%# Container.DataItem("LastName") & ", " & Container.DataItem("FirstName") %>







    [/code]

    Can easily be accomplished this way ...

    In an ASPX page
    [code]
    ...



    ...
    [/code]

    The code behind
    [code]
    ...

    With cboOptions
    .DataSource = Me.GetNames()
    .DataTextField = "FirstName"
    .DataValueField = "ID"
    .DataBind()
    End With

    ...
    [/code]

    Hopefully this all helps you.


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories