I have a AccessDataSource control which successfully get the data from TableA of field ProductID, ProductName.
Now I need to get Select ProductName where ProductID =1 so I type this into SelectCommand and it can display the record into Gridview, meaning the AccessDataSource is perfect.
However, instead of Gridview, I need to get that ProductName into label1.text.
What should be the easiest way and code for so doing ?
Thanks
Thanks. I will try to credit the ones who helped but most important is we really do sincerely thanks to all who have helped.
Any data source is used to bind data contents directly to the data presentation control, so you don't need to use this if you only wanna fetch a single value. Please use SqlCommand instead:
private static void ReadOrderData(string connectionString)
{
string commandText = "SELECT TOP1 FieldName FROM xxx;";
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand command = new SqlCommand(commandText, connection))
{
connection.Open();
using (SqlDataReader reader = command.ExecuteReader())
{
if(reader.Read())
{
Label1.Text = reader.GetString(fieldindex,0-based);
}
}
}
}
}
hkbeer
Member
709 Points
1462 Posts
How to get field value into label1.text with select query from AccessDatasource
Dec 30, 2012 11:30 AM|LINK
I have a AccessDataSource control which successfully get the data from TableA of field ProductID, ProductName.
Now I need to get Select ProductName where ProductID =1 so I type this into SelectCommand and it can display the record into Gridview, meaning the AccessDataSource is perfect.
However, instead of Gridview, I need to get that ProductName into label1.text.
What should be the easiest way and code for so doing ?
Thanks
www.developerfusion.com/tools/convert/csharp-to-vb/
oned_gk
All-Star
31373 Points
6412 Posts
Re: How to get field value into label1.text with select query from AccessDatasource
Dec 30, 2012 11:48 AM|LINK
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: How to get field value into label1.text with select query from AccessDatasource
Dec 30, 2012 11:59 PM|LINK
Hi,
Any data source is used to bind data contents directly to the data presentation control, so you don't need to use this if you only wanna fetch a single value. Please use SqlCommand instead:
private static void ReadOrderData(string connectionString) { string commandText = "SELECT TOP1 FieldName FROM xxx;"; using (SqlConnection connection = new SqlConnection(connectionString)) { using (SqlCommand command = new SqlCommand(commandText, connection)) { connection.Open(); using (SqlDataReader reader = command.ExecuteReader()) { if(reader.Read()) { Label1.Text = reader.GetString(fieldindex,0-based); } } } } }