What am I doing wrong here?

  • Thread starter Thread starter nashak
  • Start date Start date
N

nashak

Dim Parameters As IDataParameter() = { _
New SqlParameter("@StudyID", StudyID)}

....Do some processing here

'point to nothing
Parameters = Nothing

Parameters = { New sqlparameter ("@StudyID", StudyID), _
New sqlparameter("@SubjectID", SubjectID)}

the above gives me error

What am I missing here?


Thanks
 
When you use the shortcut:

arrayType[] array = { 1, 2, 3 };

you are both instantiating and initializing. You cannot use this shortcut to
reset. A better (more explicit) way of handling this is:

//1. Create params

SqlParameter param1 = new SqlParameter("@param1", 0);
SqlParameter param2 = new SqlParameter("@param2", 0);
SqlParameter param3 = new SqlParameter("@param3", 0);

//2. Attach to array by initializing array

SqlParameter[] paramArray = new SqlParameter[2];
paramArray[0] = param1;
paramArray[1] = param2;

//3. Reset array when you need different values

paramArray = new SqlParameter[3];
paramArray[0] = param1;
paramArray[1] = param2;
paramArray[2] = param3;

I realize it is not VB.NET, but it should be easy to translate the concept.


---

Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************
 
As an addendum, it is also possible to add a third New to initialize not
only the individual elements but also the array object itself, which is
missing after the "Parameters = Nothing" statement.

An even easier solution would be to simple clear or remove all individual
elements of the array instead of setting the collection object to Nothing.
However, I don't know VB.NET myself, so I cannot tell you how to do this but
it should be pretty easy to find.

S. L.

Cowboy (Gregory A. Beamer) - MVP said:
When you use the shortcut:

arrayType[] array = { 1, 2, 3 };

you are both instantiating and initializing. You cannot use this shortcut
to
reset. A better (more explicit) way of handling this is:

//1. Create params

SqlParameter param1 = new SqlParameter("@param1", 0);
SqlParameter param2 = new SqlParameter("@param2", 0);
SqlParameter param3 = new SqlParameter("@param3", 0);

//2. Attach to array by initializing array

SqlParameter[] paramArray = new SqlParameter[2];
paramArray[0] = param1;
paramArray[1] = param2;

//3. Reset array when you need different values

paramArray = new SqlParameter[3];
paramArray[0] = param1;
paramArray[1] = param2;
paramArray[2] = param3;

I realize it is not VB.NET, but it should be easy to translate the
concept.


---

Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************

Dim Parameters As IDataParameter() = { _
New SqlParameter("@StudyID", StudyID)}

....Do some processing here

'point to nothing
Parameters = Nothing

Parameters = { New sqlparameter ("@StudyID", StudyID), _
New sqlparameter("@SubjectID", SubjectID)}

the above gives me error

What am I missing here?


Thanks
 
Dim arrParam(1) As SqlParameter
Dim intRowsAffected As Int32
arrParam(0) = New SqlParameter
arrParam(1) = New SqlParameter
 
Back
Top