Click here to Skip to main content
15,890,557 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi,

I am working on this program for work where a user scans in a serial # and and they enter in a part # to be replace and it populates a grid. For ex, The grid would look like:

[0].DESCRIPTION [1].REFURB PART # [2].QTY [3]. COMPLETED [4].REFURB RATE

[0]. Cover
[1].CE-4014495
[2.]1
[3].(Check
Would
Appear
Here)
The Refurb Rate Column is the column I'm currently working on.Every time I try to debug the program, it gives me the Index Out of Range Exception when trying to add the refurb rate to the grid. I have some coding experience, but not in object arrays and adding values to a grid. Is their any individual or individuals that can help me. Thank you.

Justin
Here is some of my code to look at:

//This is where I call the methods

int refRate = GetRefurbRate();
grdRefurb.Enabled = true;

ADDRefurbRate();

}
}
//Get the Refurb Rate for the day
private int GetRefurbRate()
{
string sql = "";
//bool ret = true;
int Refurb_Rate = 0;

//string error_msg = "";

//grdRefurbChoices.Rows.Clear();
sql = "SELECT COUNT(*) " +
"FROM " + schema + ".repair_part r " +
"WHERE r.repair_ord = '" + txtRO.Text + "' ";

while (true)
{
string error_msg = "";

if (!myDb.RunSql(sql, true))
{
error_msg = "DBError for getting Refurb_Rate";
break;
}
int totalRefurb = 5;// the 5 and 10 are ex values that I entered
int totalUnits = 10;//because the refurb rate was throwing an exception of 0 first.

while (myDb.dbRdr.HasRows)
{
if (!myDb.dbRdr.Read())
break;

try
{
Refurb_Rate = (totalRefurb / totalUnits * 100);
}
catch (Exception e)
{
Console.WriteLine(e);
}

if (myDb.dbRdr.Read())
{
Refurb_Rate = Convert.ToInt32(myDb.dbRdr.GetValue(4));
}
}

break;
}

myDb.dbRdr.Close();
return Refurb_Rate;
}
// add refurb rate to the grid
//I commented out all of the database code b/c I assumed I didn't need them.
private bool ADDRefurbRate()
{

//string sql = "";
bool ret = true;
int Refurb_Rate = 0;
Int32 selectedRowCount = grdRefurbChoices.Rows.GetRowCount(DataGridViewElementStates.Selected);

string error_msg = "";

// grdRefurbChoices.Rows.Clear();
//sql = "SELECT COUNT(.repair_ord, .ref_desig,.part_nbr_inst,.part_nbr_rmvd) " +
// "FROM " + schema + ".repair_part " +
// "WHERE repair_ord = '" + txtRO.Text + "' ";

while (true)
{

//if (!myDb.RunSql(sql, true))
//{
// error_msg = "DBError for getting Refurb_Rate";
// break;
//}

//int totalRefurb = 0;
//int totalUnits = 0;

//while (myDb.dbRdr.HasRows)
{
//if (!myDb.dbRdr.Read())
// break;

//Refurb_Rate = (totalRefurb / totalUnits * 100);

Refurb_Rate++;

//object[] values = new object[myDb.dbRdr.FieldCount];
object[] grd_values = new object[4];

// Add refurb rate information on the lower-right grid
//string part_nbr = myDb.dbRdr.GetValue(4).ToString();
//Refurb_Rate = Convert.ToInt32(myDb.dbRdr.GetValue(4));

if (part_completed && Refurb_Rate <= 0)
{
MessageBox.Show("Refurb Rate, " + Refurb_Rate + ", is zero.",
"Get Refurb Rate",
MessageBoxButtons.OK, MessageBoxIcon.Asterisk);

ret = false;
break;
}

grd_values[4] = Refurb_Rate;
grdRefurbChoices.Rows.Add(grd_values);// this is where the error comes in.
grdRefurbChoices.Rows[0].Selected = true;

ret = true;

if (!ret) break;

int found_index = 0;

for (int i = 0; i < grdRefurb.Rows.Count; i++)
{
grdRefurb.Rows.Add(grd_values);
grdRefurb.Visible = true;
found_index = i;

break;

//return found_index;

//return found_index;
}

//myDb.dbRdr.Close();

if (error_msg != String.Empty)
{
MessageBox.Show(error_msg, "Get Refurb Rate",
MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}

break;

}

}
return ret;
}


Let me know if you need the whole program to further help.Thank you.

[added code block]
Posted
Updated 6-Apr-11 16:58pm
v3
Comments
Sergey Alexandrovich Kryukov 6-Apr-11 23:09pm    
Please, line of the code throwing "out of range exception". Add it as a comment to your code.
--SA
Sergey Alexandrovich Kryukov 6-Apr-11 23:16pm    
OK, I think I see it. It is at the comment "// this is where the error comes in.", just there?
--SA
Sergey Alexandrovich Kryukov 6-Apr-11 23:18pm    
Where is the declaration of grdRefurbChoices?
--SA

Just to start with: you should not allow yourself immediate constants like 4 (especially 4), 5, 10, etc. Those are a permanent source of bugs. (Imagine you change 4 to 5 in one place, forgot to change in another; not this is not an assumption, something happening all the time.) Parametrize everything. Later on, get rid of all hard-coded strings; they belong in resources or data/configuration files.

Now, I just found your bug. See:

C#
object[] grd_values = new object[4];

//...

//the exception is thrown here, because the object grd_values[4] does not exist
//(our of range); the valid range at the moment is 0..3:
grd_values[4] = Refurb_Rate;


No, you did not comment the line where exception is thrown correctly. It is thrown one line before, see above.
Way too easy. I told you, immediate constants is evil. You could use System.Collection.Generic.List or something, use it's Count property… whatever.

—SA
 
Share this answer
 
v3
Comments
Venkatesh Mookkan 6-Apr-11 23:48pm    
Good catch. 5!
Sergey Alexandrovich Kryukov 6-Apr-11 23:55pm    
Thank you very much, Venkatesh. (I tried hard this time... :-)
--SA
Justiin1265 7-Apr-11 0:33am    
I thought I was using the count property to add the refurb rate to the grid? Can you please further explain? Thanks.
Sergey Alexandrovich Kryukov 7-Apr-11 0:48am    
Justin, I don't quite understand what to explain.

You used Count property for something, maybe correctly, so what? You wanted some help in finding the problem, I pin-pointed it for you, now it's your turn. My point is: all indexes and other values should be calculated, not hard-coded. There a are many techniques you could use. I mentioned Count just to conduct and idea of the loop 0 to Count - 1, or "foreach" traversing some collection.

For now, do you understand where the bug is? This one is quite trivial. Now fix it.
And please formally accept my Answer, as it does answer your Question.

Good luck,
--SA
The main reason could be grdRefurbChoices has either less number of columns than the array object or columns could be more than the array object's Length.


SA's is correct.


Suggestion:

The best way to do is using

C#
DataRow row = grdRefurbChoices.NewRow();
//assign the values to row using the column name or the appropriate column index.
//
grdRefurbChoices.Rows.Add(row);



Mark it as answer if it is really helpful.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 6-Apr-11 23:41pm    
No! The exception it thrown one line before. Did you see my Answer? Since when Add throws exceptions? Maybe you trusted OP, that's the problem...
--SA
Venkatesh Mookkan 6-Apr-11 23:45pm    
Sorry, I missed the index part of the assignment. Thanks. Will update the answer accordingly.

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