Search code examples
c#.netsqldatareader

Error while checking if ID exists in database file


I am trying to check if certain ID already exists in database. When it doesn't, I want user to change the id to something else.

All this is done in TextChanged function of textobx.

The problem is that I am getting an error, and since query looks good I am not sure why i see this: The SELECT statement includes a reserved word or an argument name that is misspelled or missing, or the punctuation is incorrect.

Method which does the check:

private bool DoesIDExist(int dataID, string filePath)
{
    HashPhrase hash = new HashPhrase();
    DataTable temp = new DataTable();

    string hashShortPass = hash.ShortHash(pass);
    bool result = false;

    // Creating a connection string. Using placeholders make code
    // easier to understand.
    string connectionString =
        @"Provider=Microsoft.ACE.OLEDB.12.0; Data Source={0};
          Persist Security Info=False; Jet OLEDB:Database Password={1};";

    string sql = string.Format
        ("SELECT FROM PersonalData WHERE [DataID] = {0}", dataID);

    using (OleDbConnection connection = new OleDbConnection())
    {
        // Creating command object.
        // Using a string formatting let me to insert data into
        // place holders I have used earlier.
        connection.ConnectionString =
            string.Format(connectionString, filePath, hashShortPass);

        using (OleDbCommand command = new OleDbCommand(sql, connection))
        {
            // Creating command object.
            // Using a string formatting let me to insert data into
            // place holders I have used earlier.
            connection.ConnectionString =
                string.Format(connectionString, filePath, hashShortPass);

            try
            {
                // Open database connection.
                connection.Open();

                using (OleDbDataReader read = command.ExecuteReader())
                {
                    // Checking if there is any data in the file.
                    if (read.HasRows)
                    {
                        // Reading information from the file.
                        while (read.Read())
                        {
                            if (read.GetInt32(0) == dataID)
                                return true;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message);
            }
        }
    }

    return result;
}

Solution

  • I think your select is missing some columns you want to extract out??

    string sql = string.Format
        ("SELECT FROM PersonalData WHERE [DataID] = {0}", dataID);
    

    Shouldn't it be something like:

    string sql = string.Format
        ("SELECT * FROM PersonalData WHERE [DataID] = {0}", dataID);