| | |
Problem adding rows to database
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2006
Posts: 8
Reputation:
Solved Threads: 0
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
Thanks for any help possible
C# Syntax (Toggle Plain Text)
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
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
![]() |
Similar Threads
- How to adding new data to database (Visual Basic 4 / 5 / 6)
- insert multiple rows in database (MS SQL)
- Problem when adding delete confirmation (ASP)
- Deleting the database rows and adding new rows (Visual Basic 4 / 5 / 6)
- script problem adding to database (PHP)
- Having problems with adding a row to a DataSet (VB.NET)
- HELP PHP w/SQL Code HELP (PHP)
Other Threads in the C# Forum
- Previous Thread: Getting and displaying Output Debug messages to listbox
- Next Thread: Pre-Build and Missing Source issues
| Thread Tools | Search this Thread |
.net access ado.net algorithm alignment array barchart bitmap box broadcast buttons c# chat check checkbox client color combobox control conversion csharp custom customactiondata database datagrid datagridview dataset datetime degrees development draganddrop drawing encryption enum event excel file files form format forms function gdi+ hospitalmanagementsystems httpwebrequest image index input install java label list listbox listener mandelbrot math mono mouseclick mysql networking operator path photoshop picturebox pixelinversion post programming radians regex remote remoting richtextbox save saving serialization server sleep socket sql statistics stream string table tcp text textbox thread time timer treeview update usercontrol validation view visualstudio webbrowser windows winforms wpf xml





