RSS Forums RSS
Please support our C# advertiser: Programming Forums
Views: 2529 | Replies: 1
Reply
Join Date: Feb 2006
Posts: 8
Reputation: student_help is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
student_help student_help is offline Offline
Newbie Poster

Problem adding rows to database

  #1  
Mar 20th, 2006
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();
    }
}
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: Ohio
Posts: 204
Reputation: plazmo is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 16
plazmo's Avatar
plazmo plazmo is offline Offline
Posting Whiz in Training

Re: Problem adding rows to database

  #2  
Mar 20th, 2006
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
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 2:43 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC