Dynamic Arrays in ASP.NET?

T

tjonsek

I am converting an ASP page to asp.net. In this application, a user can
enter a list of numbers, separated by commas. The relationship involved
is 1 to M and I never know how many different numbers the user will
enter.
I am getting a 'specified cast not valid' error. While I'm not 100% it
is the array, This is the block of code that I have narrowed down to
causing the problem.
I've spent some time reading on the net about the differences in arrays
in vb.net and am wondering if the split function I am using isn't the
best route to go.

Please give any pointers about arrays or suggestions for what might
cause that specific error message.

In old ASP, my code looked like this:
dim woPrefix
dim aryWO
dim I
woPrefix = "ASHC"

aryWO = split(session("WO"),",",-1)

set rst1 = cmd1.Execute()
For I = 0 to UBound(aryWO)
if session("flgType") = "e" or Instr(1,session("WO"),"ASHC") then
sqlCmd = sqlCmd + "Insert into contractOutWO Values(" &
session("ID") & ",'" & aryWO(I) & "') "
else
sqlCmd = sqlCmd + "Insert into contractOutWO Values(" &
session("ID") & ",'" & woPrefix & aryWO(I) & "') "
end if
Next

This is what I have in my .NET app:
Dim woPrefix As String
Dim aryWO As Array
Dim I As Integer
woPrefix = "ASHC"

aryWO = Split(txtWO.Text, ",", -1)

SqlSelectCommand1.CommandType = CommandType.Text
SqlSelectCommand1.CommandText = "Delete from
contractOutWO where ID ='" & lblID.Text & "'"
SqlSelectCommand1.ExecuteNonQuery()
For I = 0 To UBound(aryWO)
If ddlCOType.SelectedValue = "E" Or InStr(1,
txtWO.Text, "ASHC") Then
SqlSelectCommand1.CommandText =
SqlSelectCommand1.CommandText + "Insert into contractOutWO Values('" &
lblID.Text & "','" & aryWO(I) & "') "
Else
SqlSelectCommand1 =
SqlSelectCommand1.CommandText + "Insert into contractOutWO Values('" &
lblID.Text & "','" & woPrefix & aryWO(I) & "') "
End If
If Len(SqlSelectCommand1.CommandText) > 0 Then
SqlSelectCommand1.ExecuteNonQuery()
End If
Next
 
C

Cor Ligthert [MVP]

Hi,

First of all if you are using VSNet with VBNet than set on Option Strict to
On. Probably because all still upgraders from VB6 to VBNet is this by
default of.


Dim woPrefix as String = "ASHC"
Dim aryWO as Array = Split(txtWO.Text, ",", -1)
SqlSelectCommand1.CommandType = CommandType.Text 'this is default
SqlSelectCommand1.CommandText = "Delete from contractOutWO where ID ='" &
lblID.Text & "'"
SqlSelectCommand1.ExecuteNonQuery()
For i as Integer = 0 To AryWO.length - 1
If ddlCOType.SelectedValue = "E" Or InStr(1, txtWO.Text,
"ASHC") Then
SqlSelectCommand1.CommandText =
SqlSelectCommand1.CommandText + "Insert into
contractOutWO Values('" &

Do not use the ambigous + to concatinate in VB Net. With Option Strict of it
can give you a lot of trouble, it can than be used in the late binding as an
real plus to add.

I cannot see it deeper, because of that long string. The change that this
plus is your error is however high.
If that is not the problem than reply here.

Have as well beside this a look at commandparameters. That this sample is
for windowforms does not matter in this case.

http://www.vb-tips.com/default.aspx?ID=886bba68-8a2f-4b99-8f66-7139b8970071

This is the most simple sample there are more about this on our website just
search for 'parameters'

Did you know by the way that there is a special VB Net language newsgroup.

microsoft.public.dotnet.languages.vb

I hope this helps so far,

Cor
 
T

tjonsek

Thanks. You've all given me a lot to look at. I am familiar with the VB
Net group. I posted here because I wasn't totally sure where the error
was coming from and thought maybe a more general group could provide
the answer. I appreciate your feedback. If this doesn't work, I'll
definitely post back.
Thank you.
 
T

tjonsek

For others who might have the same problem:

I had to change this:

dim woPrefix
dim aryWO
dim I
woPrefix = "ASHC"
aryWO = split(session("WO"),",",-1)

to this:

Dim woPrefix As String
Dim aryWO() As String
Dim I As Integer
woPrefix = "ASHC"
aryWO = txtWO.Text.Split(",")

I tried setting Option Strict to On, however, it gave me errors I
didn't understand. Mainly, I wanted to display the forms ID# once it
had been saved in the database. I was setting the text of a label to
display the output param of a previous stored procedure. It works fine
without option strict, but not with.
I have other apps that do the same thing and work well. So, because I'm
on a time crunch I thought I'd leave it as is and work on other things
more pressing.

Would you know why I got an error on the setting of label text?
It said 'Option strict disallows implicit conversions from
System.object to string'
 
C

Cor Ligthert [MVP]

Tjonsek,

It is very simple.

Option Strict Of acts like VB6. An object is translated at runtime to its
type accoording to the value that is used, this is very useful for all
programs that has to be converted from VB6 to VBNet.

However

VB Net with option Strict Of is comparable in speed with VB6
VB Net with option Strict On has the same speed as C#

But if you buy more expensive hardware you can of course compensate that.

I hope this helps,

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top