Click here to Skip to main content
15,896,453 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
public void Update()
        {
            OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\Employees.mdb");
            conn.Open();
            OleDbCommand cmd = new OleDbCommand("UPDATE [employee] SET ([Name],[Jobtitle],[Company])Values ('" + Name + "','" + Jobtitle + "','" + Company + "') where [EmpID] = '" + EmpID + "'", conn);
            //OleDbCommand cmd = new OleDbCommand(" update Employee set Name = '" + Name + "',Jobtitle = '" + Jobtitle + "',Company = '" + Company + "'  where EmpID = '" + EmpID + "'", conn);
            cmd.ExecuteNonQuery();
            conn.Close();

        }
Posted
Updated 20-Mar-15 1:11am
v2

First off, don't do it like that. Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

The chances are that doing that will fix your problem, but you should also not use "Name" as the name of a column or table - it's an ACCESS keyword, and that may also cause problems. "FullName" or similar is a lot more descriptive as well.
 
Share this answer
 
As Griff said, you need to fix the SQL Injection[^] vulnerability in your code.

You also need to fix the syntax of your command - what you currently have does not match the syntax of the UPDATE statement[^].

You should also wrap the connection and command objects in using blocks, to ensure that they get cleaned up properly in every case.

C#
public void Update()
{
    using (OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\Employees.mdb"))
    using (OleDbCommand cmd = new OleDbCommand("UPDATE [employee] SET [Name] = ?, [Jobtitle] = ?, [Company] = ? WHERE [EmpID] = ?", conn))
    {
        // The OleDbCommand doesn't use named parameters;
        // only the order matters here:

        cmd.Parameters.AddWithValue("p0", Name);
        cmd.Parameters.AddWithValue("p1", Jobtitle);
        cmd.Parameters.AddWithValue("p2", Company);
        cmd.Parameters.AddWithValue("p3", EmpID);

        conn.Open();
        cmd.ExecuteNonQuery();
    }
}
 
Share this answer
 
I want to tell you that i am new programmer...
Thanks a lot to everybody who spent from his/her time to give me a solution, it is working now i hope to finish my first database program.
 
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