Link to home
Start Free TrialLog in
Avatar of JRockFL
JRockFLFlag for United States of America

asked on

Sql Parameters

How does this code look? I am getting "index was outside the bounds of the array"


// this should be a 1 correct, since there are 2?
SqlParameter[] param = new SqlParameter[1];
param[0] = new SqlParameter("@ProductID", 1);
param[1] = new SqlParameter("@SupplierID", 1);

ds = objDALL.ExecuteDataSet(sql, param);


        public DataSet ExecuteDataSet(string sql, SqlParameter[] parameters)
        {
            SqlDataAdapter da = new SqlDataAdapter(sql, ConnectionString);
            DataSet ds = new DataSet();

            try
            {
             // this be parameters.Length -1 correct?
                for (int i = 0; i < parameters.Length -1; i++)
                {
                    da.SelectCommand.Parameters.Add(parameters[i]);
                }

                da.Fill(ds);

                return ds;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
ASKER CERTIFIED SOLUTION
Avatar of nepali
nepali

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JRockFL

ASKER

Thanks for the reply, does work.
Can you explain why it should be that?
Avatar of jagadeeshbandlamudi
jagadeeshbandlamudi

SqlParameter param = new SqlParameter("@ProductID", Varchar(20));
            param.Value = 1;
           da.SelectCommand.Parameters.Add(param);
SqlParameter param = new SqlParameter("@SupplierID", Varchar(20));
            param.Value = 1;
           da.SelectCommand.Parameters.Add(param);

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JRockFL

ASKER

mr_fnord1,

Thank you that makes sense now.