1

I have a simple loop I am trying to use to capture data from a form and put it in to a stored procedure that updates a table. It seems to me like it should do what it's supposed to do, but I'm getting a conversion error and I'm not really sure why. I don't even know if it does in fact work since I'm getting an error that doesn't make a lot of sense to me. Here's what I have currently:

    Dim rowcounter As Integer = 1

    While rowcounter < 15
        Dim NameHolder As String = "LineID" + rowcounter.ToString
        CMD.CommandTimeout = 60
        CMD.Connection = CONN
        CMD.CommandType = CommandType.StoredProcedure
        CMD.CommandText = "UpdateArchived"
        CMD.Parameters.Clear()
        CMD.Parameters.AddWithValue("@ID", Me.Controls(NameHolder).ToString)
        DA.SelectCommand = CMD
        DS.Clear()
        DA.Fill(DS)
    End While

The error is with the Me.Controls(NameHolder).ToString line. It says the following: Conversion from string "LineID1" to type 'Integer' is not valid.

What is being converted to an integer here? Does Me.Control return the control as an integer?

RobLife
  • 95
  • 1
  • 9
  • what type of control is `LineID1`? – Ňɏssa Pøngjǣrdenlarp Sep 27 '13 at 21:23
  • I tried that but it gives me the error - "Text is not a member of System.Web.UI.Control." LineID1 - LineID14 are textboxes. – RobLife Sep 27 '13 at 21:24
  • My guess - the @ID parameter of the stored procedure is defined as an integer. – rheitzman Sep 27 '13 at 21:35
  • I tried a Ctype before in a slightly different way and it didn't work. I tried this and it produced the same error. Even when I just try to put it in a msgbox to display. – RobLife Sep 27 '13 at 21:36
  • possible duplicate of [loop over all textboxes in a form, including those inside a groupbox](http://stackoverflow.com/questions/4673950/loop-over-all-textboxes-in-a-form-including-those-inside-a-groupbox) – MikeSmithDev Sep 27 '13 at 21:37
  • I also thought that it was the parameter, but I changed it in the stored procedure and it even throws the error when I don't assign it to that paramter. – RobLife Sep 27 '13 at 21:37
  • 3
    Guys... Me.Controls() expects an integer, hence the error. Look at the link above to see how to loop through and find controls. You'll need a recursive function. Another useful post, though in C# http://stackoverflow.com/questions/13336839/asp-net-findcontrol-and-gridview-returning-null/13337066#13337066 – MikeSmithDev Sep 27 '13 at 21:42
  • I must have misinterpreted the use of Me.Controls in the examples I looked at then. – RobLife Sep 27 '13 at 21:46
  • Unrelated - when will that loop end? – EdSF Sep 28 '13 at 01:50
  • Never, because I somehow lost the counter in all those edits. – RobLife Sep 30 '13 at 13:29

1 Answers1

1

Try using & not +

Both are concat operators but + will try and add an integer to an integer, hence the error. & only deals with strings.

So..

Dim NameHolder As String = "LineID" & rowcounter.ToString

HTH

  • 1
    also, I assume "option strict" is off in your case, if so it would wise to turn this on and this exception would be flagged before building the code. –  Sep 27 '13 at 21:16
  • 1
    I tried it but the error was the same. The strange thing is that if I output NameHolder in a msgbox it displays LineID1, but for some reason when I try to access that control it gives me that same error. Like somehow "LineID1" is an integer. – RobLife Sep 27 '13 at 21:21