Hello All. I use VS2015 Community and Sql server 2008. You could jump to the last paragraph to see the problem conclusion first.
On a WinForm I have a TypedDataSet, a BindingSource, a SqlDataAdapter(I don't use the auto-generated TableAdapter). I have on the form as you can guess, some DataBinded textboxes and some buttons for add,edit,delete,save and cancel.The program has been tested. Everything works fine.
There is a textBoxUserId and a textBoxUserName. After I press the [New] button, SqlDataAdapter fetches zero row of data from a table on the database, so as to clear the form and get SqlCommandBuilder involved. Then it calls dataTable.Add() to add a row. The field "userId" is now null. I type a user id onto textBoxUserId and I hit TAB. textBoxUserId.Text is copied to the dataTable's UserId field. This is what it is supposed to be.
Then, I added a textBoxUserId_Load Event, the code in it uses the textBoxUserId.Text as a key to fetch the user name from another table on the database, and put the user name into dataTable's UserName Field and textBoxUserName.Text is updated to reflect the change. Now I press the [Add] button, type user id and hit TAB. textBoxUserName gets updated. But dataTable's UserId is not updated to hold textBoxUserId.Text. Instead dataTable's UserId(null now) is fed back to textBoxUserId.Text, empting it . I know dataTable's UserId is null because I use MessageBox.Show() to check it. Even if I put any string value onto any field of dataTable, textBoxUserId.Text cannot be updated to dataTable's UserId field.
In short, when a dataTable with 0 row of data gets one row added, and if there is code in textBoxUserId_Leave() that changes any field of the dataTable, once input focus is moved from textBoxUserId to another control, textBoxUserId is not updated to the underlying dataTable, but instead gets reloaded from the dataTable (nullify textBoxUserId.Text in this case). Any idea ?
On a WinForm I have a TypedDataSet, a BindingSource, a SqlDataAdapter(I don't use the auto-generated TableAdapter). I have on the form as you can guess, some DataBinded textboxes and some buttons for add,edit,delete,save and cancel.The program has been tested. Everything works fine.
There is a textBoxUserId and a textBoxUserName. After I press the [New] button, SqlDataAdapter fetches zero row of data from a table on the database, so as to clear the form and get SqlCommandBuilder involved. Then it calls dataTable.Add() to add a row. The field "userId" is now null. I type a user id onto textBoxUserId and I hit TAB. textBoxUserId.Text is copied to the dataTable's UserId field. This is what it is supposed to be.
Then, I added a textBoxUserId_Load Event, the code in it uses the textBoxUserId.Text as a key to fetch the user name from another table on the database, and put the user name into dataTable's UserName Field and textBoxUserName.Text is updated to reflect the change. Now I press the [Add] button, type user id and hit TAB. textBoxUserName gets updated. But dataTable's UserId is not updated to hold textBoxUserId.Text. Instead dataTable's UserId(null now) is fed back to textBoxUserId.Text, empting it . I know dataTable's UserId is null because I use MessageBox.Show() to check it. Even if I put any string value onto any field of dataTable, textBoxUserId.Text cannot be updated to dataTable's UserId field.
In short, when a dataTable with 0 row of data gets one row added, and if there is code in textBoxUserId_Leave() that changes any field of the dataTable, once input focus is moved from textBoxUserId to another control, textBoxUserId is not updated to the underlying dataTable, but instead gets reloaded from the dataTable (nullify textBoxUserId.Text in this case). Any idea ?