Results 1 to 4 of 4

Thread: How to insert textbox on button click and save data in all the textboxes in db VB.net

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2017
    Posts
    110

    Exclamation How to insert textbox on button click and save data in all the textboxes in db VB.net

    Hi, I have a textbox named txtNumOfT where you can input the number of textboxes you want to insert (maximum is 9, minimum is 1) . When I click the insert textbox button, I want it to create the number of textboxes indicated on txtNumOfT textbox. After that, I'd like to change the name of each control to txtOp1 and so on. I'd also like to be able to save them into my database if I click on save to dab button.

    Please help. I have absolutely no idea how to do this

    UPDATE:

    Okay, so I have already resolved the part where I need to insert textboxes based on the number I entered on the txtNumOfT textbox. The only thing I need to know now is how to save them into the database.

    Code:
                Dim f As TextBox
                Dim i As Integer
                Dim a As Integer = Me.txtNumOfT.Text
                Dim l As Integer = 1
                For i = 1 To a
                    Me.TableLayoutPanel1.Controls.Add(New TextBox)
    
                Next
                For Each f In TableLayoutPanel1.Controls
                    f.Name = "txtBlank" & l.ToString()
                    f.Width = 147
                    f.Height = 24
    
                    l += 1
                Next
    Last edited by astrid22; Mar 17th, 2018 at 07:03 PM. Reason: code

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,943

    Re: How to insert textbox on button click and save data in all the textboxes in db VB

    What do you want to save to the database, and how would it look? I assume you want to save the contents of the textboxes, but you could also be talking about saving the number of textboxes, or something else, so you might as well clarify.

    As to the database, as long as you are only talking about a maximum of 9, you might have nine fields in a table in the DB. When you have a textbox, you could write data for that field, and when there isn't a textbox, you would write a null for that field. That would be one way to do it, but it wouldn't scale well if you have more than just a few textboxes.

    The alternative would be to have two tables in the database. One record would be the master record. I have no idea what would be in that record, and maybe it doesn't even need to exist. In the other table, you'd want one record per textbox. If there is no need for the master record, then the other table could just have one column with a shared value and one column for the value of the textbox.

    As I write that, I realize there are several other alternatives that would also make sense, depending entirely on what you want to store, what else goes with it, and how it will be used.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2017
    Posts
    110

    Re: How to insert textbox on button click and save data in all the textboxes in db VB

    Sorry I forgot to clarify

    I will be putting data in the textboxes and save the data into the database.

    For example, the question is list the names of your favorite friends. Then there's this txtNumOfT textbox where I will input the number of textboxes I need for example 5 textboxes, click on insert button then it will insert 5 textboxes. Then I will input the names of my friends in those 5 textboxes. After that I will click on save to database button to save the question and the names of my friends into the database.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,943

    Re: How to insert textbox on button click and save data in all the textboxes in db VB

    Then why is it limited to 9? I've heard that there is a limit on how many close friends we can have, but still, it seems like your number might go over 9.

    You might consider a FlowLayoutPanel rather than the TableLayoutPanel, and just allowing the number to grow as needed. Another alternative would make working with a database even easier, but it would look totally different, so you might not find it suitable. That other alternative would be to use a DataGridView tied to a Datatable. You'd still be able to add items, and they'd end up in a datatable, which would be easier to use for interacting with a database. In fact, even if you prefer the layout panel you have, you might find it easiest to then put the items into a datatable:
    Code:
    Dim myTable as New Datatable
    
    myTable.Columns.Add("GiveTheColumnANameHere",GetType(String))
    Then you can add rows pretty easily:
    Code:
    Dim nr As DataRow=myTable.NewRow
    myTable.Rows.Add(nr)
    They the datatable could write to a database table pretty easily. However, this isn't a great design. After all, there's no checking for duplicates, or anything of the sort. Still, there is plenty of information about reading/writing a datatable to a database, so it would get you closer as a general design.
    My usual boring signature: Nothing

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width