944,164 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 4926
  • C# RSS
Mar 20th, 2006
0

Problem adding rows to database

Expand Post »
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

C# Syntax (Toggle Plain Text)
  1. private void miAdd_Click(object sender, System.EventArgs e)
  2. {
  3. if(txtName.Text=="")
  4. {
  5. MessageBox.Show("Please enter a name");
  6. }
  7.  
  8. if(txtName.Text != "" & txtPatID.Text !="")
  9. {
  10. SqlCeConnection connDB = new SqlCeConnection ("Data Source ="+ @"\My Documents\\system.sdf");
  11. connDB.Open();
  12. SqlCeCommand cmdDB = new SqlCeCommand();
  13.  
  14. SqlCeCommand sqlInsertRow = connDB.CreateCommand();
  15. sqlInsertRow.CommandText = "INSERT INTO Patient(patID, f_name) VALUES('?','?')";
  16. sqlInsertRow.Parameters.Add(new SqlCeParameter("patID", SqlDbType.Int)).Value = txtPatID.Text;
  17. sqlInsertRow.Parameters.Add(new SqlCeParameter("f_name", SqlDbType.NText)).Value = txtName.Text;
  18. sqlInsertRow.ExecuteNonQuery();
  19. }
  20. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
student_help is offline Offline
8 posts
since Feb 2006
Mar 20th, 2006
0

Re: Problem adding rows to database

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
Reputation Points: 23
Solved Threads: 16
Posting Whiz in Training
plazmo is offline Offline
206 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Getting and displaying Output Debug messages to listbox
Next Thread in C# Forum Timeline: Pre-Build and Missing Source issues





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC