Answer the question
In order to leave comments, you need to log in
C# program adding but not editing or deleting data from database?
I made a program in Visual Studio in C# designed to write, edit and delete data from a single-table Microsoft SQL Server database.
The program successfully writes new data to a database table, but when you try to delete or edit data in any of the rows of the table, a message appears: "Update requires a valid UpdateCommand when passing a DataRow collection with modified rows." That is, changes are written to the table of the application itself in the DataGridView, but when you try to save this change by clicking on the button, this message appears, and nothing changes in the database
When you try to delete the entire row with the key from the BindingNavigator, no messages appear, the row in the application table is deleted , but nothing changes in the database
As a result, at the moment only the introduction of new data into the database functions.
Part of the program code:
namespace WindowsFormsApplication1
{public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'spisok_zakazovDataSet.Table_1' table. You can move, or remove it, as needed.
this.table_1TableAdapter.Fill(this.spisok_zakazovDataSet.Table_1);
}
private void button1_Click(object sender, EventArgs e)
{
try
{
this.Validate();
this.table1BindingSource.EndEdit();
this.table_1TableAdapter.Update(this.spisok_zakazovDataSet.Table_1);
MessageBox.Show("Update successful");
}
catch (System.Exception ex)
{
MessageBox.Show("Update failed");
}
}
}
Answer the question
In order to leave comments, you need to log in
Firstly, the problem with the deletion may be due to the fact that other tables have links to the records to be deleted, and in this case, you need to set up a cascade deletion. As for the update... I would do the update via a SQL query. UPDATE. You just take new values from your table cells, paste them into the query string, and that's it.
string sql = "UPDATE AGR SET SHORTNAME_AGR=\'" + dataGridView1.Rows[lastSelectedRowIndex].Cells[0].Value.ToString() + "\', NAME_AGR=\'" + dataGridView1.Rows[lastSelectedRowIndex].Cells[1].Value.ToString()+"\' " +
"WHERE ID_AGR=(select AGR.ID_AGR from AGR where AGR.SHORTNAME_AGR=\'"+lastSelectedRow+"\')";
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question