I am encountering syntax error in INSERT INTO statement when I run the following code.
String
myConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + @"Data source= C:\\123\\Login.mdb"; String myAddQuery = @"INSERT INTO UserLogin " + @"(Name, EmpID, Dept, UserID, Password) " + "VALUES (\'" + Name + "\',\'" +EmpID +
"\',\'" +Dept +
"\',\'" +UserID +
"\',\'" +Password +
"\')"; MessageBox.Show(myAddQuery); try{
OleDbConnection conn = new OleDbConnection(myConnectionString); OleDbCommand myCommand = new OleDbCommand(myAddQuery, conn);conn.Open();
rv = myCommand.ExecuteNonQuery();
}
catch (OleDbException e){
MessageBox.Show("OleDB Error: " + e.Message);}
finally{
// if (conn}
}
}
Can anyone please help? I am using Access Database.
Thanks
Andrzej WegierskiPosted Jan 26, 2006, 2:46 AM
String myAddQuery = "INSERT INTO UserLogin " +
"(Name, EmpID, Dept, UserID, Password) " +
"VALUES ('" + Name + "','" +
EmpID + "','" +
Dept + "','" +
UserID + "','" +
Password + "')";
In such simple code fields can't contain ' signs
Moses SolimanPosted Jan 25, 2006, 2:28 AM
try this:
String myConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data source= C:\123\Login.mdb";
String myAddQuery = "INSERT INTO UserLogin (Name, EmpID, Dept, UserID, Password) VALUES('{0}', '{1}', '{2}', '{3}','{4}')"
myAddQuery = string.Format(myAddQuery,Name, EmpID, Dept, UserID, Password)
......
-----------
Please note that if your EmpID is integer you need to remove the single couts from '{1}', also the same for Dept and UserID
Hope this will help