Send multiple textbox values to one textbox

pao1011

New member
Joined
Mar 8, 2016
Messages
3
Location
NYC
Programming Experience
Beginner
I have created a multiple dynamic textboxes based on the columns of the datatable. Every time I change tables the textbox number changed depending on the number of columns the datatable has. Now my question is: to send the values of those dynamic created textboxes to a one textbox or label making one string.
I did that reading the values from a listbox but when the values goes to the textbox the output is a list of lines same as the listbox. so I just want one simple string with all the values in TextBox1.
So even the textbox has Multiline set to false still sending the values to a new line as you see in the output
Here is a pic of the form and the code

'Sample code
Dim ta bles As Data Table = ds.Tables(lbltst.Text) ' lbltst.Text means the table selected
Dim column As DataColumn
For Each tables In ds.Tables
For Each column In tables.Columns
If tables.ToString = lbltst.Text Then
ListBox1.Items.Add(column.ColumnName & ", ") ' the comma to separate the values
End If
Next
Next
TextBox1.Lines = ListBox1.Items.Cast(Of String).ToArray ' From list to textbox
If TextBox1.Text.Length <> 0 Then
TextBox1.Text = TextBox1.Text.Substring(0, TextBox1.Text.Length - 2) 'Deleting the last comma
End If


appform.jpg
 
Why would you be taking the data from the multiple TextBoxes at all? The data in those TextBoxes has come from a DataTable. That's where you should be getting the data from to put in the one TextBox.
 
Why would you be taking the data from the multiple TextBoxes at all? The data in those TextBoxes has come from a DataTable. That's where you should be getting the data from to put in the one TextBox.

I know it looks silly doing that but is the only way I came up to insert records to a database since the datatabase and tables will be constantly changed. The user have the choice to select another database as well the tables from the database, so instead of "Insert Into mytable( bla, bla, bla)" it will be: "Insert Into "& txboxtblename.Text & "("& txboxtblefields.Text)" same for the values. btw, the dynamic texboxes will be empty for new data. Remember I'm a newbie. I just start this vb.net a few weeks ago. I have some knowledge of databases for the web. By the way thanks for the reply
 
Last edited:
I was looking for this answer for longtime searching the web without any luck so I decide to join this forum to see if someone come up with the answer but fortunately I figured out first so I will mark this post as answered. I will post the code that I made in case someone are looking for this answer.
When the app opens the form, will display a Row of dynamic created TextBoxes with a dynamic Label on top . This labels and texboxes are created based on the fields of the tables that the database has. When you change the table with a combobox that is loaded with the table names of the database, the number of textboxes changed. If the table has more or less fields they will be show. So if you need to insert records to one of the tables in one of the databases you can't write a code like:
"INSERT INTO tablename(field1, field2 etc...) VALUES(val1, val2 ect...)"
since you don't know which table the user want to insert the records. So you need to get the names of the fields of the table dynamically as well the values of the textboxes with a textbox and the code will be:
"INSERT INTO " & tablenamecombox.SelectedItem & "(" & tablefieldtexbox.Text & ") VALUES(" & valuestextbox.Text & ")"
So, any table can be selected and the text will be inserted successfully. That is the reason for the code. Now the code you will use, aside that you need to have the code to create the textboxes the code will be the following:

'Getting the table fields to the texbox

For Each DymLablel In Panel1.Controls 'You need to use a panel containers to align the labels with the textboxes
tablefieldtexbox.AppendText(DymLabel.Text & ", " 'The comma is to separate the values (Val 1, Val2)
Next
'Will removed the last , and space inserted on the textbox string (val1, val2, ) to (val1, val2)
If tablefieldtexbox.Text.Length <> 0 Then
tablefieldtexbox.Text = tablefieldtexbox.Text.Substring(0, tablefieldtexbox.Text.Length - 2)
End If
'Getting the textboxes Values to the textbox
For Each txbox In Panel2.Controls 'Use panel 2 to align with the lables
valuestextbox.AppendText("'" & txbox.Text & "', ")
Next
If valuestextbox.Text.Length <> 0 Then
valuestextbox.Text = valuestextbox.Text.Substring(0, valuestextbox.Text.Length - 2)
End If

And that's it! The dynamic insert output code will be "INSERT INTO tablenameinuse(field1in use, field2inuse, etc...) VALUES(val1typed, val2typed, etc...)"

Thank you for reading the post
 
Last edited:
Back
Top