Search code examples
c#.netstrongly-typed-dataset

.NET DataSet not updating with new row


I am trying to add a new typed row in a DataTable and update it to the source but its not working

public partial class JobTableAdapter
{
    public OpenMassSenderCore.OpenMassSenderDBDataSet.JobRow getNewRow()
    {
        return OpenMassSenderDBDataSet.getInstance().Job.NewJobRow();
    }
    public void submitRow(OpenMassSenderCore.OpenMassSenderDBDataSet.JobRow row)
    {
        OpenMassSenderDBDataSet.getInstance().Job.Rows.Add(row);
        OpenMassSenderDBDataSet.getInstance().Job.AcceptChanges();
        Update(OpenMassSenderDBDataSet.getInstance().Job);//thats the default tableadapter's update
    }

    private static JobTableAdapter instance;
    public static JobTableAdapter getInstance()
    {
        if (instance == null) instance = new JobTableAdapter();
        return instance;
    }
}



    private void button1_Click(object sender, EventArgs e)
    {
        OpenMassSenderCore.OpenMassSenderDBDataSet.JobRow job=JobTableAdapter.getInstance().getNewRow();
        job.ID = 2;
        job.query = "";
        job.group = "smaplist1";
        job.sender_account = 1;
        job.status = OpenMassSenderCore.OpenMassSenderDBDataSet.JobRow.JobStatus.PENDING;
        job.user = 1;
        job.message =1;
        JobTableAdapter.getInstance().submitRow(job);
    }

If I press the button twice I get the key(ID) existing exception so this part works ok, the only problem is that its not updating the database(access)


Solution

  • Well, duh, you called AcceptChanges.

    You basically said "Ok, this data is now committed, everything is fine. By the way, DataAdapter, can you commit all the changes on this dataset to the database? Thanks." "Sure, let me see... okay, your data set has no changes, so I'm done". You've thrown away the only information the data adapter can use to actually update the database :)

    (As a side note - please, don't write Java in C#. It hurts. And DataSets are an ancient technology that wasn't updated in years, you might want to find something a bit more uptodate.)