Click here to Skip to main content
15,894,405 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi
 public void ssaa(GridView grv1,GridView grv2)
    {
     
        if (RdoYes.Checked == true)
        {
            ArrayList da1 = new ArrayList();
            ArrayList da2 = new ArrayList();
            int a1 = 0, a2 = 0;

            foreach (GridViewRow row1 in grv1.Rows)
            {
                

                a1 = System.Convert.ToInt32(row1.Cells[2].Text);
                a2 = System.Convert.ToInt32(row1.Cells[3].Text);
                da1.Add(a1);
                da2.Add(a2);
                   foreach ( int ir in da1 )
                {
                        
                        foreach (GridViewRow row2 in grv2.Rows)
                        {
                        for (int id = 0; id < ir; id++)
                        {
                           response.write(row1.cells[0].text);
                          
                        }
                    }
                }

            }
        }
      
}

i am calling this function in button click event i am getting the error Input string was not in a correct format.
Posted
Updated 17-Feb-11 21:35pm
v5

If your string is not null and you are getting this error, then it means your string contains not only numbers. For example converting "10 days" will throw an exception. You should only keep the numbers before converting the string.
 
Share this answer
 
Comments
Dima Popov 18-Feb-11 4:03am    
You're right.
gowdhami 18-Feb-11 4:20am    
i give only 4 but the output is not getting for me..could u explain more
Dima Popov 18-Feb-11 4:40am    
You can prepare the string for parsing by checking each char in it with Char.IsDigit. And do not forget negative numbers' preceding minus and floating point (it's value is stored as string in System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator, but in fact it's only '.' or ',').
Olivier Levrey 18-Feb-11 4:24am    
If you want to convert a string to an int, then all characters inside the string should be numbers (except first one: it can be '-'). So before converting the string, do something like: txt = txt.Split(new char[]{' '})[0];
This line of code will keep only the first part of your string: the part before the space.
a1 = System.Convert.ToInt32(row1.Cells[2].Text);                <br />
a2 = System.Convert.ToInt32(row1.Cells[3].Text);


You are trying to convert text to integers.
Make sure these values are not null.
 
Share this answer
 
v3
Comments
[no name] 18-Feb-11 0:47am    
Good Call.
Abhinav S 18-Feb-11 0:48am    
Thanks.
arindamrudra 18-Feb-11 0:54am    
Great answer. null checking is required and if you have doubt that all the value may not be integer type, then please use int Num;
bool isNum = int.TryParse(row1.Cells[3].Text.Trim(), out Num);
if (isNum)
{}
gowdhami 18-Feb-11 2:18am    
i tried in button click event also but it was showing the same error.and also i checked that values are not null
Pravin Patil, Mumbai 18-Feb-11 3:53am    
and more than that if you want to store null value into the int, make the variable int?.
public void ssaa(GridView grv1,GridView grv2)
    {
     
        if (RdoYes.Checked == true)
        {
            ArrayList da1 = new ArrayList();
            ArrayList da2 = new ArrayList();
            int a1 = 0, a2 = 0;

            foreach (GridViewRow row1 in grv1.Rows)
            {
                
if(!String.IsNullOrEmpty(System.Convert.ToInt32(row1.Cells[2].Text))
{
                a1 = System.Convert.ToInt32(row1.Cells[2].Text);
}
 
 if(!String.IsNullOrEmpty(System.Convert.ToInt32(row1.Cells[3].Text))
{

                a2 = System.Convert.ToInt32(row1.Cells[3].Text);
}
                da1.Add(a1);
                da2.Add(a2);
                   foreach ( int ir in da1 )
                {
                        
                        foreach (GridViewRow row2 in grv2.Rows)
                        {
                        for (int id = 0; id < ir; id++)
                        {
                           response.write(row1.cells[0].text);
                          
                        }
                    }
                }

            }
        }
      
}
 
Share this answer
 
v2

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