Tutorial :autodecrement in number using c# [closed]



Question:

alt text http://www.imagechicken.com/uploads/1265914403006619100.jpg

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();            }  


Solution:1

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.


Solution:2

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.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »