I have about four or more textboxes on my screen but I set false to visible.
As the result (count) of my database table, I want to show the textboxes.
Here is my code.
//my datatable list
Dim dl As List(Of String) = dt.Rows.Cast(Of DataRow).Select(Function(dr) dr(0).ToString).ToList
For i As Integer = 1 To dl.Count
Me.Controls("txtSrc" & i.ToString).Visible = True
Next
Then, I have this kind of error .
InvalidCastException: String The conversion from "lblSrc1" to type'Integer' is invalid.
How can I fix that error?
Note; I am using VS 2019<<ASP.NET webform (using VB.NET)>>
2
Answers
Firstly, I am sorry that I forgot to say I have a Master page. So I tried that way and it did work for me. Thanks.
Ok, unlike say VBA/VB6 – maybe vb.net desktop?
You get getting a type conversion error because
Wants a number (index value) into the controls collection.
So, you can’t use a string or "variable" to reference the controls the same way you can say in VBA/Access/VB6
So, your example code would become this:
Also, there is little (no reason) to convert the data table into a list to get the row count, since a data table as above shows .Rows.Count is the same value
So
BOTH above will result in the SAME row count value – so you can dump that messy first line of code to convert the dt to a list. Not required.
(so, with above both RowCount1 and RowCount2 would have same value and same result)
So this should do the trick: