Click here to Skip to main content
15,849,996 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
An error occurred when I update my SQL server compact database (.sdf)
I'm using C# windows form application
This is the error

ExecuteNonQuery: Connection property has not been initialized.

This is my update code
What is the wrong I stuck 3h in here

Thank in advance !

int index = cb1.SelectedIndex; //combo box
int uid=1;

            SqlCeConnection con = new SqlCeConnection("Data Source="C:\application\application.sdf;Persist Security Info=False;");
            SqlCeCommand cm = new SqlCeCommand("UPDATE tbl_user SET u_name=@u_name , u_scl=@u_scl , u_ext=@u_ext WHERE u_id=@u_id");
            cm.Parameters.AddWithValue("@u_id", uid);
            cm.Parameters.AddWithValue("@u_name", txtname.ToString());
            cm.Parameters.AddWithValue("@u_scl", txtscl.ToString());
            cm.Parameters.AddWithValue("@u_ext",index );
                int eff = cm.ExecuteNonQuery();
                if (eff == 1)
                    MessageBox.Show("User Added", "Done");
                    MessageBox.Show("not inserted ", "Error");
            catch (SqlCeException ex)
Updated 19-Jul-23 0:07am

1 solution

Easy. You made a SqlCeConnection object and a SqlCeCommand object, but you never told the command object to use the connection object.

I think you're looking for this[^].
Share this answer
Manoj Chamikara 4-Nov-13 12:47pm    
Thank you dude :)
Sergey Alexandrovich Kryukov 5-Nov-13 1:33am    
Easy, well-spotted, a 5.

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