Click here to Skip to main content
15,887,477 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The below code throws "Index was outside the bounds of the array" error
Please help me out.

C#
SqlParameter[] parameters = new SqlParameter[2]
{
new SqlParameter("@Param1",param1),
new SqlParameter("@Param2",param2)
};
if (condition)
{
 parameters[2] = new SqlParameter("@Param3",param3)
}



with Thanks,
Rajan.
Posted

Change this to
VB
parameters[1] = new SqlParameter("@Param3",param3)
 
Share this answer
 
You have to change your SqlParameter[] to a List<sqlparameter> and use the List<t>.Add(T obj) method...

Arrays are not resizable in CSharp. if you declare an array of length 2 then you can't assign anything at 3rd index (parameters[2]).
 
Share this answer
 
v2
Comments
NPSSR 24-Jul-13 7:00am    
I was declared as 'new SqlParameter[3]'. And it throws the same error.
Guirec 24-Jul-13 23:45pm    
Use a List... that is your best option here.
NPSSR 25-Jul-13 4:18am    
It was the rare requirement. Ok. Thanks for your suggestion.

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