Geeks With Blogs

News

Microsoft MVP


DZone MVB


Moderator at CodeASP.NET


Quiz Master







free counters
Free counters
Added on January 19,2012


Follow Me @vmsdurano

A bit About Me



Disclaimer
The opinions expressed herein are my own personal opinions and does not represent the opinions of my employers. Nor does it represent the opinion of my dog, because I don’t have one.


Vinz' Blog (ProudMonkey) "Code, Beer and Music ~ my way of being a programmer"

This sample snippet below describes on how we are going to Populate a TextBox and Label control in the page based on the data associated per user using the ADO.NET way..

C#

private void getData(string user)
{
    DataTable dt = new DataTable();
    SqlConnection connection = new SqlConnection("YOUR CONNECTION STRING HERE");
    connection.Open();
    SqlCommand sqlCmd = new SqlCommand("SELECT * from TABLE1 WHERE UserID = @username", connection);
    SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);

    sqlCmd.Parameters.AddWithValue("@username",user);
    sqlDa.Fill(dt);
    if (dt.Rows.Count > 0)
    {
           TextBox1.Text = dt.Rows[0]["ColumnName1"].ToString(); //Where ColumnName is the Field from the DB that you want to display
           TextBox2.Text = dt.Rows[0]["ColumnName2"].ToString();
           Label1.Text = dt.Rows[0]["ColumnName3"].ToString();
           Label2.Text = dt.Rows[0]["ColumnName4"].ToString();
    }
        connection.Close();
}

protected void Page_Load(object sender, EventArgs e)
{

   if (!Page.IsPostBack){
    getData(this.User.Identity.Name);
   }

}

VB.NET

Private Sub getData(ByVal user As String)
    Dim dt As New DataTable()
    Dim connection As New SqlConnection("YOUR CONNECTION STRING HERE")
    connection.Open()
    Dim sqlCmd As New SqlCommand("SELECT * from TABLE1 WHERE UserID = @username", connection)
    Dim sqlDa As New SqlDataAdapter(sqlCmd)
   
    sqlCmd.Parameters.AddWithValue("@username", user)
    sqlDa.Fill(dt)
    If dt.Rows.Count > 0 Then
        TextBox1.Text = dt.Rows(0)("ColumnName1").ToString()  'Where ColumnName is the Field from the DB that you want to display
        TextBox2.Text = dt.Rows(0)("ColumnName2").ToString()
        Label1.Text = dt.Rows(0)("ColumnName3").ToString()
        Label2.Text = dt.Rows(0)("ColumnName4").ToString()
    End If
    connection.Close()
End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    If Not Page.IsPostBack Then
        getData(Me.User.Identity.Name)
    End If
End Sub

Note: Don't forget to declare the following Namespaces below for you to make it work.

For C#:
Using System.Data;
Using System.Data.SqlClient;


For VB.NET
Imports System.Data;
Imports System.Data.SqlClient;

That simple! Hope someone find this post useful!

Technorati Tags: ,
Posted on Friday, September 12, 2008 1:16 AM ADO.NET , ASP.NET | Back to top


Comments on this post: Bind TextBox and Label Control with Data from database

# re: Bind TextBox and Label Control with Data from database
Requesting Gravatar...
This is the clearest, most complete, most helpful page of info around this topic that I have seen in days! Thank you so much!

~Michelle :-)
Left by Michelle on Jan 06, 2011 5:32 AM

# re: Bind TextBox and Label Control with Data from database
Requesting Gravatar...
I am not able to get all fields attached to my controls although I am retrieving all data from the DB using SProc. This is happening moreso on the Dropdowns, some dropdowns get data but some dont.
Left by Just on Feb 23, 2011 4:38 PM

# re: Bind TextBox and Label Control with Data from database
Requesting Gravatar...
Its work for me ... thank u
Left by anto sujesh on Sep 03, 2011 7:26 PM

# re: Bind TextBox and Label Control with Data from database
Requesting Gravatar...
Where can i declare the 'Page'

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not Page.IsPostBack Then
getData(Me.User.Identity.Name)
End If
End Sub
Left by Haniel on Oct 07, 2011 8:58 AM

# re: Bind TextBox and Label Control with Data from database
Requesting Gravatar...
Yes, agreed with Michelle.. The most helpful info on this topic... Thank you very much...
Left by cahmad on Oct 19, 2011 4:00 AM

comments powered by Disqus

Copyright © Vincent Maverick Durano | Powered by: GeeksWithBlogs.net