locked
databinding a textbox RRS feed

  • Question

  • User-1816311589 posted

    hi there! I want to select couple of data in my sql server and then pass it to several textboxes. can somebody assist me? I really don't know how to code this.thanks:D

    Sunday, June 28, 2009 1:50 PM

Answers

  • User626880745 posted

    here's sample code for you: http://www.aspfree.com/c/a/Database/ADO-NET-101-SqlDataReader/3/

    since you have textboxes you will have to do this in the reader loop:

    While dr.Read
                'here you retrieve the row pertaining to the first
                'column()
                'Response.Write(dr.Item(1) & "<br/>")
               
    TextBox1.Text = dr("yourDataColumn").ToString()

            End While


    if you want you can follow the complete tutorial: http://www.aspfree.com/c/a/Database/ADO-NET-101-SqlDataReader/

    if you want help on connection string for your specific case: http://www.connectionstrings.com/

    if you work with C# you can convert your code here

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, June 28, 2009 2:20 PM
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, June 28, 2009 2:36 PM
  • User232384818 posted

    in addition to the answers you already got, I'd like to provide my own contribution too Laughing

    perhaps simply something different? But if you aren't afraid to try something different, then continue to the sample code below Wink

    note how databind is called invidually on the textboxes, this is necessary to evaluate the databinder expressions. Doing things this way allows you to have a much more flexible declarative code, you simply need to provide your data in a collection.

    One thing you may note from the sample code below in Page_load is that databind is called invidually on the textboxes, this can get easily boring, so in case they are all nested in a common control eg, say a panel, you can call databind on the panel directly. Or simply call DataBind() on the page itself :

      <asp:Panel ID="Panel1" runat="server">
                <asp:TextBox ID="TextBox1" runat="server" Text='<%#DataBinder.Eval(dv[0], "FirstName") %>'></asp:TextBox>
                <br />
                <asp:TextBox ID="TextBox2" runat="server" Text='<%#DataBinder.Eval(dv[0], "LastName") %>'></asp:TextBox>
            </asp:Panel>

    basically a call to databind will iterate through all children and call databind on those too. so in this case you as the code above you'd only be calling databind on the panel eg : Panel1.DataBind();


    lastly you may just use a datacontrol which is made for this very purpose. But i guess you may have your reasons not to use one at this point. This code in a way tries to mirror the datacontrols with the difference that this will allow you more flexibility in where your put the controls and there will no naming issues.

    Ofcourse a datcontrol brings much more value as it allows more than just displaying data. it also eases in CRUD operations :-)

    <%@ Page Language="C#" %>

    <%@ Import Namespace="System.Data" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
    Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <script runat="server">
        DataView dv;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CreateDataSource();
                TextBox1.DataBind();
                TextBox2.DataBind();
            }
        }
        //dummy data
        ICollection CreateDataSource()
        {
            DataTable dt = new DataTable();
            DataRow dr;

            dt.Columns.Add(new DataColumn("FirstName", typeof(string)));
            dt.Columns.Add(new DataColumn("LastName", typeof(string)));

            dr = dt.NewRow();
            dr[0] = "John";
            dr[1] = "Adams";

            dt.Rows.Add(dr);

            dv = new DataView(dt);
            return dv;
        }

    </script>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:TextBox ID="TextBox1" runat="server"
            Text='<%#DataBinder.Eval(dv[0], "FirstName") %>'></asp:TextBox>
            <br />
            <asp:TextBox ID="TextBox2" runat="server"
            Text='<%#DataBinder.Eval(dv[0], "LastName") %>'></asp:TextBox>
        </div>
        </form>
    </body>
    </html>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, June 28, 2009 3:57 PM

All replies

  • User626880745 posted

    here's sample code for you: http://www.aspfree.com/c/a/Database/ADO-NET-101-SqlDataReader/3/

    since you have textboxes you will have to do this in the reader loop:

    While dr.Read
                'here you retrieve the row pertaining to the first
                'column()
                'Response.Write(dr.Item(1) & "<br/>")
               
    TextBox1.Text = dr("yourDataColumn").ToString()

            End While


    if you want you can follow the complete tutorial: http://www.aspfree.com/c/a/Database/ADO-NET-101-SqlDataReader/

    if you want help on connection string for your specific case: http://www.connectionstrings.com/

    if you work with C# you can convert your code here

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, June 28, 2009 2:20 PM
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, June 28, 2009 2:36 PM
  • User232384818 posted

    in addition to the answers you already got, I'd like to provide my own contribution too Laughing

    perhaps simply something different? But if you aren't afraid to try something different, then continue to the sample code below Wink

    note how databind is called invidually on the textboxes, this is necessary to evaluate the databinder expressions. Doing things this way allows you to have a much more flexible declarative code, you simply need to provide your data in a collection.

    One thing you may note from the sample code below in Page_load is that databind is called invidually on the textboxes, this can get easily boring, so in case they are all nested in a common control eg, say a panel, you can call databind on the panel directly. Or simply call DataBind() on the page itself :

      <asp:Panel ID="Panel1" runat="server">
                <asp:TextBox ID="TextBox1" runat="server" Text='<%#DataBinder.Eval(dv[0], "FirstName") %>'></asp:TextBox>
                <br />
                <asp:TextBox ID="TextBox2" runat="server" Text='<%#DataBinder.Eval(dv[0], "LastName") %>'></asp:TextBox>
            </asp:Panel>

    basically a call to databind will iterate through all children and call databind on those too. so in this case you as the code above you'd only be calling databind on the panel eg : Panel1.DataBind();


    lastly you may just use a datacontrol which is made for this very purpose. But i guess you may have your reasons not to use one at this point. This code in a way tries to mirror the datacontrols with the difference that this will allow you more flexibility in where your put the controls and there will no naming issues.

    Ofcourse a datcontrol brings much more value as it allows more than just displaying data. it also eases in CRUD operations :-)

    <%@ Page Language="C#" %>

    <%@ Import Namespace="System.Data" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
    Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <script runat="server">
        DataView dv;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CreateDataSource();
                TextBox1.DataBind();
                TextBox2.DataBind();
            }
        }
        //dummy data
        ICollection CreateDataSource()
        {
            DataTable dt = new DataTable();
            DataRow dr;

            dt.Columns.Add(new DataColumn("FirstName", typeof(string)));
            dt.Columns.Add(new DataColumn("LastName", typeof(string)));

            dr = dt.NewRow();
            dr[0] = "John";
            dr[1] = "Adams";

            dt.Rows.Add(dr);

            dv = new DataView(dt);
            return dv;
        }

    </script>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:TextBox ID="TextBox1" runat="server"
            Text='<%#DataBinder.Eval(dv[0], "FirstName") %>'></asp:TextBox>
            <br />
            <asp:TextBox ID="TextBox2" runat="server"
            Text='<%#DataBinder.Eval(dv[0], "LastName") %>'></asp:TextBox>
        </div>
        </form>
    </body>
    </html>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, June 28, 2009 3:57 PM