SUM statement RRS feed

  • Question

  • User-986822757 posted

    Need help with the code below, it runs well untill the line with *** infront, I want to SUM all the time for each user in a new column and then write the result in the lblSUMTid.

    userID = User.Identity.Name;

    string c = this.AccessDataSource1.ConnectionString;

    OleDbConnection con = new OleDbConnection(c);

    string sSql = "SELECT SUM(Time) AS SUMTime FROM Loggposter WHERE (UserID = ' " + userID + " ')";

    OleDbDataAdapter cnLoggAdapter = new OleDbDataAdapter(sSql, con);

    DataTable dtLogg = new DataTable();


    DataRow drLogg = dtLogg.Rows[0];

    string nTime;

    ***nTime = (string)drLogg["SUMTime"];

    this.lblSUMTid.Text = nTid.ToString();

    Friday, April 11, 2008 4:33 PM


  • User-821857111 posted

    You should always provide the error message you are getting.  Otherwise people have to guess.  And that may delay responses to your question.  As it happens, there are several potential sources of error in your code:


    Try the ToString() method instead of casting: 

    string nTime;
    nTime =


    TIME is a reserved word in Jet, and should not be used as a field name.  If you can't change it, change the SQL so that the reserved word is surrounded by [ ] brackets to:

    string sSql = "SELECT SUM([Time]) AS SUMTime FROM Loggposter WHERE (UserID = ' " + userID + " ')";



    UserIDs are often numeric.  You have delimited the value in your concatenated SQL as a string.  The easiest way to avoid this kind of problem (and prevent SQL Injection) is to use parameters: http://www.mikesdotnetting.com?Article.aspx?ArticleID=26.


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, April 12, 2008 5:42 AM