[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 234
  • Last Modified:

Update table bind to datagrid

I want to update the table that is bind to the datagrid through dataset.

her is what i am doing

private void FrmTemplateNew_Load(object sender, System.EventArgs e)
{
AsaCon= new SqlConnection("user id=sa;" +
      "password=sa;server=harbinger;" +
      "database=oct29_2004; " +
      "connection timeout=30");
AsaCon.Open();
AsaCmd=new SqlCommand("Select * from TblTemplate",AsaCon);
AsaDptr=new SqlDataAdapter(AsaCmd);
Dset=new DataSet("tblTemplate");
AsaDptr.Fill(Dset,"tblTemplate");
dataGrid1.SetDataBinding(Dset,"tblTemplate");
dataGrid1.DataSource=Dset;
}

private void button1_Click(object sender, System.EventArgs e)
{
Dset.AcceptChanges();//I used this becz without it the update was giving error that I
                                 //must specifiy a valid update statement
AsaDptr.Update(Dset,"tblTemplate");
}

Now it is not giving the error but is also not updatting the table.

Please help me out. Its urgent.

0
arvsha
Asked:
arvsha
  • 2
1 Solution
 
123654789987Commented:
U need to make some changes in the code

private void FrmTemplateNew_Load(object sender, System.EventArgs e)
{
AsaCon= new SqlConnection("user id=sa;" +
     "password=sa;server=harbinger;" +
     "database=oct29_2004; " +
     "connection timeout=30");
AsaCon.Open();
AsaCmd=new SqlCommand("Select * from TblTemplate",AsaCon);
SqlCommandBuilder custCB = new SqlCommandBuilder(AsaDptr); // A commandbuilder is required
AsaDptr=new SqlDataAdapter(AsaCmd);
Dset=new DataSet("tblTemplate");
AsaDptr.Fill(Dset,"tblTemplate");
dataGrid1.SetDataBinding(Dset,"tblTemplate");
dataGrid1.DataSource=Dset;
}

private void button1_Click(object sender, System.EventArgs e)
{
AsaDptr.Update(Dset,"tblTemplate");
}

0
 
arvshaAuthor Commented:
I tried it

private void FrmTemplateNew_Load(object sender, System.EventArgs e)
{
AsaCon= new SqlConnection("user id=sa;" +
      "password=sa;server=harbinger;" +
                "database=oct29_2004; " +
      "connection timeout=30");
AsaCon.Open();
AsaCmd=new SqlCommand("Select * from dt_Account",AsaCon);
custCB = new SqlCommandBuilder(AsaDptr); // A commandbuilder is required
AsaDptr=new SqlDataAdapter(AsaCmd);
Dset=new DataSet("dt_Account");
AsaDptr.Fill(Dset,"dt_Account");
dataGrid1.SetDataBinding(Dset,"dt_Account");
dataGrid1.DataSource=Dset.Tables["dt_Account"];
}
private void button1_Click(object sender, System.EventArgs e)
{
//dataGrid1.Update();
Dset.AcceptChanges(); //added this because without it the next stetement is generating error.
AsaDptr.Update(Dset,"dt_Account");
//dataGrid1.Update();
}

but still its not updating the table



0
 
Mohammed NasmanSoftware DeveloperCommented:
Do you have a primay key in dt_Account table?
if you don't, update will not work
0
 
arvshaAuthor Commented:
ya it hasa primary key but still its not working what should I do.

Its urgent please you techi people help me
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now