Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Database to Label

Options
  • 21-02-2008 2:49am
    #1
    Registered Users Posts: 325 ✭✭


    Hi

    Using MySQL, VS2005 and C#

    I have an sql statement here that performs a SELECT statement on a column in the DB and gets the average of it.

    {
    myConn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver};Server=localhost;Database=project;User=root;Password=*****;");

    myConn.Open();

    string sql = "SELECT AVG(Average) FROM average";

    OdbcCommand cmd = new OdbcCommand(sql, myConn);
    cmd.ExecuteNonQuery();
    }

    Could someone tell me how I could get the answer to display on a label?

    Cheers


Comments

  • Registered Users Posts: 2,931 ✭✭✭Ginger


    Before I walk you through this what would the words Execute Non Query normally mean????

    Non Queries are updates, inserts or deletes rather than SELECTS

    So in your case, you are you are executing a returnable query.

    So if you are only returning 1 row with 1 column use ExecuteScalar or if not use ExecuteReader

    Assign the return value(s) of your command to an object (string,int,dataset) whatever suits

    eg
    int myRetValue = cmd.ExecuteScalar();
    

    Then use your label...

    eg
    lblText.Text = myRetValue.ToString();
    

    PLease take a look at http://msdn2.microsoft.com/en-us/library/system.data.odbc.odbccommand.aspx


  • Registered Users Posts: 325 ✭✭doyler442


    Cheers for the help mate - after a while messing around with it last night I managed to get it working - same code as yourself.


  • Closed Accounts Posts: 52 ✭✭zardette


    ExecuteScalar returns an "object" type maybe converting it to an int might be required?

    Z


Advertisement