string sqlInsert = "INSERT INTO Transaction " +
"(VIN, Price, TranDate, TranType) " +
"VALUES('" + VIN + "', '" + price + "', '" + aDate + "', '" + transType + "')";
// check SQL string in Output window for debugging
Console.WriteLine(sqlInsert);
myDataAdapter = new OleDbDataAdapter();
//insert new account into database
try
{
myDataAdapter.InsertCommand = new OleDbCommand(sqlInsert);
myAccessConn = ConnectionClass.getConnection();
if (myAccessConn.State == ConnectionState.Closed)
{
myAccessConn.Open();
}
myDataAdapter.InsertCommand.Connection = myAccessConn;
myDataAdapter.InsertCommand.ExecuteNonQuery();
}
catch (Exception e)
{
Console.WriteLine("Error: Insert in DA failed");
Console.WriteLine(e);
}
coop123 0 Newbie Poster
Recommended Answers
Jump to PostWhat error are you getting
remove ' ' for price unless its of type string
"', " + price + ", '"
Shouldn't make a difference you should still get whatever error ur getting.
Jump to PostTry to understand the use of Parameters.
OleDbCommand cmd=new OleDbCommand(); cmd.CommandText="insert into Transaction (vin,price,trandate,trantype) values (@vin,@price,@trandate,@trantype)"; cmd.Connection=ConnectionClass.getConnection(); cmd.Parameters.AddWithValue("@vin",VIN); cmd.Parameters.AddWithValue("@price",price); cmd.Parameters.AddWithValue("@trandate",aDate); cmd.Parameters.AddWithValue("@trantype",transType); cmd.Connection.Open(); cmd.ExecuteNonQuery(); cmd.Connection.Close();
Jump to PostThis is a better code.
You can even add the datatype per parameter.
All 9 Replies
finito 46 Nearly a Posting Virtuoso
coop123 0 Newbie Poster
coop123 0 Newbie Poster
finito 46 Nearly a Posting Virtuoso
finito 46 Nearly a Posting Virtuoso
kvprajapati 1,826 Posting Genius Team Colleague
powerbox 13 Light Poster
finito 46 Nearly a Posting Virtuoso
coop123 0 Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.