Error: Index is outside the bounds of the array

W

weird0

I have no idea why this error this is coming and how can i fix it?
Can anyone please help me with it..............?

public static class BillManager
{
private static Object ExecuteSP(Object[] argArray)
{
SqlCommand sqlcmd = new SqlCommand();
SqlConnection sqlconnection1 = new SqlConnection();
sqlcmd.Connection = sqlconnection1;
Object returnValue;
int paramCount = (int)argArray[1]; // the number of
parameters
sqlcmd.CommandText = (string)argArray[2]; // the whole
query
sqlcmd.CommandType = CommandType.StoredProcedure;

for (int j = 0, i = 3; j < paramCount; i += 4, j++)
{
SqlParameter myPrm = new
SqlParameter((string)argArray,

(SqlDbType)argArray[i + 1],

(int)argArray[i + 2]); // Line of Error
myPrm.Value = argArray[i + 3];
sqlcmd.Parameters.Add(myPrm);
}

sqlconnection1.Open();

if ((int)argArray[0] == 0)
returnValue = sqlcmd.ExecuteNonQuery(); //
returns the number of rows affected
if ((int)argArray[0] == 1)
returnValue = sqlcmd.ExecuteScalar(); //
returns a single value
else
returnValue =
sqlcmd.ExecuteReader(CommandBehavior.CloseConnection); //returns
multiple values

return returnValue;

}


public static void Insert_Utility_Bill(string CompanyName,
string CustomerId, string Alias, string UserName)
{
object[] prms = new object[11];

prms[0] = 0;
prms[1] = 4;
prms[2] = "INSERT_UTILITY_BILL";
prms[3] = "@COMPANYNAME";
prms[4] = SqlDbType.VarChar;
prms[5] = 50;
prms[6] = CompanyName;
prms[7] = "@CUSTOMERID";
prms[8] = SqlDbType.NChar;
prms[9] = 10;
prms[10] = CustomerId.ToCharArray();
prms[7] = "@alias";
prms[8] = SqlDbType.VarChar;
prms[9] = 50;
prms[10] = Alias;
prms[7] = "@username";
prms[8] = SqlDbType.VarChar;
prms[9] = 50;
prms[10] = UserName;

ExecuteSP(prms);

}
}
 
N

Nicholas Paldino [.NET/C# MVP]

Well, it's rather obvious. You have an array that you giving an index
to which is outside the bounds of the array.

What happens when you debug this? The debugger will most certainly tell
you what the problem is.
 
G

Guest

Hi,
Ckech the content of the Array object you are sending. This error means that
Your index value grater then the array you are sneding

Regards,
Husam Al-a'araj
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

weird0 said:
I have no idea why this error this is coming and how can i fix it?
Can anyone please help me with it..............?
--8<-- snip

public static void Insert_Utility_Bill(string CompanyName,
string CustomerId, string Alias, string UserName)
{
object[] prms = new object[11];

prms[0] = 0;
prms[1] = 4;
prms[2] = "INSERT_UTILITY_BILL";
prms[3] = "@COMPANYNAME";
prms[4] = SqlDbType.VarChar;
prms[5] = 50;
prms[6] = CompanyName;
prms[7] = "@CUSTOMERID";
prms[8] = SqlDbType.NChar;
prms[9] = 10;
prms[10] = CustomerId.ToCharArray();

Hm... What number might be following 10? ;)
prms[7] = "@ALIAS";
prms[8] = SqlDbType.VarChar;
prms[9] = 50;
prms[10] = Alias;
prms[7] = "@USERNAME";
prms[8] = SqlDbType.VarChar;
prms[9] = 50;
prms[10] = UserName;

ExecuteSP(prms);

}
}
 
R

rossum

I have no idea why this error this is coming and how can i fix it?
Can anyone please help me with it..............?
[snip]

(int)argArray[i + 2]); // Line of Error
Step through your program with the Debugger. Set a watch on the
values of i and on argArray. Check the value of i+2 against the size
of argArray every time through your loop.

rossum
 

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