getting output from sql command in c#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • olaleyeoladejo
    New Member
    • Nov 2008
    • 3

    #1

    getting output from sql command in c#

    i'm working on a project and the only problem is that i want to sum a prticular column and put the out put on a label using sqlreader but instead of the value i get 'System.Data.Sq lClient.SqlData Reader' on the label

    THE PROGRAM

    Code:
    SqlDataReader da = null;
                    SqlCommand A = new SqlCommand("Select sum(amount) from transactions where type_of_transaction like '%" + Search + "%'", conn2);
                    A.ExecuteNonQuery();
                    
                    da = A.ExecuteReader();
                    da.Read();
                    sum.Text = da.ToString();
                    
                    conn2.Close();
    i will really appreciate ur help
    Last edited by kenobewan; Nov 4 '08, 10:40 AM. Reason: Use code tags
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Do not use ExecuteNonQuery when you are executing a query. See the last part of this article on how to use ExecuteScalar to get your results.
    Also you are setting the reader itself to the the label's text rather than the reader's results.

    Comment

    • olaleyeoladejo
      New Member
      • Nov 2008
      • 3

      #3
      so how do i set the reader's result to the label

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Something like
        Code:
        cmd.CommandText = "SELECT COUNT(*) FROM tableName";
         Int32 count = (Int32) cmd.ExecuteScalar();
        You then set the value count to be your label's text.

        Comment

        • olaleyeoladejo
          New Member
          • Nov 2008
          • 3

          #5
          thanks it's working now

          Comment

          Working...