Click here to Skip to main content
15,895,812 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have developed a code for insert data to sql compact server?


C#
string connetionString = null;

            connetionString = "Data Source=stockDb.sdf";

            using (SqlCeConnection con = new SqlCeConnection(connetionString))
            {
                //
                // Open the SqlConnection.
                //
                con.Open();
                //
                // The following code uses an SqlCommand based on the SqlConnection.
                //

                string sqlquery = ("insert into Enquiry(userName,cusName,cusContact,subDate,reqDate,moreDetails,tktType,noPax,firstStatus,secStatus,thirdStatus)Values(@uName,@cName,@cContact,@sDate,@rDate,@mDetails,@tType,@nPax,@fStatus,@sStatus,@tStatus)");

                SqlCeCommand command = new SqlCeCommand(sqlquery, con);


                    string submitDate = sDate.Value.ToString("yyyy-MM-dd");
                    string requireDate = mDate.Value.ToString("yyyy-MM-dd");
                    int value = Convert.ToInt32(Cus_PaxNo.Text);

                    command.Parameters.AddWithValue("@uName", Loginlbl.Text);
                    command.Parameters.AddWithValue("@cName", Cus_Name.Text);
                    command.Parameters.AddWithValue("@cContact", Cus_Contact.Text);
                    command.Parameters.AddWithValue("@sDate", submitDate);
                    command.Parameters.AddWithValue("@rDate", requireDate);
                    command.Parameters.AddWithValue("@mDetails", Cus_Details.Text);
                    command.Parameters.AddWithValue("@tType", Cus_TicketType.Text);
                    command.Parameters.AddWithValue("@nPax", value);
                    command.Parameters.AddWithValue("@fStatus", Cus_FirstStatus.Text);
                    command.Parameters.AddWithValue("@sStatus", Cus_SecondStatus.Text);
                    command.Parameters.AddWithValue("@tStatus", Cus_ThirdStatus.Text);
                    command.ExecuteNonQuery();

                        MessageBox.Show("Password Accepted");
           }


this code gave me an error saying?

An unhandled exception of type 'System.Data.SqlServerCe.SqlCeException'occured in System.Data.SqlserverCe.Dll
Posted
Comments
CHill60 10-Jun-15 7:18am    
Which line produced the error and what was the rest of the error message?

Things to try:
1. "thirdStatus)Values" - try putting a space in "thirdStatus) Values"
2. In the debugger check the contents of each of the text boxes, value and the dates - are any them empty?
3. Are any of the values too big for the database columns?
Maciej Los 10-Jun-15 8:28am    
Seems you asked this before...
Maciej Los 10-Jun-15 8:33am    
Date is date. Do not convert it to string!
Hemas Ilearn 10-Jun-15 23:32pm    
command.ExecuteNonQuery();
this code gives an error....!!

but there is no data type for date in compact sql server?

1 solution

Hhhmmm... code seems to look OK with 2 small notes:

1) connection string is not full, see here: http://www.connectionstrings.com/sql-server-compact/[^]
Proper connection string is: Data Source=MyData.sdf;Persist Security Info=False;

2) Replace this:
C#
string submitDate = sDate.Value.ToString("yyyy-MM-dd");
string requireDate = mDate.Value.ToString("yyyy-MM-dd");

with:
C#
DateTime submitDate = sDate.Value;
DateTime requireDate = mDate.Value;

because datetime data type must be inserted as datetime not string!
See: Data Types Supported in SQL Server CE[^]

Try!
 
Share this answer
 
Comments
Hemas Ilearn 11-Jun-15 3:12am    
still same error...
in
command.ExecuteNonQuery();

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