Hi I am working with C# compact framwork. At the moment I am trying to add rows to a sqlce database accept i have an bug in my code which i am not able to fix. Would anyone be able to advise me where i am going wrong. The way i believe that this code is set up is that when the user clicks on the add button on the screen it takes the details from the text boxes and place the values in the database. Am I right in this and why is it not finishing the method. I am notgetting an error message the program is just freezing.

Thanks for any help possible

private void miAdd_Click(object sender, System.EventArgs e)
{
    if(txtName.Text=="")
    {
         MessageBox.Show("Please enter a name");
    }

    if(txtName.Text != "" & txtPatID.Text !="")
   {				
        SqlCeConnection connDB = new SqlCeConnection ("Data Source ="+  @"\My Documents\\system.sdf");
        connDB.Open();
        SqlCeCommand cmdDB = new SqlCeCommand();
												  
        SqlCeCommand sqlInsertRow = connDB.CreateCommand();
        sqlInsertRow.CommandText = "INSERT INTO Patient(patID, f_name) VALUES('?','?')";
        sqlInsertRow.Parameters.Add(new SqlCeParameter("patID", SqlDbType.Int)).Value = txtPatID.Text;
        sqlInsertRow.Parameters.Add(new SqlCeParameter("f_name", SqlDbType.NText)).Value = txtName.Text;
         sqlInsertRow.ExecuteNonQuery();
    }
}

remove the insert to the id

it is probably your primary key with a unique idenifier, which auto increments.

which means that you can have 2 of the same ids, if it doesnt auto increment make it so it does or make sure the id doesnt already exist.

if you want more then one of the same ids remove the unique identifier from it


or
i jsut noticed your connection string may not be right. i havent worked with ceslq, but in normal sql you point it to the server name, you have it pointed to a file. unless the ce version works like access or something,

oh one last thing, make sure you close that connection or youll have a bunch of open connections creating a leak,
put the executenonquery in a try and close it in a finaly

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.