Dataset BindingContext problem (can not add new row to dataset correctly)

sercanparlak

New member
Joined
Sep 14, 2004
Messages
3
hi,
i have problem with dataset. I create a form that has two textboxes, 1 datagrid and 4 buttons. textboxes are binding to datesets columns.

my problem is when i want to add a new record to dataset with "BindingContext[dataSet11, "deneme"].AddNew()" it adds a new row at the end of dataset with null values. And updates current row with values that i want to add to the last row of dataset. thanks for helps.

You can see database preview at the bottom of code.

Here Is My Code
*******************************************************************
//Fills dataset
private void button1_Click(object sender, System.EventArgs e)
{
dataSet11.Clear();
sqlDataAdapter1.Fill(dataSet11);
dataGrid1.DataSource=dataSet11.Tables["deneme"];
}

private void dataGrid1_Click(object sender, System.EventArgs e)
{
this.BindingContext[dataSet11,"deneme"].Position=dataGrid1.CurrentRowIndex;
}

//delete current row
private void button4_Click(object sender, System.EventArgs e)
{
this.BindingContext[dataSet11, "deneme"].RemoveAt(this.BindingContext[dataSet11, "deneme"].Position);
}

//add new row, where the problem occurs.
private void button2_Click(object sender, System.EventArgs e)
{
BindingContext[dataSet11, "deneme"].AddNew();
BindingContext[dataSet11, "deneme"].EndCurrentEdit();

}

//update current row
private void button3_Click(object sender, System.EventArgs e)
{
//this.textBox1.DataBindings["Text"].BindingManagerBase.EndCurrentEdit();
BindingContext[dataSet11, "deneme"].EndCurrentEdit();
}

//update database
private void button5_Click(object sender, System.EventArgs e)
{
sqlDataAdapter1.Update(dataSet11);
}
*******************************************************************
-----------------------------------------
DB Before add
-----------------------------------------
ad soyad
--- --------
1- ahmet demir
2- veli gul


-----------------------------------------
I insert "cemal" "kaya"
DB After Add (before add currentrow was "ahmet demir")
-----------------------------------------
ad soyad
--- --------
1- cemal kaya
2- veli gul
3- "null" "null"
 
I would go by adding the row directly into the data table and not through the BindingContext.

Since everything is dynamicaly linked togheter, it will automaticaly be updated.

I assume that you know how to add a row to a DataTable and assign them values.

Cheers
 
i tried that way too. this adds a row with empty values at the end; but binds the values to the current row. where is my mistake, someone please tell me.

DataRow newRow = dataSet11.Tables[0].NewRow();
newRow["ID"]=textBox1.Text;
newRow["ad"]=textBox2.Text;
newRow["soyad"]=textBox3.Text;
dataSet11.Tables[0].Rows.Add(newRow);
 
Back
Top