Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello
I am coding a database migrator and I execute the following functions.
The second one if the database table was created in the first function always return null and then crashes on the trim function call. The thing is that if I place a breakpoint on the execute scalar function it works fine and returns the value on the db_version but if I place the breakpoint a little below the version value is null
Any idea?


private static void CreateVersionTable()
{
    if (!DatabaseManager.TableExists("poscuba_info"))
    {
        string sqlTable = @"CREATE TABLE poscuba_info(
                       db_version   CHAR(10));";

        string sqlVersion = @"INSERT INTO poscuba_info (db_version) VALUES ('1.02') ";

        try
        {
            DatabaseManager.executeSQL(sqlTable);

            DatabaseManager.executeSQL(sqlVersion);

            dataBaseVersion = "1.02";
        }
        catch (Exception e)
        {
            MigrationError = e.Message;
        }
    }
}

public static string GetDBVersion()
{
    string version = "";

    try
    {
        version = (string)DatabaseManager.executeScalar("Select db_version from poscuba_info");

        if (version == "")
        {
            MigrationError = "Version not found";
        }
    }
    catch (Exception e)
    {
        MigrationError = e.Message;
    }

    dataBaseVersion = version.Trim();

    return dataBaseVersion;
}


What I have tried:

I have tried debugging and it works when debugged step by step
Posted
Updated 23-Nov-17 20:25pm
Comments
phil.o 24-Nov-17 2:26am    
A better check would be if (string.IsNullOrEmpty(version)).
Could we see the definition of executeScalar method of DatabaseManager class?
Vasily Tserekh 24-Nov-17 11:25am    
static public int executeSQL(string sqlQuery)
{
OleDbConnection connection = new OleDbConnection(connectionString);
connection.Open();
OleDbCommand command = new OleDbCommand(sqlQuery, connection);
return command.ExecuteNonQuery();
}

static public object executeScalar(string sqlQuery)
{
OleDbConnection connection = new OleDbConnection(connectionString);
connection.Open();
OleDbCommand command = new OleDbCommand(sqlQuery, connection);
return command.ExecuteScalar();
}

1 solution

It is possibly a timing issue in release mode, try adding the following:
C#
DatabaseManager.executeSQL(sqlTable);
Thread.Sleep(1000); // add a 1s wait here 
DatabaseManager.executeSQL(sqlVersion);
 
Share this answer
 
Comments
Vasily Tserekh 24-Nov-17 11:29am    
It didn't work :(
Mehdi Gholam 25-Nov-17 0:07am    
What error are you getting?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900