Loop thru Textboxes which are not empty and insert

Posted on 2004-11-28
Last Modified: 2008-02-20
I have 4 textbox groups (ie 8 textboxes)  on windows form where user enters the lanuage English and french text

Database Table:
Tbl_Languages :
Language_ID   auto
Line_Number   Integer
Englishtext   varchar(40)
FrenchText    varchar(40)

TxtBoxEnglish1   Indicates line 1 eng entered
TxtBoxFrench1    indicates line1 french entered

TxtboxEnglish2    2nd line entered
TxtboxFrench2     2nd line entered

TxtboxEnglish3    3rd line entered
TxtboxFrench3     3rd line entered

TxtboxEnglish4    4th line entered

I need  a loop where it checks the textboxes entered and insert into db

for eg if txtboxenglish1 and txtboxfrench1 are entered 2 records exist with Eng as line1 and french as line1
if txtboxenglish1/txtboxfrench1/txtboxenglish4/txtboxfrench4 entered it inserts 4 records as eng linenumber1,french linenumber1,english linenumber4,frenchline number4

How can we achieve this
Question by:sainavya1215
    LVL 25

    Assisted Solution

    Hi Sainavya1215,

    There is something I don't understand, if 2 textboxes are entered (txtboxenglish1 and txtboxfrench1)

    why 2 records ? Why not one, Because your table has a french and a english Varchar in its row

    Language_ID   auto
    Line_Number   Integer
    Englishtext   varchar(40)
    FrenchText    varchar(40)
    LVL 1

    Assisted Solution

    why not just do an If statement?


    If txtboxenglish1.text.trim <> "" Then
        ' Do you insert sql here
    End If

    Or write a sub routine to do it.

    Private sub InsertTextToDB(ByRef textBoxEnglish as TextBox, ByRef textBoxFrench as TextBox, ByVal LineNumber as Integer)

       If textBoxEnglish.Text.Trim <> "" Then
          ' Insert into Database
       End If

    End Sub

    Then call the routine passing in your two text boxes,


    InsertTextToDB(txtboxenglish1, txtboxfrench1, 1)
    InsertTextToDB(txtboxenglish2, txtboxfrench2, 2)
    InsertTextToDB(txtboxenglish3, txtboxfrench3, 3)
    InsertTextToDB(txtboxenglish4, txtboxfrench4, 4)


    Author Comment

    Hi Ronald,
    Your right ..........I am sorry............... Yes we need to check what all textboxes are filled group wise and insert the records.

    could that logic be shown............ I have to pass these values to a DLL which invokes a stored procedure


    Author Comment

    Lets say all textboxes are empty then nothing should be inserted........... Its not compulsory that all are filled. The validation is that only Groups should be filled compulsarily. I already had done that........ For eg if txtenglish1 has been entered txtfrench1 should be entered .........
    I have taken care of those validations..........
    LVL 4

    Accepted Solution

      In .NET there is no textBox array like there was in VB6.
    To get around this, you create a textbox 'Collection', and use the collection.

    I Tested the code below in and had tBoxE for your TextBoxEnglish(1..4) and tBoxF for your TextBoxFrench(1..4)

        'Defined here so they work in all subrouotines below
        Dim colTxtBoxes As New Collection
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            'in .net there is no textbox array, so you have to add the text boxes to the collection manually

        End Sub

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim iCount As Integer
            Dim tBoxE As TextBox
            Dim tBoxF As TextBox

            For iCount = 1 To 4
                tBoxE = colTxtBoxes.Item(iCount)
                tBoxF = colTxtBoxes.Item(iCount + 4)
                If Trim(tBoxE.Text) <> "" And Trim(tBoxF.Text) <> "" Then
                    'Replace MsgBox with your "Insert Statement.  Left MsgBox so you could cut and paste working code."
                    'I.E. insert into Tbl_Languages (EnglishText,FrenchText,LineNumber) _
                    '                  Values(tBoxE.Text,tBoxF.Text,iCount)
                    MsgBox(tBoxE.Text + " " + tBoxF.Text)
                End If

        End Sub

    End Class

    This should give you your loop like you wanted...

    LVL 3

    Assisted Solution

    This may be the loop you're looking for.  It loops through controls looking for textboxes that are not empty.

            Dim subCtrl As Control
            Dim CtlName As String

            For Each subCtrl In TabPage1.Controls  'Replace TabPage1 with groupbox or form etc

                CtlName = subCtrl.GetType().ToString

                If InStr(CtlName, "TextBox") <> 0 and subCtrl.Text <> "" Then

                    'Add Your record here

                End If


    Author Comment

    Hi all,

    Pls help me out with this question which is on the same Textboxes being inserted into a tran stored proc

    Its a bit urgent

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
    Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now