Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Data Bind SQL to Label Control

Status
Not open for further replies.

slipfrase

Programmer
Feb 23, 2004
16
GB
This is driving me crazy, apologies if this is simple, but I cant seem to find the answer to this.

I'm learning asp.net 2.0 with visual web developer, and am developing a simple banking website for practice. Its uses windows authentication with AD, which is working great.

I have 2 basic tables in an sql 2005 express db:

Accounts:
Account No (Int)
UserName (nvarchar(50))
Balance (money)

Tranactions
Account No (Int) - Foriegn Key to above.
Date (smalldate)
Amount (money)

Ok now on a form, I have a label control which picks up the windows username from HttpContext.Current.User.Identity.Name
and I have a data source which queries the Accounts table passing the value of this label in.

Now I can set a grid view, form view, etc to pull the details of that particular record, to look up the account.

What I am trying to do, is reference a label control to get the account no (as I will have another data source which will be based on the value of this control)

What I would like to do is populate a label control with account number, or be able to reference the data items from the formview control in code in the aspx.vb code file.

Hope this makes sense and am sure this is very easy...

Many thanks....
Fraser



 
If you are using the datasource controls I don't think you can do it. You would need to fill a dataset or datatable and then get the value from there, and set the text property of the label.
 
ok I've kind of figured how to do this, but not the way I was hoping to. What I've done is to create a direct connection to the database through code to pull the value onto the label. Code looks like below:

Dim connectionString As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Bank.MDF;Integrated Security=True;User Instance=True"
Dim QueryString As String

QueryString = "SELECT AccountNo "
QueryString = QueryString & "FROM Accounts "
QueryString = QueryString & "WHERE UserName like " & "'" & UserName & "'"

Using connection As New SqlConnection(connectionString)
Dim command As New SqlCommand(QueryString, connection)
connection.Open()
Dim reader As SqlDataReader = command.ExecuteReader()
Try
While reader.Read()
label1.text = reader.Item("AccountNo")
End While
Finally
' Always call Close when done reading.
reader.Close()
End Try

Would still be interested to hear if there is a cleaner way to do this?

Thanks,
Fraser

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top