Question I can't save update my database; need update command

Godis

Member
Joined
Jul 31, 2019
Messages
19
Programming Experience
Beginner
Error in saving records.png


I have the mind to develop a payroll database.
After I have created the form in Microsoft Visual Studio 2015, I dragged both the nodes of DataGridView and Details front the DataSet window onto the Form.
When I run the application I was able to save two entries I made into the database. But thereafter I couldn't save any record again.
Attached is the Error message displayed when I click on the Save Button.
I earnestly wish someone could help me solve my problem.

- Godis
 
The error seems pretty self-explanatory: the update call needs a valid update command to be able to save modified rows.

Please post your code. We may be able to spot what maybe wrong with your current update command.
 
It appears that you are using a typed DataSet. In that case, the wizard will generate the UpdateCommand automatically IF it is able to do so. If it wasn't able to do so in your case then that is most likely because one or more of your tables doesn't have a primary key. With no primary key, there's no obvious way to uniquely identify a record to update or delete, thus no UpdateCommand or DeleteCommand can be generated. It's a very rare thing that not having a primary key in a database table is a good idea. You should fix that and then regenerate the typed DataSet in the Data Sources window to have those commands automatically generated.
 
For future reference, please don't post pictures of code. Code is text so post it as text, formatted as code for readability. The code editor on this site allows you to highlight specific lines so you can indicate where errors occur or the like. Error messages are also text.

Also, when you do post screenshots, which ca be helpful on occasion, don't post your entire screen when only a small part of it is relevant. Help us to help you by posting all that is relevant and nothing that isn't, in the form easiest to consume. With regards to screenshots, we can't copy and paste text from them so it means we can't easily run code or search for error messages or the like. If it takes a bit more time and effort on your part to require less time and effort on ours, that's what should happen, given that we're the strangers volunteering our time to help you. I'm not saying that that isn't what you were trying to do this time but you'll have a better idea of how next time.
 
The following is the code of the project I am building.
It would be observed in the code that I have two separate save button with different codes, but none of them could save updates.

Am standing by for assistance:-

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
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 Payroll
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);
SqlCommand command = new SqlCommand();


private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'salariesDataSet.Salaries' table. You can move, or remove it, as needed.
this.salariesTableAdapter.FillBy(this.salariesDataSet.Salaries);
}
//Declaration of variables
decimal BasicSalary, Overtime, ExtraOvertime, LeaveOutstanding, GrossSalary, SSNIT13, SSNIT5, TaxableIncome, IncomeTax, Absent, TotalDeductions, NetSalary;
int DaysWorked, DaysAbsent;
DateTime EmpDate, CurrentDate;




private void saveButton_Click(object sender, EventArgs e)
{
this.Validate();
this.salariesBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.salariesDataSet);
}
}

private void salariesBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
try
{
this.Validate();
this.salariesBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.salariesDataSet);
SalariesDataSet.SalariesDataTable deletedSalaries = (SalariesDataSet.SalariesDataTable)
SalariesDataSet.SalariesRow.GetChanges(DataRowState.Deleted);
SalariesDataSet.SalariesDataTable newSalaries = (SalariesDataSet.SalariesDataTable)
SalariesDataSet.SalariesRow.GetChanges(DataRowState.Added);
SalariesDataSet.SalariesDataTable modifiedSalaries = (SalariesDataSet.SalariesDataTable)
SalariesDataSet.SalariesRow.GetChanges(DataRowState.Modified);

{
// Remove all deleted salary from the Salaries table.
if (deletedSalaries != null)
{
salariesTableAdapter.Update(deletedSalaries);
}
// Add new salary to the Salaries table.
if (newSalaries != null)
{
salariesTableAdapter.Update(newSalaries);
}
// Update all modified Salaries.
if (modifiedSalaries != null)
{
salariesTableAdapter.Update(newSalaries);
}
salariesDataSet.AcceptChanges();
}

}

catch (System.Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}

}
}
}
 
Please try again. I asked you previously to post the code as text AND format it as code. You have done the former but not the latter. I tried to do the formatting for you but what you posted is just a complete mess. The indenting is all over the place and the code wouldn't even compile as it is. If what you posted is what you see in VS yourself then it's no wonder you are making mistakes. Make sure that you have formatted the code properly for readability.
 
I have fixed the indentation for you and enclosed your methods inside your class, but I don't have spare time to go over your post. Someone else will need to help you out.
Use indentation provided by VS:
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace Payroll
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);
        private SqlCommand command = new SqlCommand();

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'salariesDataSet.Salaries' table. You can move, or remove it, as needed.
            this.salariesTableAdapter.FillBy(this.salariesDataSet.Salaries);
        }

        //Declaration of variables
        private decimal BasicSalary, Overtime, ExtraOvertime, LeaveOutstanding, GrossSalary, SSNIT13, SSNIT5, TaxableIncome, IncomeTax, Absent, TotalDeductions, NetSalary;

        private int DaysWorked, DaysAbsent;
        private DateTime EmpDate, CurrentDate;

        private void saveButton_Click(object sender, EventArgs e)
        {
            this.Validate();
            this.salariesBindingSource.EndEdit();
            this.tableAdapterManager.UpdateAll(this.salariesDataSet);
        }

        private void salariesBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            try
            {
                this.Validate();
                this.salariesBindingSource.EndEdit();
                this.tableAdapterManager.UpdateAll(this.salariesDataSet);
                SalariesDataSet.SalariesDataTable deletedSalaries = (SalariesDataSet.SalariesDataTable)
                SalariesDataSet.SalariesRow.GetChanges(DataRowState.Deleted);
                SalariesDataSet.SalariesDataTable newSalaries = (SalariesDataSet.SalariesDataTable)
                SalariesDataSet.SalariesRow.GetChanges(DataRowState.Added);
                SalariesDataSet.SalariesDataTable modifiedSalaries = (SalariesDataSet.SalariesDataTable)
                SalariesDataSet.SalariesRow.GetChanges(DataRowState.Modified);

                {
                    // Remove all deleted salary from the Salaries table.
                    if (deletedSalaries != null)
                    {
                        salariesTableAdapter.Update(deletedSalaries);
                    }
                    // Add new salary to the Salaries table.
                    if (newSalaries != null)
                    {
                        salariesTableAdapter.Update(newSalaries);
                    }
                    // Update all modified Salaries.
                    if (modifiedSalaries != null)
                    {
                        salariesTableAdapter.Update(newSalaries);
                    }
                    salariesDataSet.AcceptChanges();
                }
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }
        }
    }
 
I don't even really understand why you're posting that code. I told you that the problem is almost certainly that your database table doesn't have a primary key but you haven't addressed that at all. Do your database tables have primary keys or not? If not, do as i said and add them and then regenerate your DataSet. There's no better way to put people off helping you in the future than ignoring the advice you asked for when it's provided.
 
Thanks jmcilhinney,

I got it right when I went back to the SQL Management Studio and found that the Primary Key was not set. I set it, came back to the VS, deleted the DataSet and reconfigured it, rebuilt the application and now I can save updates.

Thanks a lot to all contributors.
 
came back to the VS, deleted the DataSet and reconfigured it
There was no need to delete the existing DataSet and create a new one. I pointed you to the Data Source window in the first place because there is a button in the toolbar there to rerun the wizard on your existing DataSet to update the schema.
 
Back
Top Bottom