• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 298
  • Last Modified:

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
4 Solutions
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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

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


Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now