Answered by:
Add a row to a datatable from a form button click

Question
-
I have a login form connected to a datatable containing 2 columns: username ans password.
I want the user to be able to add new usernames and passwords from a button click after they enter a new username and password into textboxes on "create new user" form. I think I'm close but my table doesn't seem to be updating. Why is the table not updating? This is my code:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click Dim dataset As DataSet dataset = EEDataDataSet Dim table As DataTable = DataSet.Tables("Users") ' Use the NewRow method to create a DataRow 'with the table's schema. Dim newuserrow As DataRow = table.NewRow() ' Set values in the columns: newuserrow("Username") = Me.UsernameTextBox.Text newuserrow("Password") = Me.PasswordTextBox.Text ' Add the row to the rows collection. table.Rows.Add(newuserrow) MsgBox("New User addition successful.") Me.Close() End Sub
Thanks in advance,David
DavidThursday, September 22, 2011 4:57 AM
Answers
-
You only add it to the off line datatable, that is not your database.
If EEDataSet is an instanced generic dataset, than you can use the tableadapter of that to do the update.
However, why are you still using a login form like in 1980.
A VB program (with the newer frameworks) is only running on Widows computers from the type NT.
Those all have a login part and nobody in 2011 is not anymore using that.
So you can use the integrated security and get the user simple by
dim TheUser = environment.username
Success
CorThursday, September 22, 2011 7:31 AM -
I have a login form connected to a datatable containing 2 columns: username ans password.
I want the user to be able to add new usernames and passwords from a button click after they enter a new username and password into textboxes on "create new user" form. I think I'm close but my table doesn't seem to be updating. Why is the table not updating? This is my code:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click Dim dataset As DataSet dataset = EEDataDataSet Dim table As DataTable = DataSet.Tables("Users") ' Use the NewRow method to create a DataRow 'with the table's schema. Dim newuserrow As DataRow = table.NewRow() ' Set values in the columns: newuserrow("Username") = Me.UsernameTextBox.Text newuserrow("Password") = Me.PasswordTextBox.Text ' Add the row to the rows collection. table.Rows.Add(newuserrow) MsgBox("New User addition successful.") Me.Close() End Sub
Thanks in advance,David
David
Hi David,Please take a look at the following code:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click Dim table As DataTable = EEDataDataSet.Tables("Users") ' Use the NewRow method to create a DataRow 'with the table's schema. Dim newuserrow As DataRow = table.NewRow() ' Set values in the columns: newuserrow("Username") = Me.UsernameTextBox.Text newuserrow("Password") = Me.PasswordTextBox.Text ' Add the row to the rows collection. table.Rows.Add(newuserrow) yourTableAdapter.updateAll(EEDataDataSet) MsgBox("New User addition successful.") Me.Close() End Sub
I hope this will be helpful.
Best regards,
Mike Feng [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Sunday, September 25, 2011 8:25 AM
All replies
-
You only add it to the off line datatable, that is not your database.
If EEDataSet is an instanced generic dataset, than you can use the tableadapter of that to do the update.
However, why are you still using a login form like in 1980.
A VB program (with the newer frameworks) is only running on Widows computers from the type NT.
Those all have a login part and nobody in 2011 is not anymore using that.
So you can use the integrated security and get the user simple by
dim TheUser = environment.username
Success
CorThursday, September 22, 2011 7:31 AM -
Try this:
Dim dataset As DataSet dataset = EEDataDataSet Dim table As DataTable = dataSet.Tables("Users") ' Use the NewRow method to create a DataRow 'with the table's schema. Dim newuserrow As DataRow = table.NewRow() ' Set values in the columns: newuserrow("Username") = Me.UsernameTextBox.Text newuserrow("Password") = Me.PasswordTextBox.Text ' Add the row to the rows collection. table.Rows.Add(newuserrow) MsgBox("New User addition successful.") Me.Close()
MitjaThursday, September 22, 2011 7:50 AM -
Mitja,
did you post the same code like the op by mistake ?
Hannes
If you have got questions about this, just ask.
In a perfect world,
users would never enter data in the wrong form,
files they choose to open would always exist
and code would never have bugs.
C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/Thursday, September 22, 2011 7:55 AM -
Take a closer look - its not the same.
his: Dim table As DataTable = DataSet.Tables("Users") mine: Dim table As DataTable = dataSet.Tables("Users")
VB is case sensitive, so small and capital letters count!
DataSet is a class, while dataSet (in this example) is a reference of DataSet class. Big difference.
MitjaThursday, September 22, 2011 10:56 AM -
I didn`t see this, but VB is for sure not case sensitive ? If you use this line from the op and at the end hit enter it just will be changed to your line.
Hannes
If you have got questions about this, just ask.
In a perfect world,
users would never enter data in the wrong form,
files they choose to open would always exist
and code would never have bugs.
C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/Thursday, September 22, 2011 11:01 AM -
I have a login form connected to a datatable containing 2 columns: username ans password.
I want the user to be able to add new usernames and passwords from a button click after they enter a new username and password into textboxes on "create new user" form. I think I'm close but my table doesn't seem to be updating. Why is the table not updating? This is my code:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click Dim dataset As DataSet dataset = EEDataDataSet Dim table As DataTable = DataSet.Tables("Users") ' Use the NewRow method to create a DataRow 'with the table's schema. Dim newuserrow As DataRow = table.NewRow() ' Set values in the columns: newuserrow("Username") = Me.UsernameTextBox.Text newuserrow("Password") = Me.PasswordTextBox.Text ' Add the row to the rows collection. table.Rows.Add(newuserrow) MsgBox("New User addition successful.") Me.Close() End Sub
Thanks in advance,David
David
Hi David,Please take a look at the following code:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click Dim table As DataTable = EEDataDataSet.Tables("Users") ' Use the NewRow method to create a DataRow 'with the table's schema. Dim newuserrow As DataRow = table.NewRow() ' Set values in the columns: newuserrow("Username") = Me.UsernameTextBox.Text newuserrow("Password") = Me.PasswordTextBox.Text ' Add the row to the rows collection. table.Rows.Add(newuserrow) yourTableAdapter.updateAll(EEDataDataSet) MsgBox("New User addition successful.") Me.Close() End Sub
I hope this will be helpful.
Best regards,
Mike Feng [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Sunday, September 25, 2011 8:25 AM