Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
I want to display data in gridview based on check box selection from another grid view. The given below code getting an error. The error is Input string was not in a correct format . Help me to find a proper solution. Thank you.

C#
protected void btnAssign_Click(object sender, EventArgs e)
  {
  Dictionary<int, string> selectedEmployees = new Dictionary<int, string>();
  foreach (GridViewRow row in GridView1.Rows)
    {
      if (row.RowType == DataControlRowType.DataRow)
         {
          CheckBox chkRow = (row.Cells[2].FindControl("CheckBox1") as CheckBox);
          if (chkRow.Checked)
            {
            selectedEmployees.Add(int.Parse(row.Cells[0].Text), row.Cells[1].Text); // error
            }
         }
    }
    if (selectedEmployees.Any())
    {
       GridView2.DataSource = selectedEmployees;
    }
 }



ASPX:

ASP.NET
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="EmployeeID" HeaderText="Employee ID" ReadOnly="True" SortExpression="EmployeeID" />
<asp:BoundField DataField="FirstName" HeaderText="Employee Name" ReadOnly="True" SortExpression="FirstName" />


<asp:TemplateField HeaderText="Select" SortExpression="Select">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

<asp:GridView ID="GridView2" runat="server"> </asp:GridView>


DB:

HTML
[EmployeeID]        NVARCHAR (50)  NOT NULL,
[FirstName]         NVARCHAR (150) NULL,
Posted
Comments
Praveen Kumar Upadhyay 9-Dec-14 2:51am    
Try debugging the code and check what is the value of row.Cells[0].Text where you are getting exception, you will automatically come to know what's wrong with.

Error will occur if you don't have any value which not convertible for integer when calling int.Parse(row.Cells[0].Text)
if you have integer values for employee ID then change the employeeID column to int column in your database.
debug and check the value you get for row.Cells[0].Text which causing the error and then decide how to change the application to avoid or correct those data. for example you may have data like "1212dg", "dd233" due to wrong input data, so you need to do a validation on the forms where you collect those data to enter only the numbers.
 
Share this answer
 
v3
1.The error is generated by the int.Parse() call that is trying to covert to integer an invalid value (maybe is empty string or other string but must be an integer value like "1001");

2.You should debug that line of code and check the value from row.Cells[0], and if is not OK, you should also check the Page_Load method where you bind your grid with data, and be shore that the init is done only once, and not for each postback!

For details see my next answer: System.FormatException: Input string was not in a correct format. | Server Error in '/' Application[^]
 
Share this answer
 

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