Deejdd
Deejdd

Reputation: 129

SQL query doesn't get inserted

I've been trying to get my query to work for some time it runs but doesn't insert anything nor does it return any errors.

The database connection is open and is successfuly connection.

The Table is called errorlog and holds the following data

- id (int autoincremental, Primary key, Unique)
- exception (varchar)
- time (DateTime)

exception = String(error message)
time = DateTime.Now

Here's the code:

 public void insertError(string error, DateTime time)
    {
        SqlCeParameter[] sqlParams = new SqlCeParameter[]  
        {  
           new SqlCeParameter("@exception", error), 
           new SqlCeParameter("@time", time)
        };

        try
        {
            cmd = new SqlCeCommand();
            cmd.Connection = connection;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "INSERT INTO errorlog (exception, time) VALUES(@exception, @time)";
            cmd.Parameters.AddRange(sqlParams);
            cmd.ExecuteNonQuery();
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
    }

Any help would be appreciated, Thanks in advance.

EDIT Removed quotes around @exception

Heres the connection:

protected DataController()
    {
        try
        {
            string appPath = System.IO.Path.GetDirectoryName(Assembly.GetAssembly(typeof(DataController)).CodeBase).Replace(@"file:\", "") + @"\";
            string strCon = @"Data Source = " + appPath + @"Data\EasyShop.sdf";
            connection = new SqlCeConnection(strCon);
        }
        catch (Exception e)
        {

        }

        connection.Open();
    }

Finally the way it gets called:

public bool log(string msg, bool timestamp = true)
    {
        DataController dc = DataController.Instance();
        dc.insertError(msg, DateTime.Today);

        return true;
    }

Upvotes: 0

Views: 131

Answers (1)

Mihail Golubev
Mihail Golubev

Reputation: 126

  1. Debug your application and see if connection points exactly to the database you want. Also check if you look for the inserted records in the same database.
  2. If your connection belongs to the transaction, check if it's committed. You will not see those records inserted until transaction is committed.
  3. It seems to me, that you INSERT is wrong. Remove quotes around @exception
  4. Open SQL Server Profiler, connect to your database and check if your INSERT appears in there.

Upvotes: 1

Related Questions