tags:

views:

300

answers:

3

Hi there...

I am a DB programming noob. I need to populate a DB from textbox fields but when I try to then commit it to the DB, I go and view the database and all I see is Nulls... nothing is being saved... please help..

thanks

private void btnSubmit_Click(object sender, EventArgs e)
    {
        TradesDataSet.TradesRow newTradesRow = tradesDataSet.Trades.NewTradesRow();

        newTradesRow.ID = textBoxTradeID.Text; 
        newTradesRow.EntryPrice = textBoxEntryPrice.Text;
        newTradesRow.ExitPrice = textBoxExitPrice.Text;            

        tradesDataSet.Trades.Rows.Add(newTradesRow);
        tradesDataSet.Trades.AcceptChanges();

        try
        {

            this.Validate();
            this.tradesBindingSource.EndEdit();
            this.tradesTableAdapter.Update(this.tradesDataSet.Trades);                
            MessageBox.Show("Update successful");
        }
        catch (System.Exception ex)
        {
            MessageBox.Show("Update failed");
        }
    }        
+4  A: 

Remove the AcceptChanges call. Data adapter's Update method looks at the changes in the database and uses the change list to update the actual database. It automatically accepts the changes in the DataSet after the update. If you call AcceptChanges on the DataSet manually before updating, the DataAdapter will think nothing is changed and doesn't do anything.

Mehrdad Afshari
I tried that. It still doesn't work... :( Thanks though, I think I'm going to take GxG's advice and checkout SqlClient...
Woody
A: 
private void btnSubmit_Click(object sender, EventArgs e) 
{ 
    TradesDataSet.TradesRow newTradesRow = tradesDataSet.Trades.NewTradesRow(); 

    newTradesRow.ID = textBoxTradeID.Text;  
    newTradesRow.EntryPrice = textBoxEntryPrice.Text; 
    newTradesRow.ExitPrice = textBoxExitPrice.Text;             

    tradesDataSet.Trades.Rows.Add(newTradesRow); 
    //Wrong, this command says that what I have in the dataset is what is in 
    //the database.  You only use this if you manually update the dataset in 
    //the background.
    //tradesDataSet.Trades.AcceptChanges(); 

    try 
    { 
        //EndEdit then force a validate.
        this.tradesBindingSource.EndEdit();
        this.Validate();  
        //Internally this method calls .AcceptChanges();
        this.tradesTableAdapter.Update(this.tradesDataSet.Trades);                 
        MessageBox.Show("Update successful"); 
    } 
    catch (System.Exception ex) 
    { 
        MessageBox.Show("Update failed"); 
    } 
}         
Spence