473,322 Members | 1,911 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

add on button click

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
Nov 9 '09 #1
5 1477
tlhintoq
3,525 Expert 2GB
.
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?
Nov 9 '09 #2
tlhintoq
3,525 Expert 2GB
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.
Nov 9 '09 #3
Frinavale
9,735 Expert Mod 8TB
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
Nov 9 '09 #4
tlhintoq
3,525 Expert 2GB
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?
Nov 9 '09 #5
Frinavale
9,735 Expert Mod 8TB
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
Nov 9 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Steve | last post by:
Hi, Is it possible to make hitting the enter key in an ASP textbox run the code behind an ASP button on a form? I have a search page which users tend to type in the query then just hit enter...
11
by: CW | last post by:
I have message entry screen that's causing me a bit of an issue. At the moment, there are 2 buttons, one is used to send message to another user (btnSend) and another is used to send messages to...
1
by: Klaus Jensen | last post by:
Hi! This has been annoying me all day, and I can't get it to work It is really driving me nuts! Basicly this simple webapp created to illustrate my problem, renders five buttons, and adds a...
3
by: Imran Aziz | last post by:
Hello All, I have a search text and button that post data and my button handler filters the repeater control. However when the button is clicked the first time. The page_load event is being called...
5
by: Wonder | last post by:
How can I create or use the msgobx to show a message without a default button. The user has explicity to click on the button, so the msgbox closes it. Thanks,
21
by: Ben | last post by:
Hello I have frames set up in an asp.net application and need one frame to refresh another. Seeing as events need to be registered at the time the page is sent from the server, I was wondering...
6
by: user | last post by:
Hello, With ASP.NET2, how to program a button's click ? For example, I have two button and when i click the first one, i would like that the second one will be click too (by programming) ......
7
by: =?Utf-8?B?bWFydGluMQ==?= | last post by:
Hi, All, I create button in the code ( Dim Button as new Button), not using button web component (means not drap button and drop it ont he webform), after that I try to use button_click event,...
9
by: Jonathan Wood | last post by:
Does anyone know of any reason a button on a master page would have no effect? I have a complex HTML page that I'm converting to ASP.NET, and acknowledge I could have something odd that is...
1
by: daonho | last post by:
I tried to use javascript to trigger up the button click function when user press enter key from the textbox. This function work fine with a single button click such has login page. However, if the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.