By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,231 Members | 1,613 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,231 IT Pros & Developers. It's quick & easy.

"Index was outside the bounds of the array" on Add

P: n/a
kim
Scenario (I'm a newbie): I have a datagrid with countries
listed and 5 parameters in each row. I want to add a row
to this datagrid via an Event Handler. Very basic stuff.
This method then call a Business method, which calls a
Data method which calls an SP in MS SQL.

My code compiles well, but the page throws "Index was
outside the bounds of the array". I really can't figure
why, take a look maybe you pros see can read between the
lines:

----- .apsx page:

protected void AddNew_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
//convert to Int
int milobs =
Convert.ToInt32(NewContribMilobs.Text);
int contingent =
Convert.ToInt32(NewContribContingent.Text);

Business.ContribCountry
contribCountry = new Business.ContribCountry();
//SiteIdentity currUser =
(SiteIdentity)Context.User.Identity;

// add the new record
contribCountry.Create
(NewContribCountry.Text.Trim(), NewContribPays.Text.Trim
(), milobs, contingent, currUser.UserID).Visible = true;

ShowAddNewControls(false);
BindGrid();
}
}

---- business class:
// create a new record
public int Create(string country, string
pays, int milobs, int contingent, int userID)
{
Data.Military military = new
Data.Military(settings.ConnectionString);
countryID = military.Add(country,
pays, milobs, contingent, userID);
return countryID;
}

---- data class:
// add a military
public int Add(string country, string
pays, int milobs, int contingent, int userID)
{
int numAffected;

// create the parameters
SqlParameter[] parameters = {

new SqlParameter
("@Country", SqlDbType.VarChar, 25),

new SqlParameter("@Pays",
SqlDbType.VarChar, 25),

new SqlParameter
("@Milobs", SqlDbType.Int, 4),

new SqlParameter
("@Contingent", SqlDbType.Int, 4),

new SqlParameter
("@UserID", SqlDbType.Int, 4)

};

// set the values
parameters[0].Value = country.Trim
();
parameters[1].Value = pays.Trim();
parameters[2].Value = milobs;
parameters[3].Value = contingent;
parameters[4].Value = userID;
parameters[5].Direction =
ParameterDirection.Output;

RunProcedure
("sp_Contributions_Military_Insert", parameters, out
numAffected);

return (int)parameters[5].Value;
}

---- end

Thanks in advance for any help.

Jul 19 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
It would be very helpful if you told us which line of code throws the error.

"kim" <ki********@yahoo.com> wrote in message
news:01****************************@phx.gbl...
Scenario (I'm a newbie): I have a datagrid with countries
listed and 5 parameters in each row. I want to add a row
to this datagrid via an Event Handler. Very basic stuff.
This method then call a Business method, which calls a
Data method which calls an SP in MS SQL.

My code compiles well, but the page throws "Index was
outside the bounds of the array". I really can't figure
why, take a look maybe you pros see can read between the
lines:

----- .apsx page:

protected void AddNew_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
//convert to Int
int milobs =
Convert.ToInt32(NewContribMilobs.Text);
int contingent =
Convert.ToInt32(NewContribContingent.Text);

Business.ContribCountry
contribCountry = new Business.ContribCountry();
//SiteIdentity currUser =
(SiteIdentity)Context.User.Identity;

// add the new record
contribCountry.Create
(NewContribCountry.Text.Trim(), NewContribPays.Text.Trim
(), milobs, contingent, currUser.UserID).Visible = true;

ShowAddNewControls(false);
BindGrid();
}
}

---- business class:
// create a new record
public int Create(string country, string
pays, int milobs, int contingent, int userID)
{
Data.Military military = new
Data.Military(settings.ConnectionString);
countryID = military.Add(country,
pays, milobs, contingent, userID);
return countryID;
}

---- data class:
// add a military
public int Add(string country, string
pays, int milobs, int contingent, int userID)
{
int numAffected;

// create the parameters
SqlParameter[] parameters = {

new SqlParameter
("@Country", SqlDbType.VarChar, 25),

new SqlParameter("@Pays",
SqlDbType.VarChar, 25),

new SqlParameter
("@Milobs", SqlDbType.Int, 4),

new SqlParameter
("@Contingent", SqlDbType.Int, 4),

new SqlParameter
("@UserID", SqlDbType.Int, 4)

};

// set the values
parameters[0].Value = country.Trim
();
parameters[1].Value = pays.Trim();
parameters[2].Value = milobs;
parameters[3].Value = contingent;
parameters[4].Value = userID;
parameters[5].Direction =
ParameterDirection.Output;

RunProcedure
("sp_Contributions_Military_Insert", parameters, out
numAffected);

return (int)parameters[5].Value;
}

---- end

Thanks in advance for any help.

Jul 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.