Loop thru Textboxes which are not empty and insert

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
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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)
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)

sainavya1215Author Commented:
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

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

sainavya1215Author Commented:
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..........
  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 vb.net 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...


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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

sainavya1215Author Commented:
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

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.