tags:

views:

122

answers:

2

alt text

How do I autodecrement the number of students allowed everytime you assign a section to each student added?

I have the code, but it has an error.

    private void btnAssign_Click(object sender, EventArgs e)
    {
        ////for auto increment
        ds = DBConn.getStudentDetails("sp_Retrieve_Student_Section");
        int cnt = ds.Tables[0].Rows.Count;
        string lastrec = ds.Tables[0].Rows[cnt+1][1].ToString();
        int newpcode = int.Parse(lastrec) - 1;
        txtAllowed.Text= newpcode.ToString();



    }
+3  A: 

At first glance it looks like this line:

string lastrec = ds.Tables[0].Rows[cnt+1][1].ToString();

cnt+1 is out of the bounds of the collection, and an exception probably told you this. You are probably looking for cnt-1.

blu
everytime i run the system.... it appears higlighted line codeint newpcode = int.Parse(lastrec) - 1;
malou
*What* appears?
Sarah Brown
it appears highlighted code in the system telling that i have an error in my codes
malou
Pretty sure that's what she was asking. What are the errors you are receiving. Telling us where you receive the error but not what the error is doesn't help a whole lot.
Cory Charlton
There is no row at position 6.
malou
@malou: Then I suggest you re-read this answer as blu both explains why that error would occur **AND** how to solve it. Not only that but he did it through mind reading so +1 to him for sure.
Cory Charlton
i already change my cnt+1 to cnt-1
malou
@malou: Then your error message has changed. If you expect further help you should edit your question to show the changed code and describe the new error message you are receiving.
Cory Charlton
just edidted it private void btnAssign_Click(object sender, EventArgs e) { ////for auto increment ds = DBConn.getStudentDetails("sp_Retrieve_Student_Section"); int cnt = ds.Tables[0].Rows.Count; string lastrec = ds.Tables[0].Rows[cnt-1][1].ToString(); int newpcode = 0; if (Int32.TryParse(lastrec, out newpcode)) { newpcode--; } txtAllowed.Text= newpcode.ToString(); }
malou
there is no errror appear...its just that when i select the combobox for their section the number of students 40 becomes 0.
malou
"Rows[cnt+1][1]" - Check the value that is returned by this column [1] (the collection is zero based). It sounds like your new error is lastrec is not a number in a string.
blu
i just change Rows[cnt+1][1] to [cnt-1][1]and it appears if the student is 40 then assign their serction it becomes 0 already
malou
malou
A: 

I'd probably write

 int newpcode = int.Parse(lastrec) - 1;

as

int newpcode = 0;
if(Int32.TryParse(lastrec, out newpcode))
{
   newpcode--;
}

That way, if it can successfully parse the lastrec, it will decrement. If it can't successfully parse, your newpcode will be 0, but will not throw an error.

taylonr
when i rewrite my codeinto your code and try it run... when i assign a section in the combo box.. the number of student becamse 0
malou
@malou: That's because you don't understand the basics of what you are trying to do. The error message you describe in blu's answer point to that being the problem and not what taylonr is describing. Sorry to be so blunt.
Cory Charlton
already change my errors@ Cory: sorry for vague questions and answers..well thats the thing it is higlighted anyway i just gave the error message saying there is no row in line 6
malou
Didn't see that error message in the comments on Blu's answer... if the error is "there is no row in line 6" then that's definitely point to Blu's answer. If you're always getting set to 0, then that means lastrec is unable to be parsed into an int.
taylonr