Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
private void button1_Click(object sender, EventArgs e)
        {
            //int iNum = Convert.ToInt32(numericUpDown1.Value);
            string sName = textBox1.Text;
            string connetionString = null;
            OleDbConnection conn;
            OleDbDataAdapter ole = new OleDbDataAdapter();
            string sql = null;
            connetionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Users\\Thabiso\\Documents\\Visual Studio 2013\\Projects\\mdb\\1888.mdb";
            
            conn = new OleDbConnection(connetionString);
            sql = "INSERT INTO Cars(Desc) VALUES(" + "'" + sName + "') ";
            //OleDbCommand command = new OleDbCommand("INSERT INTO FND(userName, userNumber) VALUES('" + txtName.Text + "','" + numericUpDown1.Value + "') ");
            
            try
            {
                conn.Open();
                ole.InsertCommand  = new OleDbCommand(sql, conn);
                ole.InsertCommand.CommandType.ToString();
                ole.InsertCommand.ExecuteNonQuery();
                conn.Close();
                MessageBox.Show("done");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
Posted

1 solution

C#
sql = "INSERT INTO Cars ([Desc]) VALUES('" + sName + "') ";

The column name "Desc" is a bad selection: DESC is a keyword in most (or even all!) sql dialects ("order by column1 desc"). With MS SQL Server or MS Access, you can use it when you put it in square brackets [].
Also note the the name must not contain a ' character with this simple version of creating the query. Better change to parameterized queries.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900