I am new to C#. This example below works as long as I write the value directly into the OleDBcommand.CommandText.
The format of the column C_1 in the DB is integer, just like the variable “b” is.
Running the example code, I get the error “types incompatible”. If I change the column format to “text”, the program writes a “b” into the DB. As my way of code is, the CommandText would not accept any variable with its value.
Tried all kinds and combinations of brackets and quotation marks. Can it be?
Thanks for a hint!
private void CmdNeuerDatenSatz_Click(object sender, RoutedEventArgs e)
{
int c = 8;
int b = 110;
OleDbConnection con = new OleDbConnection();
OleDbCommand cmd = new OleDbCommand();
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source = C:\\Users\\Linner OHG\\Desktop\\TestDB.mdb";
cmd.Connection = con;
cmd.CommandText = "INSERT INTO DB_Test (Col_1) VALUES (b)";
try
{
con.Open();
c = cmd.ExecuteNonQuery();
MessageBox.Show("Row has been inserted!");
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}