Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
hi all,

im sorry if this question is already been posted before.im not sure how to check every single textboxes that key in by user..if it is empty string then convert to 0 auto..

my code is:

Dim con As New SqlConnection(connectionString)
Dim cmd4 As New SqlCommand
cmd4.Connection = con
Try
con.Open()
cmd4.CommandText = "insert into dailyloco_status ([KTMB],[IRCON],[SMH],[total],[requirement],[short],[type],[datetime]) " _
& " values (@KTMB, @IRCON,@SMH,@total,@requirement,@short,@type,@datetime)"
Dim p1 As New SqlParameter("@KTMB", SqlDbType.Int)
Dim p2 As New SqlParameter("@IRCON", SqlDbType.Int)
Dim p3 As New SqlParameter("@SMH", SqlDbType.Int)
Dim p4 As New SqlParameter("@total", SqlDbType.Int)
Dim p5 As New SqlParameter("@requirement", SqlDbType.Int)
Dim p6 As New SqlParameter("@short", SqlDbType.Int)
Dim p7 As New SqlParameter("@type", SqlDbType.SmallInt)
Dim p8 As New SqlParameter("@datetime", SqlDbType.VarChar)


For i As Integer = 0 To 7 Step 1

If i = 0 Then

p1.Value = Integer.Parse(TextBox12.Text)
p2.Value = Integer.Parse(TextBox13.Text)
p3.Value = Integer.Parse(TextBox14.Text)
p4.Value = Integer.Parse(TextBox15.Text)
p5.Value = 0
p6.Value = 0
p7.Value = 1

ElseIf i = 1 Then

p1.Value = Int64.Parse(TextBox20.Text)
p2.Value = Int64.Parse(TextBox21.Text)
p3.Value = Int64.Parse(TextBox22.Text)
p4.Value = Int64.Parse(TextBox23.Text)
p5.Value = Int64.Parse(TextBox41.Text)
p6.Value = Int64.Parse(TextBox42.Text)
p7.Value = 2
totdaily_loco()
ElseIf i = 2 Then
p1.Value = Int64.Parse(TextBox24.Text)
p2.Value = Int64.Parse(TextBox25.Text)
p3.Value = Int64.Parse(TextBox26.Text)
p4.Value = Int64.Parse(TextBox27.Text)
p5.Value = Int64.Parse(TextBox43.Text)
p6.Value = Int64.Parse(TextBox76.Text)
p7.Value = 3
ElseIf i = 3 Then
p1.Value = Integer.Parse(TextBox28.Text)
p2.Value = Integer.Parse(TextBox29.Text)
p3.Value = Integer.Parse(TextBox30.Text)
p4.Value = Integer.Parse(TextBox31.Text)
p5.Value = Integer.Parse(TextBox77.Text)
p6.Value = Integer.Parse(TextBox78.Text)
p7.Value = 4
ElseIf i = 4 Then
p1.Value = Integer.Parse(TextBox32.Text)
p2.Value = Integer.Parse(TextBox33.Text)
p3.Value = Integer.Parse(TextBox34.Text)
p4.Value = Integer.Parse(TextBox35.Text)
p5.Value = Integer.Parse(TextBox79.Text)
p6.Value = Integer.Parse(TextBox80.Text)
p7.Value = 5
ElseIf i = 5 Then 'total
p1.Value = Integer.Parse(TextBox36.Text)
p2.Value = Integer.Parse(TextBox37.Text)
p3.Value = Integer.Parse(TextBox38.Text)
p4.Value = Integer.Parse(TextBox39.Text)
p5.Value = Integer.Parse(TextBox81.Text)
p6.Value = Integer.Parse(TextBox82.Text)
p7.Value = 6
ElseIf i = 6 Then
p1.Value = Integer.Parse(TextBox44.Text)
p2.Value = Integer.Parse(TextBox45.Text)
p3.Value = Integer.Parse(TextBox46.Text)
p4.Value = Integer.Parse(TextBox47.Text)
p5.Value = 0
p6.Value = 0
p7.Value = 6
End If
p8.Value = DateTime.Now
cmd4.Parameters.Add(p1)
cmd4.Parameters.Add(p2)
cmd4.Parameters.Add(p3)
cmd4.Parameters.Add(p4)
cmd4.Parameters.Add(p5)
cmd4.Parameters.Add(p6)
cmd4.Parameters.Add(p7)
cmd4.Parameters.Add(p8)
cmd4.ExecuteNonQuery()
cmd4.Parameters.Clear()
Next


Catch ex As Exception
MesgBox(ex.Message.ToString())

Finally
con.Close()

End Try
Posted

1 solution

Please see my comment to the question.

Here is how you can resolve the problem: don't use the designer; create all similar controls (text boxes) in code, it will allow you to use arrays or some collections of controls (such as lists).

—SA
 
Share this answer
 
Comments
musiw 10-Sep-13 3:04am    
thank you for reply.but i dont understand how to implement the way you suggest..

currently my form is like that.many textbox user will have to key in.they want it to be like that.because each textbox has its own category.just like a table and then fill in.

thanks.
Sergey Alexandrovich Kryukov 10-Sep-13 10:47am    
It's like
TextBox[] myTextBoxes = new TextBox[/* ... */]; // create an array
for (int index = 0, index < myTextBoxes.Length, ++index) {
myTextBoxes[index] = new TextBox();
// arrange it: assign location, add some text, etc., it probably should come with some labels...
ParentControl.Controls.Add(myTextBoxes[index]); // where ParentControl is some panel or form...
//...
}


Now you have some array you can use in loops.

—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900