views:

324

answers:

3

I have a table and I want to select a field in it and then display it in a text box

something like:

SELECT userName
FROM userTable
WHERE (userLogged = 'ON')

how can I display the selected username in a textbox?

BTW the userLogged indicates wether the user is logged in or not

if the user is logged in then the userLogged will be changed to "ON"

if the user is not logged in it will be "OFF"

I know it's not that practical but I'm still practicing.

I'm using Visual Web Developer 2008 Express

--------- update ----------

I use table adapter procedures for querying

A: 

To set the text property on a textbox you simply call...

TextBox1.Text = "Value";

From your codebehind, If in your aspx page you have a textbox control...

<asp:TextBox ID="TextBox1" runat="server"/>

However there is much missing from your code example and many different ways of accessing a field value from a database

Nick Allen - Tungle139
yes I understand how to change the text property of a textboxwhat I meant by my question is: can I do it like thistextbox.text = selectUsername()selectUsername() is a table adapter procedure that contains the code that I typed in my question.I tried this way but it didn't work.
KmOj86
sorry, the code is: "textbox.text = selectUsername()"
KmOj86
A: 

If you're just pulling a single field then the best way is to run your SqlCommand in scalar execution mode; which will return just one field/value.

Imports System.Data.SqlClient
....
Using sqlConn as new SqlConnection("Data Source=YourServer";Trusted_Connection=True;Database=DBName", _
      sqlComm as new Sqlcommand("SELECT userName FROM userTable WHERE userLogged = 'ON'", sqlConn)
    sqlConn.Open();
    dim result as string = sqlComm.ExecuteScalar().ToString
    TextBox1.Text = result
End Using

Note: "Trusted_Connection" inside the SQL connection string indicates to use windows authentication to login to the SQL Server; you can replace it with "User Id=Username; Password=Password;"

STW
A: 

Well, this is a broad question as we don't know how you are querying the database or anything like that. Basically, you just take the result of your query, assuming it is in a DataTable object called dt and do something like this:

myTextBox.Text = dt.Rows[0]["userName"].ToString();

But, I suspect you need more than that. I recommend using the wonderful resources over at:

Learn Visual Studio.NET

It it a great place for beginners and has plenty of tutorials on ADO.NET.

BobbyShaftoe
I use table adapter procedures for querying
KmOj86