Hi there...
im working in Visual Studio.NET 2005 and C#, i have binded two text boxes with dataTable named dt.
textbox1.Databindings.Add("text", dt, "customerName");
textBox2.Databinding.Add("Text", dt, "billNo");
Here customerName, billNo is the feildName in the table name customer.
This binding works fine. When i run the application i see the values from the first row in the table.
I can also perform navigation, if i bind the datatable with the BindingSource object and then calling its moveFirst(), moveLast() methods.
BindingSource bindingSrc = new BindingSource();
bindingSrc.DataSource = dt;
it also Works fine;
NOW, when it comes to updating. nothing works
for updating i have tried the following methods but does'nt work;
1)
textBox1.DataBindings.Add("text", bindingSource, "customerName");
textBox2.DataBindings.Add("text", bindingSource, "billNo");
da.Update(dt);
This did not worked
2)
textBox1.DataBindings.Add("text", dt, "customerName");
textBox2.DataBindings.Add("text", dt, "billNo");
and in save button event:
da.Update(dt);
This also did not worked.
I tried doing updating in VS.NET 2003 C# it worked perfectly, and the same code does not work in VS.NET 2005.
I am trying for several days now, but could'nt fix this problem.
I want to ask that if there is any problem in vs.net 2005 or the method of binding and updating through text boxes is different in vs 2005. please tell.
I will be very greateful if some body try an example in 2005 and send me in my email: [email protected]
Thanks for reading such a leangthy problem. but plz help
Muhammad Imran Khan 0Posted Sep 13, 2007, 7:18 AM
Hi Dear..... Thanks for reply... but it did'nt worked
here is my code....it is has no compile time errors, and generates no exceptions but update.......un aan...sorry
namespace
textBoxBinding4{
public partial class Form1 : Form{
public Form1(){
InitializeComponent();
}
string strCon = ""; SqlConnection conObj; SqlDataAdapter da; SqlCommandBuilder cmdBuilder; DataTable dt; DataRow dr; BindingSource bindingSource; private void Form1_Load(object sender, EventArgs e){
strCon =
"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Sales And Purchase;Data Source=haseeb";conObj =
new SqlConnection(strCon);conObj.Open();
da =
new SqlDataAdapter("Select * From Customer", conObj);cmdBuilder =
new SqlCommandBuilder(da);dt =
new DataTable("customer"); ;da.TableMappings.Add(
"Table", "customer");da.Fill(dt);
bindingSource =
new BindingSource();bindingSource.DataSource = dt;
// dt.NewRow();textBox1.DataBindings.Add(
"text", dt, "customerName");textBox2.DataBindings.Add(
"text", dt, "billNo");}
private void savebtn_Click(object sender, EventArgs e){
da.Update(dt);
dt.AcceptChanges();
}
private void nextbtn_Click(object sender, EventArgs e){
bindingSource.MoveNext();
}
private void previousbtn_Click(object sender, EventArgs e){
bindingSource.MovePrevious();
}
}
}
here if i bind the text box with BindingSource bindingSource object, the text is displayed in textboxes and it also enables navigation.
eg.
textBox1.DataBindings.Add("text", bindingSource, "customerName");
textBox2.DataBindings.Add("text", bindingSource, "billNo");
And
and if bind text box with dataTable ie. dt
eg
textBox1.DataBindings.Add("text", dt, "customerName");
textBox2.DataBindings.Add("text", dt, "billNo");
THEN, First: Navigation in textboxes is disabled,
And Secondly: Results are the same i,e., No updation Even With
da.AcceptChanges(dt);
Can u please try it on your pc on vs 2005.net and tell me if it works there... because if have tried this in vs 2003.net if worked fine.
please help
Thanks for reply........
Thank you so Much
Yogesh VermaPosted Sep 13, 2007, 5:47 AM
try to accept change method after update.
ds.Tables(0).AcceptChanges()