locked
Assign returned value to a label RRS feed

  • Question

  • User-1887133141 posted

    Hi pals,

    I have the following code:

    string connect = "SomeConnectionString";
            SqlConnection con = new SqlConnection(connect);
            var cmd = new SqlCommand("SELECT COUNT(DISTINCT parent) AS Expr1 FROM Menus", con);
            cmd.Connection.Open();
            
            SqlDataReader reader = cmd.ExecuteReader();
    
             /*
                 Assign the returned value to Lable1.text
             */
    
            reader.Close();
            cmd.Connection.Close();
            cmd.Dispose();

    The result drawn from the command is an integer value stored in a column named "Expr1".

    My question is: How can I assing the returned value to Label1.text?

    Thursday, April 24, 2014 4:02 PM

Answers

  • User-1360095595 posted

    Google/bing: ExecuteScalar() and use it. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 24, 2014 4:08 PM
  • User724169276 posted

    Hello Benglish,

    Using Reader for getting scalar values is an overkill.Use ExecuteScalar.

            string connect = "SomeConnectionString";
            SqlConnection con = new SqlConnection(connect);
            var cmd = new SqlCommand("SELECT COUNT(DISTINCT parent) AS Expr1 FROM Menus", con);
            cmd.Connection.Open();
            //Assign the returned value to Lable1.text
            Label1.Text= (string) cmd.ExecuteScalar();
            cmd.Connection.Close();
            cmd.Dispose();

    Hope This Helps.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 25, 2014 12:34 AM

All replies

  • User-1360095595 posted

    Google/bing: ExecuteScalar() and use it. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 24, 2014 4:08 PM
  • User-1716253493 posted
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                Label1.Text = reader.GetString(0);
            }

    Thursday, April 24, 2014 9:12 PM
  • User2103319870 posted

    You can also use the below code to read the values from SQLDataReader 

                   //Get the value from SQLDataReader
                    YourLabelID.Text = reader.GetOrdinal("Expr1");

    Your Modified Code:

     string connect = "SomeConnectionString";
                SqlConnection con = new SqlConnection(connect);
                var cmd = new SqlCommand("SELECT COUNT(DISTINCT parent) AS Expr1 FROM Menus", con);
                cmd.Connection.Open();
    
                SqlDataReader reader = cmd.ExecuteReader();
    
                if (reader.Read())
                {
                    //Get the value from SQLDataReader
                    YourLabelID.Text = reader.GetOrdinal("Expr1");
                }
                /*
                    Assign the returned value to Lable1.text
                */
    
                reader.Close();
                cmd.Connection.Close();
                cmd.Dispose();

    Change the labelId as per your design

    Thursday, April 24, 2014 11:10 PM
  • User724169276 posted

    Hello Benglish,

    Using Reader for getting scalar values is an overkill.Use ExecuteScalar.

            string connect = "SomeConnectionString";
            SqlConnection con = new SqlConnection(connect);
            var cmd = new SqlCommand("SELECT COUNT(DISTINCT parent) AS Expr1 FROM Menus", con);
            cmd.Connection.Open();
            //Assign the returned value to Lable1.text
            Label1.Text= (string) cmd.ExecuteScalar();
            cmd.Connection.Close();
            cmd.Dispose();

    Hope This Helps.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 25, 2014 12:34 AM