Populating form fields from a dataset

Discussion in 'ASP .Net' started by Guest, Mar 7, 2005.

  1. Guest

    Guest Guest

    I know that this is probably a simple question, but I can't seem to find out
    how to populate the form fields I have with the results from my stored
    procedure, which I have stored in a datase. I have seen examples of to use
    dataset, datagrids, but how can I populate textboxes and label's with the
    results of my stored procedure?
    Guest, Mar 7, 2005
    1. Advertisements

  2. Hi Stephen,

    You can use the data from the dataset to bind to a textbox or other control
    but you use it somewhat differently. You have to decide which value you want
    to use from the dataset, assuming there's more than one.

    Here's some sample code that might get you started. Let us know if it helps?

    Microsoft MVP [ASP.NET]

    <form id="Form1" method="post" runat="server">
    <asp:textbox id="TextBox1" runat="server"></asp:textbox></p>
    <asp:button id="Button1" runat="server"
    <asp:label id="Label1" runat="server"></asp:label></p>

    Private Sub Page_Load _
    (ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles MyBase.Load
    ' From Ken Cox Microsoft MVP [ASP.NET]
    If Not IsPostBack Then
    ' Create a dataset
    Dim ds As New DataSet
    ' Add a table to the dataset
    ' Create a filter so that we only get one row
    ds.Tables(0).DefaultView.RowFilter = "IntegerValue = 5"
    ' Pass the dataset and an expression to DataBinder.Eval
    ' so that it returns the string called StringValue in
    ' the default dataview
    TextBox1.Text = DataBinder.Eval(ds, _
    ' Bind everything on the page
    End If
    End Sub

    Private Sub Button1_Click _
    (ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles Button1.Click
    Label1.Text = TextBox1.Text
    End Sub

    Function CreateDataSource() As DataTable
    Dim dt As New DataTable
    Dim dr As DataRow
    dt.Columns.Add(New DataColumn _
    ("IntegerValue", GetType(Int32)))
    dt.Columns.Add(New DataColumn _
    ("StringValue", GetType(String)))
    dt.Columns.Add(New DataColumn _
    ("CurrencyValue", GetType(Double)))
    dt.Columns.Add(New DataColumn _
    ("Boolean", GetType(Boolean)))
    Dim i As Integer
    For i = 0 To 5
    dr = dt.NewRow()
    dr(0) = i
    dr(1) = "Item " + i.ToString()
    dr(2) = 1.23 * (i + 1)
    dr(3) = (i = 4)
    Next i
    Return dt
    End Function
    Ken Cox [Microsoft MVP], Mar 7, 2005
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.