Displaying a table field in a text box
I have a table and I want to select a field in it and then display it in a textbox
sort of:
SELECT userName
FROM userTable
WHERE (userLogged = 'ON')
how can i display the selected username in a textbox?
BTW userLogged indicates the user has logged in or not
if user is logged in then userLogged will be changed to "ON"
If the user is not registered, it will be "OFF"
I know it isn't, but I am still practicing.
I am using Visual Web Developer 2008 Express
--------- update ----------
I am using table adapter procedures for queries
a source to share
If you're just pulling out one field, the best way is to run the SqlCommand in scalar execution mode; which will only return 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 that Windows authentication is used to log into SQL Server; you can replace it with "User Id = Username; Password = Password;"
a source to share
To set the text property to a textbox, you simply call ...
TextBox1.Text = "Value";
From your code, if your aspx page has a textbox control ...
<asp:TextBox ID="TextBox1" runat="server"/>
However, in your example code there are very many and many different ways to access the field value from the database
a source to share
Well, this is a broad question as we don't know how you are querying the database or anything. Basically, you just take the result of your query, assuming it is in a DataTable 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 great resources at:
This is a great place for newbies and has many ADO.NET tutorials.
a source to share