Inserting into a Access DB

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Edward Mallett
    New Member
    • Dec 2010
    • 1

    Inserting into a Access DB

    Hello All,

    Sure this is something simple but it is driving me mad! I have the code below which I want to use to insert Data from my C# programme into access database.

    However everytime that it tries to write to the DB it throws a error.

    Code:
     conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;" +
                    @"Data source= C:\db1.mdb; Persist Security Info=False";            
         
        
                    OleDbCommand cmd = new OleDbCommand();
    
                  try
                    {
                    conn.Open();
                    cmd = conn.CreateCommand();
                    cmd.CommandText = "INSERT INTO logData([IP Address],[Username],[Referrer],[URL])VALUES(" + savedData + ",'" + savedData1 + ",'" + savedData2 + ",'" + savedData3 + "');";
    
                    cmd.ExecuteNonQuery();
                    }
                    catch (Exception ex)
                    {
                     MessageBox.Show("Failed to connect to data source");
                    }
                  finally
                    {
                conn.Close();
                    }
    savedData through to savedData3 are string variables that have been populated with either numbers a "-" or text. Can anyone help as to why this wont save to my DB?

    Thanks
  • xMetalDetectorx
    New Member
    • Oct 2008
    • 44

    #2
    In your catch block, you should output the error message (ex.Message) and the stacktrace.


    Please let us know what error your code is throwing =)

    Comment

    Working...