Problem while inserting a new row in database from C# application
-
I need some help in my project. I am trying to insert a row in my database table using a SQL query from my dataset. If I execute the query from query builder, it is able to insert a new row to my database table. But, as I am trying to use the same query from my code, and try to view the rows from my code (using a getdata() query), the newly inserted row is shown, but if I close the my project, I run it after some time, the recently inserted row is gone ..... I checked the database, but the new row is not there ..... :( What can be wrong ? Please provide me some guidance ...
Apurv “Never trust a computer you can’t throw out a window.” (Steve Wozniak) “There are only two industries that refer to their customers as ‘users’.” (Edward Tufte)
-
I need some help in my project. I am trying to insert a row in my database table using a SQL query from my dataset. If I execute the query from query builder, it is able to insert a new row to my database table. But, as I am trying to use the same query from my code, and try to view the rows from my code (using a getdata() query), the newly inserted row is shown, but if I close the my project, I run it after some time, the recently inserted row is gone ..... I checked the database, but the new row is not there ..... :( What can be wrong ? Please provide me some guidance ...
Apurv “Never trust a computer you can’t throw out a window.” (Steve Wozniak) “There are only two industries that refer to their customers as ‘users’.” (Edward Tufte)
-
Can you the insertion code you are using?
50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!
INSERT INTO PatientsDetails
(FirstName, LastName, DateOfBirth, BloodGroupId, Gender)
VALUES (@FirstName,@LastName,@DateOfBirth,@BloodGroupId,@Gender);
SELECT ReferenceId, FirstName, LastName, DateOfBirth, BloodGroupId, Gender FROM PatientsDetails WHERE (ReferenceId = SCOPE_IDENTITY())Apurv “Never trust a computer you can’t throw out a window.” (Steve Wozniak) “There are only two industries that refer to their customers as ‘users’.” (Edward Tufte)
-
INSERT INTO PatientsDetails
(FirstName, LastName, DateOfBirth, BloodGroupId, Gender)
VALUES (@FirstName,@LastName,@DateOfBirth,@BloodGroupId,@Gender);
SELECT ReferenceId, FirstName, LastName, DateOfBirth, BloodGroupId, Gender FROM PatientsDetails WHERE (ReferenceId = SCOPE_IDENTITY())Apurv “Never trust a computer you can’t throw out a window.” (Steve Wozniak) “There are only two industries that refer to their customers as ‘users’.” (Edward Tufte)
I modified the above query as
INSERT INTO PatientsDetails
(FirstName, LastName, DateOfBirth, BloodGroupId, Gender)
VALUES (@FirstName,@LastName,@DateOfBirth,@BloodGroupId,@Gender);Still its happening the same ...
Apurv “Never trust a computer you can’t throw out a window.” (Steve Wozniak) “There are only two industries that refer to their customers as ‘users’.” (Edward Tufte)
-
I modified the above query as
INSERT INTO PatientsDetails
(FirstName, LastName, DateOfBirth, BloodGroupId, Gender)
VALUES (@FirstName,@LastName,@DateOfBirth,@BloodGroupId,@Gender);Still its happening the same ...
Apurv “Never trust a computer you can’t throw out a window.” (Steve Wozniak) “There are only two industries that refer to their customers as ‘users’.” (Edward Tufte)
This may sound stupid, but are you querying the same DB programatically, and via the SQl query? Have you checked your connection string?
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones "Rumour has it that if you play Microsoft CDs backwards you will hear Satanic messages.Worse still, is that if you play them forwards they will install Windows"
-
I modified the above query as
INSERT INTO PatientsDetails
(FirstName, LastName, DateOfBirth, BloodGroupId, Gender)
VALUES (@FirstName,@LastName,@DateOfBirth,@BloodGroupId,@Gender);Still its happening the same ...
Apurv “Never trust a computer you can’t throw out a window.” (Steve Wozniak) “There are only two industries that refer to their customers as ‘users’.” (Edward Tufte)
I meant the C# code. If this query is working through management studio, it must be correct. There has to something wrong in your code.
50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!
-
I meant the C# code. If this query is working through management studio, it must be correct. There has to something wrong in your code.
50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!
private void btnSave_Click(object sender, EventArgs e)
{
try
{
if (txtFirstName.Text == "")
MessageBox.Show("Patient's First Name can't be blank", "Incomplete data", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
else if (txtDOB.Text == "")
MessageBox.Show("Patient's Date of Birth can't be blank", "Incomplete data", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);if (txtLastName.Text == "") txtLastName.Text = " "; bool gender = true; if (!rbMale.Checked) gender = false; MRMDbDataSetTableAdapters.PatientsDetailsTableAdapter pdta = new MediRecordManager.MRMDbDataSetTableAdapters.PatientsDetailsTableAdapter(); if (pdta.AddNewPatient(txtFirstName.Text, txtLastName.Text, calDOB.SelectionStart, (byte)cbBloodGroup.SelectedValue, gender) > 0) { MessageBox.Show("Data inserted to database !"); btnCancel.Text = "Close"; } } catch (Exception ex) { MessageBox.Show(ex.Message); } } }
Apurv “Never trust a computer you can’t throw out a window.” (Steve Wozniak) “There are only two industries that refer to their customers as ‘users’.” (Edward Tufte)
-
I need some help in my project. I am trying to insert a row in my database table using a SQL query from my dataset. If I execute the query from query builder, it is able to insert a new row to my database table. But, as I am trying to use the same query from my code, and try to view the rows from my code (using a getdata() query), the newly inserted row is shown, but if I close the my project, I run it after some time, the recently inserted row is gone ..... I checked the database, but the new row is not there ..... :( What can be wrong ? Please provide me some guidance ...
Apurv “Never trust a computer you can’t throw out a window.” (Steve Wozniak) “There are only two industries that refer to their customers as ‘users’.” (Edward Tufte)
Are you using an access database? If not just ignore the rest of this posting. Some time ago, someone had the same problem with an access database. His problem was, that he added the empty .mdb file to the project, and every time he build his solution the empty database has overwritten the one with data in it.
Greetings Covean
-
Are you using an access database? If not just ignore the rest of this posting. Some time ago, someone had the same problem with an access database. His problem was, that he added the empty .mdb file to the project, and every time he build his solution the empty database has overwritten the one with data in it.
Greetings Covean
-
I am using MS SQL 2008 server database
Apurv “Never trust a computer you can’t throw out a window.” (Steve Wozniak) “There are only two industries that refer to their customers as ‘users’.” (Edward Tufte)
-
private void btnSave_Click(object sender, EventArgs e)
{
try
{
if (txtFirstName.Text == "")
MessageBox.Show("Patient's First Name can't be blank", "Incomplete data", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
else if (txtDOB.Text == "")
MessageBox.Show("Patient's Date of Birth can't be blank", "Incomplete data", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);if (txtLastName.Text == "") txtLastName.Text = " "; bool gender = true; if (!rbMale.Checked) gender = false; MRMDbDataSetTableAdapters.PatientsDetailsTableAdapter pdta = new MediRecordManager.MRMDbDataSetTableAdapters.PatientsDetailsTableAdapter(); if (pdta.AddNewPatient(txtFirstName.Text, txtLastName.Text, calDOB.SelectionStart, (byte)cbBloodGroup.SelectedValue, gender) > 0) { MessageBox.Show("Data inserted to database !"); btnCancel.Text = "Close"; } } catch (Exception ex) { MessageBox.Show(ex.Message); } } }
Apurv “Never trust a computer you can’t throw out a window.” (Steve Wozniak) “There are only two industries that refer to their customers as ‘users’.” (Edward Tufte)
You are just adding the row to a table in your in-memory dataset. At least that's all the code you've posted does. To save it to the database, call Update() on the data adapter created for your typed data set.