none
Dadapter.Update(DS,"TBname") success but no update occured on DB RRS feed

  • Question

  • Hi, this code

     

    DataSet DS = new
     DataSet();
    SqlDataAdapter DataAdapter = new SqlDataAdapter("select * from Operation" ,cor);
    private void dataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
    if (dataGrid1.SelectedIndex == -1)
    return ;


    if (dataGrid1.SelectedIndex != (dataGrid1.Items.Count - 1))
    {
    OperationNumberTextBox.Text = DS.Tables["Operation" ].Rows[dataGrid1.SelectedIndex][0].ToString().Trim();
    DoctorTextBox.Text = DS.Tables["Operation" ].Rows[dataGrid1.SelectedIndex][1].ToString().Trim();
    CostTextBox.Text = DS.Tables["Operation" ].Rows[dataGrid1.SelectedIndex][2].ToString().Trim();
    OperationDatePicker.Text = DS.Tables["Operation" ].Rows[dataGrid1.SelectedIndex][3].ToString().Trim();
    ResaultTextBox.Text = DS.Tables["Operation" ].Rows[dataGrid1.SelectedIndex][4].ToString().Trim();
    NoteTextBox.Text = DS.Tables["Operation" ].Rows[dataGrid1.SelectedIndex][5].ToString().Trim();
    newad.Fill(sos, "sos" );
    CoDoctorListBox.Items.Clear();
    CoDoctorListBox.Items.Add(sos.Tables["sos" ].Rows[i][0]);
    }



    }

    //this button for update DB
    private void button4_Click(object sender, RoutedEventArgs e)
    {
    DS.Tables["Operation" ].Rows[dataGrid1.SelectedIndex][0]=OperationNumberTextBox.Text;
    DS.Tables["Operation" ].Rows[dataGrid1.SelectedIndex][1] = DoctorTextBox.Text;
    DS.Tables["Operation" ].Rows[dataGrid1.SelectedIndex][2]=CostTextBox.Text ;
    DS.Tables["Operation" ].Rows[dataGrid1.SelectedIndex][3]=OperationDatePicker.Text;
    DS.Tables["Operation" ].Rows[dataGrid1.SelectedIndex][4]=ResaultTextBox.Text;
    DS.Tables["Operation" ].Rows[dataGrid1.SelectedIndex][5]=NoteTextBox.Text;
    CoDoctorListBox.Items.Clear();
    //and here no list box clear occured?? what is the reason?

    SqlCommandBuilder tm = new SqlCommandBuilder(DataAdapter);
    DS.AcceptChanges();
    DataAdapter.Update(DS, "Operation" );//error Exception occure here but when I set the commandbuilder and accept DS changes no Exception occured but also no change occured on DB

    }

    when this code run there is no update on database occured

    the data will be fetched from the database to wpf window controls so the user can update data in controls and press button 4 to save any change occured if any , it will be saved in the same data base and the same table (edit table).

    so I use dataadapter.update(dataset,"tablename");

    but I think the problem is that I edit the dataset date in button 4

    I noticed that when that work sucsessfully when I use it with datagrid ,when I edit data in datagrid and resave changes in database

    I don't set the command builder and the don't use the ds.accept(); but it saved.

    what is the problem and what can I do?

    • Moved by Sheldon _Xiao Friday, January 7, 2011 8:56 AM (From:Windows Presentation Foundation (WPF))
    Wednesday, January 5, 2011 7:42 PM

Answers

  • but when I take the data from the Dataset to Textboxes and edit it .It will not save change's

    I assume you're talking about the code where you're taking the data from the TextBoxes, this bit here:

    DS.Tables["Operation"
    ].Rows[dataGrid1.SelectedIndex][0]=OperationNumberTextBox.Text;
    
    // etc.etc. for the rest of the TextBoxes
    

    You probably need an .EndEdit() on that row after you've done the above. So try this after you've set those columns and before you do the .Update():

    DS.Tables["Operation"
    ].Rows[dataGrid1.SelectedIndex].EndEdit();
    
    

     


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    • Marked as answer by abulwleed Thursday, January 13, 2011 1:19 AM
    Wednesday, January 12, 2011 5:55 PM

All replies

  • please help me guys ..Mr. Brian  where are you ?



    All experts help this poor guy.   

    Thursday, January 6, 2011 4:11 AM
  • I thing this problem is difficult for me and all beginners not for experts...

    where are you experts ..?

    Thursday, January 6, 2011 4:54 PM
  • please help me guys ..Mr. Brian  where are you ?



    All experts help this poor guy.   


    Sorry abulwleed,  I don't use DataSets so I am not the person to help you this one.  Hopefully someone else can jump in.
    Thursday, January 6, 2011 5:01 PM
  • Hi abulwleed,

    Based on your description I think your issue is more related to ADO.NET DataSet forum, so I move your thread to that forum to get better and quicker responses.

    http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataset/threads

    Best regards,


    Sheldon _Xiao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, January 7, 2011 8:55 AM
  • Don't do an .AcceptChanges() before the .Update(). When you .AcceptChanges(), all the .Rows in the DataSet.DataTables are marked as Unchanged, so the .Update() thinks there's nothing to do.


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Saturday, January 8, 2011 5:32 PM
  • Don't do an .AcceptChanges() before the .Update(). When you .AcceptChanges(), all the .Rows in the DataSet.DataTables are marked as Unchanged, so the .Update() thinks there's nothing to do.


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Thank you. even when I remove AcceptChanges() no changes will happen to the database I want to notice the note that when I update the Dataset is the DataContext for the DGV and when I update the DGV rows and apply the aboe code -except AcceptChanges() - the dataset will be edited and the DB will save the changes but when I take the data from the Dataset to Textboxes and edit it .It will not save change's. so Here I don't know what is the problem exactly .NO exception raised. thanks fro efforts.
    Wednesday, January 12, 2011 9:44 AM
  • but when I take the data from the Dataset to Textboxes and edit it .It will not save change's

    I assume you're talking about the code where you're taking the data from the TextBoxes, this bit here:

    DS.Tables["Operation"
    ].Rows[dataGrid1.SelectedIndex][0]=OperationNumberTextBox.Text;
    
    // etc.etc. for the rest of the TextBoxes
    

    You probably need an .EndEdit() on that row after you've done the above. So try this after you've set those columns and before you do the .Update():

    DS.Tables["Operation"
    ].Rows[dataGrid1.SelectedIndex].EndEdit();
    
    

     


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    • Marked as answer by abulwleed Thursday, January 13, 2011 1:19 AM
    Wednesday, January 12, 2011 5:55 PM
  • thnk you in advance

    that was very helpfull.

    thank you

    Thursday, January 13, 2011 1:20 AM
  • You're welcome! I'm glad I could help! =0)
    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Thursday, January 13, 2011 3:27 PM