Click here to Skip to main content
15,905,566 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I trying to insert data to my SQL server compact database(.sdf) in C#
This is my code i allways popup not inserted error message
waht is the wronge !
I new to C#
Thank in advance!




C#
int index= cb1.SelectedIndex; //cb1=combo box
            SqlCeConnection con = new SqlCeConnection("Data Source="C:\application\application.sdf"");
            con.Open();
            SqlCeCommand cm = new SqlCeCommand("INSERT INTO tbl_user(u_name, u_scl, u_cmb, u_id) VALUES(@u_name, @u_scl, @u_cmb, @u_id)", con);
            cm.Parameters.AddWithValue("@u_name", txtname.Text);
            cm.Parameters.AddWithValue("@u_scl", txtscl.T.Text);
            cm.Parameters.AddWithValue("@u_cmb",index);
            cm.Parameters.AddWithValue("@u_id", 1);
            try
            {
                int eff = cm.ExecuteNonQuery();
                
                if (eff == 0)
                {
                    MessageBox.Show("Student Added ", "Done");
                }
                else
                {
                    MessageBox.Show("not inserted ", "Error");
                }
            }
            catch (SqlCeException ex)
            {
                MessageBox.Show(ex.ToString());
                //MessageBox.Show("User not Inserted", "Error");
            }
            con.Close();
Posted
Comments
Bernhard Hiller 4-Nov-13 2:36am    
And the exception is ...?
Rishikesh_Singh 4-Nov-13 2:50am    
Check the exception and inner exception. Check the value of your each parameter and also double check your column datatype of the table you are trying to insert. If you still can't figure it out,update your question with all the information i mentioned.

1 solution

cross check with database that row is actually inserted or not because ExecuteNonQuery()
method will return number of rows affected so it should be 1 if one row is inserted properly and you are displaying message 'not inserted' if eff is other than 0.
set breakpoint at
int eff = cm.ExecuteNonQuery();


find the value of eff and modify code accordingly.
Also verify your connection string.
Data Source=C:\application\application.sdf;Persist Security Info=False;

this is standard connection string for sql server compact.

--
Thanks
 
Share this answer
 
v2
Comments
Manoj Chamikara 5-Nov-13 2:19am    
Row insert properly ExecuteNonQuery () returns the value 1 . When I check the database right click table and show table data there is no new row I insert but I insert the same data again error occurred "Duplicate value cannot be inserted into a queindex .............. "
Why is that?
i cannot find why is that
Thank you so much your reply sorry about my late
member 8888995 5-Nov-13 4:34am    
Hey Manoj ,
No need to say sorry for being late. Try by refreshing table, disconnect and reconnect to database or restart sql server.

--
Thanks

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