locked
How to get field value into label1.text with select query from AccessDatasource RRS feed

  • Question

  • User-775831949 posted

    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

    Sunday, December 30, 2012 7:30 AM

Answers

  • User3866881 posted

    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);
                           }
                      }
                }
          }
    }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, December 30, 2012 7:59 PM

All replies

  • User-1716253493 posted
    DataTable dt = ((DataView)AccessDataSource1.Select(DataSourceSelectArguments.Empty)).Table;
    Label1.Text = dt.Rows[0][0].ToString();

    Sunday, December 30, 2012 7:48 AM
  • User3866881 posted

    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);
                           }
                      }
                }
          }
    }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, December 30, 2012 7:59 PM