Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
using System.Data.SqlClient;
using System.Configuration;

public partial class _Default : System.Web.UI.Page
{
    SqlConnection con;
    int m = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString);
        if (!Page.IsPostBack)
        {
            BindGridview();
        }
    }

     void BindGridview()
    {
        
        string sqlquery = "select eid,ename,salary from employee12";
        SqlDataAdapter da = new SqlDataAdapter(sqlquery, con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds.Tables["Table"];
        GridView1.DataBind();
    }


    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label salary = (Label)e.Row.FindControl("lblsalary");
            //int addsalary = 10 + int.Parse(salary.Text);
            //salary.Text = addsalary.ToString();
            m = m + int.Parse(salary.Text);


        }
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            Label lbltotalprice = (Label)e.Row.FindControl("salary");

            lbltotalprice.Text = m.ToString();
        }

    }

What I have tried:

how to solve? this type of error
Posted
Updated 22-Aug-16 21:37pm
Comments
CPallini 23-Aug-16 3:28am    
You should give us more info about the error (e.g. location of the offending line).
Member 12599631 23-Aug-16 3:32am    
m = m + int.Parse(salary.Text);

The value the user entered wasn't a valid integer - so check it and report a problem instead of continuing.
Use int.TryParse instead:
C#
int sal;
if (!int.TryParse(salary.Text, out sal))
    {
    // Report problem to user
    ...
    return;
    }
m += sal;
 
Share this answer
 
Comments
Member 12599631 23-Aug-16 3:37am    
Thanks
OriginalGriff 23-Aug-16 3:59am    
You're welcome!
CPallini 23-Aug-16 3:39am    
5.
Reason for the error : you are trying to parse a string to int which is not in an interger format.
C#
m = m + int.Parse(salary.Text); // error

Int.TryParse[^] for casting string to integer
C#
int temp;
if (int.TryParse(salary.Text.Trim(), out temp))
    m = m + temp;

However, you will have to figure out why the Salary.Text is not in an integer format and fix that
 
Share this answer
 
Comments
CPallini 23-Aug-16 3:39am    
5.
Karthik_Mahalingam 23-Aug-16 3:40am    
Thanks CPallini
Member 12599631 23-Aug-16 3:43am    
thanks
Karthik_Mahalingam 23-Aug-16 3:47am    
welcome

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