Database Binding , data not being saved

waheedrafiq

Member
Joined
Jan 4, 2015
Messages
11
Programming Experience
1-3
hi guys

I try number of sites, including this A Detailed Data Binding Tutorial - CodeProject and am still unable to figure out what is going on with my save button.

here is the code
private void btnSave_Click(object sender, EventArgs e)
{


this.Validate();
this.customerDetailsBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.RZMegaDesktopDataSet);


MessageBox.Show("Record Saved");




}
I have double check and triple check the names just to make sure i haven't misspell any thing , the code just does not save to the database

any ideas please help am trying to learn from my mistakes.
 
When you call UpdateAll, there are only three possible outcomes:

1. The call fails and an exception is thrown.
2. The call succeeds and returns zero, which means that there were no changes in the DataSet to save.
3. The call succeeds and returns a non-zero value, which means that there were changes and they were saved.

Presumably it's not the first option in your case, given that you didn't mention an exception. That means that the call is succeeding, so you need to test the value returned by UpdateAll. I'll wager that it will be non-zero, indicating that there were indeed changes and they were indeed saved. A lot of people get tripped up because they don;t actually know where or when to look for the changes in the database. In that case, follow the first link in my signature to learn how local data files are managed. That will hopefully shed some light on the issue.
 
Hi jmcilhinney

I check out your first link it was definitely useful and educational , but I think and don't quote me on this its not related to my error. as I am able to view the table "CustomerDetails" when I run the app , I have a first form call frmMain with three buttons one of them is " Add new Customer Record" up on clicking this event I am taken to the frmCustomerRecs form as you will notice I have added buttons as well as left the Navigation bar , when I click on the add new 'button' notice I get '-1' I don't know how I am getting this , also notice from the screen shot the navigation bar increments to '3' one fills in the data and clicks save , you can still view the data go back forth and even return back to the frmMain menu then return back and you will be able to view the data with out any errors. Now my problem is when I exit it don't save the data as if the heap was wipe clean and nothing was written in .mdf file.

I have work on this now for my second week without any result and I bet ya its something very , very simple that I have over look.

attach are screen shots and the code from frmCustomerRecs if you can replicate this problem and fix it please , please education me as i serious have drifted off to planet Mars.

sql queire to create the table
CREATE TABLE [dbo].[CustomerDetails] (
[CustomerID] INT IDENTITY (1, 1) NOT NULL,
[First Name] NCHAR (50) NULL,
[Surname] NCHAR (50) NULL,
[Company Name] NCHAR (50) NULL,
[Address1] NCHAR (100) NULL,
[PostCode] NCHAR (30) NULL,
[Telephone] NCHAR (50) NULL,
NCHAR (50) NULL,
[Business Customer] BIT NULL,
[Last Record Saved] DATETIME NULL,
PRIMARY KEY CLUSTERED ([CustomerID] ASC)
);


and the code from frmCustomerRecs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace rzMegaD
{ // start namespace
public partial class frmCustomerRecs : Form
{ // start partial class
public frmCustomerRecs()
{
InitializeComponent();
}


private void customerDetailsBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();
this.customerDetailsBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.RZMegaDesktopDataSet);


}


private void frmCustomerRecs_Load(object sender, EventArgs e)
{
//TODO: This line of code loads data into the 'rZMegaDesktopDataSet.CustomerDetails' table. You can move, or remove it, as needed.
this.customerDetailsTableAdapter.Fill(this.RZMegaDesktopDataSet.CustomerDetails);


}




private void btnSave_Click(object sender, EventArgs e)
{


this.Validate();
this.customerDetailsBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.RZMegaDesktopDataSet);


this.customerDetailsTableAdapter.Update(this.RZMegaDesktopDataSet);






MessageBox.Show("Record Saved");




}


private void btnExit_Click(object sender, EventArgs e)
{
this.Hide();

}


private void btnAddNew_Click(object sender, EventArgs e)
{
// add new record
this.customerDetailsBindingSource.AddNew();
}


private void btnRemove_Click(object sender, EventArgs e)
{
// remove current record
MessageBox.Show("Are you sure you want to remove this record !","", MessageBoxButtons.OKCancel);






this.customerDetailsBindingSource.RemoveCurrent();


}


private void btnLastRecord_Click(object sender, EventArgs e)
{
this.customerDetailsBindingSource.MoveLast(); // move to the last record
}


private void btnFirstRecord_Click(object sender, EventArgs e)
{
this.customerDetailsBindingSource.MoveFirst(); // go to first record
}


}
} // ending namespace



as you can see from the coding its very , very simple so why it doesn't work i don't fully understand

any support is welcome many thanks
 

Attachments

  • Datasource.PNG
    Datasource.PNG
    10.2 KB · Views: 59
  • frmCustomerRecs.PNG
    frmCustomerRecs.PNG
    88.9 KB · Views: 61
  • frmCustomerRecs_screenshot2.PNG
    frmCustomerRecs_screenshot2.PNG
    33.9 KB · Views: 66
  • frmMain.PNG
    frmMain.PNG
    49.9 KB · Views: 55
  • sql_screenshot.PNG
    sql_screenshot.PNG
    81.4 KB · Views: 66
It's like you didn't read my post.
When you call UpdateAll, there are only three possible outcomes:

1. The call fails and an exception is thrown.
2. The call succeeds and returns zero, which means that there were no changes in the DataSet to save.
3. The call succeeds and returns a non-zero value, which means that there were changes and they were saved.

Presumably it's not the first option in your case, given that you didn't mention an exception. That means that the call is succeeding, so you need to test the value returned by UpdateAll. I'll wager that it will be non-zero, indicating that there were indeed changes and they were indeed saved. A lot of people get tripped up because they don;t actually know where or when to look for the changes in the database. In that case, follow the first link in my signature to learn how local data files are managed. That will hopefully shed some light on the issue.

Did you do that? What was the value?

If you actually read the information provided by my link then you know how the Copy To Output Directory property of the data file affects its behaviour in this regard. What is yours set to?
 
Thanks for the reply back jmcilhinney , I did read your post , and much appreciated for the advice provided. I think its my lack of understanding on database and how they are logically , pragmatically saved in the actual database , going to re-read all your links and then come back to this post hopefully I would have figure out a fix
 
I like to give special thanks to the forum moderator Jmchilhinney , the above codes works fine. I followed Jmchihinney advice and went back to basic , on his link there is a link to "Beginner Tutorial" , I suggest to all that you click on this link as its has tons of tutorial with easy to read step by step instructors and explanation about how to build a database.

my problem came down this (If the new record doesn't appear when you restart, go back to Design Time. In the Solution Explorer, click on your Database under Resources to select it. Now have a look at the Properties window below the Solution Explorer. Locate a property called Copy to Output Directory. It has three settings: Do not copy, Copy always, and Copy if newer. If your database is not updating, try either Copy if newer or Copy always.) and this works spot on.

I can now relax and continue on with my database project learning
 
Back
Top Bottom