locked
Select Count not giving result RRS feed

  • Question

  • User-1283384789 posted

     Hi,

    Can someone please tell me why this code is not showing result.

     

       protected void Page_Load( EventArgs e)
    {
    string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\\Orozje-Reloading-Center.mdb";
    OleDbConnection conn = new OleDbConnection(connectionString);
    conn.Open();
    OleDbCommand cmd = new OleDbCommand();
    cmd.Connection = conn;

    cmd.CommandText = "SELECT COUNT(*) AS TOTAL FROM VIDEO_MOJI WHERE (UserId = ?)";
    int counter = (int)cmd.ExecuteScalar();

    lblTotalCount.Text = "Result:" + counter.ToString();
    conn.Close();


    }
    thanks 
      
    Sunday, May 24, 2009 4:10 AM

Answers

  • User1152736709 posted
    Try adjusting your code as such:
     protected void Page_Load( EventArgs e)
    {
    string connectionString = "your connection string";
    OleDbConnection conn = new OleDbConnection(connectionString);
    conn.Open();
    OleDbCommand cmd = new OleDbCommand();
    cmd.Connection = conn;

    cmd.CommandText = "SELECT COUNT(*) AS TOTAL FROM VIDEO_MOJI WHERE (UserId = @UserId)";
    cmd.Parameters.Add("@UserId", OleDbType.Integer).Value = 239;

    int counter = (int)cmd.ExecuteScalar();

    lblTotalCount.Text = "Result:" + counter.ToString();
    conn.Close();
    }
    Use a parameter (@UserId) to supply a user id (which I assume is an integer) to the query. Ofcourse you need to replace 239 with a value of your own.
      
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, May 24, 2009 6:23 AM