views:

232

answers:

1

I am simply trying to add a single row in the database but I keep getting an exception. I created a local database and added a single table: users. It consists of two columns: "id" and "name". I only made the id primary key (not auto-increment or anything else).

When I run the following code:

    string execPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
    string dbfile = execPath + @"\LocalDatabase.sdf";
    SqlCeConnection conn = new SqlCeConnection("datasource=" + dbfile);
    conn.Open();
    string command = "INSERT INTO users VALUES('1','pek')";
    Debug.WriteLine(command);
    SqlCeCommand comm = conn.CreateCommand();
    comm.CommandText = command;
    comm.ExecuteNonQuery();

I get the following Exception at "comm.ExecuteNonQuery();":

SqlCeException was unhandled

The column cannot be modified. [ Column name = id ]

What's with the "modified" part?

+1  A: 

How is the "users" table defined? If the first column (BTW, you really should be naming the columns in your query explicitly, not leaving it to the parser to define them) is an IDENTITY field, you cannot set the value.

ctacke

related questions