Connecting Tech Pros Worldwide Help | Site Map

add on button click

Newbie
 
Join Date: Nov 2009
Posts: 1
#1: 1 Week Ago
Hello Everybody.
I am doing a windows appln in vb.net.
I have a button in my form. When i click the button, textboxes are added dynamically to the form. For 1st click 4 textboxes are added. When clicked again, again the textboxes are added once again...Now i need to save the data from the textboxes to database. Pls anyone help me with that. Here is my coding.

Expand|Select|Wrap|Line Numbers
  1. Dim value1 As String = ""
  2. Dim value2 As String = ""
  3. Dim value3 As String = ""
  4. Dim value4 As String = ""
  5. Dim i As Integer
  6. For i = 0 To FlowLayoutPanel1.Controls.Count - 1
  7. Select Case FlowLayoutPanel1.Controls(i).Name
  8.  
  9. Case "TextBox1"
  10. value1 = FlowLayoutPanel1.Controls(i).Text
  11.  
  12. Case "TextBox2"
  13. value2 = FlowLayoutPanel1.Controls(i).Text
  14.  
  15. Case "TextBox3"
  16. value3 = FlowLayoutPanel1.Controls(i).Text
  17.  
  18. Case "TextBox4"
  19. value4 = FlowLayoutPanel1.Controls(i).Text
  20. End Select
  21. Next i
  22. Dim strsql As String
  23. strsql = "insert into stud values ('" & value1 & "','" & value2 & "','" & value3 & "','" & value4 & "')"
Only the text in the first four textboxes is getting aded to the database.What about the others
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,745
#2: 1 Week Ago

re: add on button click


.
Quote:
Only the text in the first four textboxes is getting aded to the database.What about the others
What others? Your code specifically is getting the text from TextBox1 - TextBox4 and putting it into value1 - value4. What else were you expecting it to do but what you have told it to do?
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,745
#3: 1 Week Ago

re: add on button click


TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,066
#4: 1 Week Ago

re: add on button click


Hmm I would approach this differently.

I would create class level variable that would contain a List Of TextBoxes. When the button was clicked I would create 4 new text boxes and add them to the FlowLayoutPanel1 and to the List Of TextBoxes.

Now, when I need to access the TextBoxes I would just loop through the TextBoxes in the List Of TextBoxes.....

So instead of having:
Expand|Select|Wrap|Line Numbers
  1. Dim value1 As String = ""
  2. Dim value2 As String = ""
  3. Dim value3 As String = ""
  4. Dim value4 As String = ""
  5. Dim i As Integer
  6. For i = 0 To FlowLayoutPanel1.Controls.Count - 1
  7. Select Case FlowLayoutPanel1.Controls(i).Name
  8.  
  9. Case "TextBox1"
  10. value1 = FlowLayoutPanel1.Controls(i).Text
  11.  
  12. Case "TextBox2"
  13. value2 = FlowLayoutPanel1.Controls(i).Text
  14.  
  15. Case "TextBox3"
  16. value3 = FlowLayoutPanel1.Controls(i).Text
  17.  
  18. Case "TextBox4"
  19. value4 = FlowLayoutPanel1.Controls(i).Text
  20. End Select
  21. Next i
  22. Dim strsql As String
  23. strsql = "insert into stud values ('" & value1 & "','" & value2 & "','" & value3 & "','" & value4 & "')"
I would have:
Expand|Select|Wrap|Line Numbers
  1. 'The following will add 4 more TextBoxes to the FlowLayoutPanel1 control 
  2. 'and to the list of TextBoxes that holds a reference to all of the dynamically created TextBoxes.
  3. Dim i As Integer
  4. For i = 0 To 3
  5.   Dim aTextBox As New TextBox()
  6.   aTextBox.Name = "TextBox" + myListOfTextBoxes.Count.ToString
  7.   myListOfTextBoxes.Add(aTextBox)
  8.   FlowLayoutPanel1.Controls.Add(aTextBox)
  9. Next i
  10.  
Please note that myListOfTextBoxes is declared as a class level member and instantiated in the constructor for the class:
Expand|Select|Wrap|Line Numbers
  1. Private myListOfTextBoxes As List(Of TextBoxes)
  2.  
  3. Public Sub New()
  4.   ' This call is required by the Windows Form Designer.
  5.   InitializeComponent()
  6.   myListOfTextBoxes = New List(Of TextBoxes)
  7. End Sub
  8.  
Later, when I need to access the dynamic TextBoxes I can just loop through the List Of TextBoxes:
Expand|Select|Wrap|Line Numbers
  1. Dim textBoxValues As New StringBuilder
  2. For Each dynamicallyCreatedTextBox As TextBox In myListOfTextBoxes 
  3.   textBoxValues.Append(dynamicallyCreatedTextBox.Text)
  4.   textBoxValues.Append(", ")
  5. Next
-Frinny
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,745
#5: 1 Week Ago

re: add on button click


Same logic as I would employ. I just wasn't going to give the OP the complete code for his/her problem until (s)he had taken a day to think about it, visualize what was happening, and tried to figure out a better scheme on there own.

Though there is benefit to giving a great example (which all of yours are), I think there is a lot to be said for someone having to do at least a little work, research and trial and error. We learn so much more by failure and experience than by being handed the answer. Don't you think?
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,066
#6: 1 Week Ago

re: add on button click


I completely agree with you Tlhintoq...

But, the OP obviously has attempted the problem....and I have this squeamish aversion to nasty looking code and couldn't help myself.

Maybe the OP will learn better from example this time round :)

Besides, I didn't give them the "whole" solution...I don't have a clue what they're trying to do with the database stuff. And I didn't throw everything into the methods where they belong either.

Hehe, I didn't even answer their question out-right either....they'll have to look at my proposed solution and think about how to do their database thing.

Their question doesn't even make sense.
The database table that they are updating/inserting/managing has a defined table structure.....

I do not understand how the table is going to be updated with the information the user provides. I mean the user can potentially add an infinite number TextBoxes to fill out....but the table has a finite number of fields to update.

????????? I don't get it ?????????

-Frinny
Reply