Andy Wij

Andy Wij

  • NA
  • 26
  • 35.1k

Can't update record after call another form

Sep 27 2011 11:58 PM
I have a problem about .update() method Say that I have a table Currency with fields: Name, Symbol, Rate, and Country.

Form1 have a datagrid and button Edit, Form2 have a detail of selected record from Form1 in textbox.

So my problem is when I edit record in form2 and save it, datatable have change but not the database in server. am I missing something?
this is my code:

Form1
------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using KK.Class;

namespace KK
{
  namespace Master.Forms
  {
  public partial class Currency : Form
  {
  public SqlDataAdapter da = null;
  public DataTable dt = null;
  //public BindingSource bs = null;
  //public DataSet ds;
  private string sql = null;
  //public DataViewManager dv;
  public CurrencyManager myCurrencyManager = null;
  public SqlCommandBuilder cb;
 

  public Currency()
  {
  InitializeComponent();
  }

  private void Currency_Load(object sender, EventArgs e)
  {
  Connection con = new Connection();

  sql = "select * from currency";

  da = new SqlDataAdapter(sql, con.open_connection());
  cb = new SqlCommandBuilder(da);

  //ds = new DataSet();
  dt = new DataTable();
  //da.TableMappings.Add("Table", "Currency");
  da.Fill(dt);

  //bs = new BindingSource();
  //bs.DataSource = dt;

  grid_currency.DataSource = dt;
  //da.Fill(ds);
  //bs = new BindingSource();
  //bs.DataSource = ds.Tables["Currency"];
 
  //da.Fill(ds);

  //grid_currency.DataSource = bs;

  //dv = ds.DefaultViewManager;

  txt_tes.DataBindings.Add("Text", dt, "Name");

  myCurrencyManager = (CurrencyManager)this.BindingContext[dt];

  //myCurrencyManager.Position = 0;
  }

  private void cmd_edt_Click(object sender, EventArgs e)
  {
  int position = Convert.ToInt32(myCurrencyManager.Position.ToString());

  Currency_det currency_det = new Currency_det(da, dt, position);
  currency_det.BindingContext = this.BindingContext;

  if (currency_det.ShowDialog() == DialogResult.OK)
  {
  dt.AcceptChanges();
  grid_currency.Refresh(); 
  }
  }

  private void cmd_del_Click(object sender, EventArgs e)
  {
  try
  {
  grid_currency.Rows.RemoveAt(grid_currency.CurrentRow.Index);
  da.Update(dt);
  MessageBox.Show("Data success updated");
  }
  catch
  {
  MessageBox.Show("Data fail updated");
  }
  }

  private void cmd_ext_Click(object sender, EventArgs e)
  {
  this.Close();
  }

  }
  }
}

Form2
------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace KK
{
  namespace Master.Forms
  {
  public partial class Currency_det : Form
  {
  public DataTable dtt;
  public SqlDataAdapter dat;
  public CurrencyManager myCurrencyManager;
  int position;
  public SqlCommandBuilder cb;

  public Currency_det(SqlDataAdapter dAdapter ,DataTable dTable,int pos)
  {
  position = pos;
  dtt = dTable;
  dat = dAdapter;
  InitializeComponent();
  }

  private void Currency_det_Load(object sender, EventArgs e)
  {
  cb = new SqlCommandBuilder(dat);

  myCurrencyManager = (CurrencyManager)this.BindingContext[dtt];
  myCurrencyManager.Position = position;

  txt_name.DataBindings.Add("Text", dtt , "Name");
  txt_symbol.DataBindings.Add("Text", dtt, "Symbol");
  txt_rate.DataBindings.Add("Text", dtt, "Rate");
  txt_nation.DataBindings.Add("Text", dtt, "Country");
  }

  private void cmd_ext_Click(object sender, EventArgs e)
  {
  this.Close();
  }

  private void cmd_save_Click(object sender, EventArgs e)
  {
  dat.Update(dtt);
  this.DialogResult = DialogResult.OK;
  }
  }
  }
}


If I add/edit record directly from datagrid and call update method in form1 without call form2, It can successful update to grid.. But I don't want to make user to add/edit directly from datagrid, I want them to add/edit record on another form.. So am I missing something?

Answers (3)