Click here to Skip to main content
15,888,087 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 !

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

            SqlCeConnection con = new SqlCeConnection("Data Source="C:\application\application.sdf;Persist Security Info=False;");
            con.Open();
            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 );
           
            try
            {
                int eff = cm.ExecuteNonQuery();
                
                if (eff == 1)
                {
                    con.Close();
                    MessageBox.Show("User Added", "Done");
                }
                else
                {
                    con.Close();
                    MessageBox.Show("not inserted ", "Error");
                }
            }
            catch (SqlCeException ex)
            {
                MessageBox.Show(ex.ToString());
            }
Posted
Updated 18-Jul-23 23:07pm

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
 
Comments
Manoj Chamikara 4-Nov-13 12:47pm    
Thank you dude :)
Sergey Alexandrovich Kryukov 5-Nov-13 1:33am    
Easy, well-spotted, a 5.
—SA

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