Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
HI, my problem i have 3 coloumns to make sum and display in total column but the code below have mistake with whenever u put null value it will give me
Input string was not in a correct format.
its only allow all field to be full with value so plz plz
plz some 1 help i want to allow null value and thx

What I have tried:

C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string val1 = e.Row.Cells[3].Text; //Gets the value in Column 1
            string val2 = e.Row.Cells[4].Text; //Gets the value in Column 2
            string val3 = e.Row.Cells[5].Text; //Gets the value in Column 3
            Label lblTotal = (Label)e.Row.Cells[6].FindControl("Label1"); //

            if (string.IsNullOrEmpty(val1))
            { val1 = "0"; }
            if (string.IsNullOrEmpty(val2))
            { val2 = "0"; }
            if (string.IsNullOrEmpty(val3) )
            { val3 = "0"; }


            Int64 sum = Int64.Parse(val1) + Int64.Parse (val2) + Int64.Parse (val3);
            lblTotal.Text += sum.ToString();

        }
Posted
Updated 23-Mar-17 15:30pm
Comments
[no name] 23-Mar-17 19:49pm    
This code will NOT produce a "Input string was not in a correct format." for a null value. Like I already told you, you need to step through your code with the debugger and find the string that is causing the exception. We can't do that for you.
Member 13044689 24-Mar-17 5:32am    
y i did it thank u

use Int32.TryParse Method [^], it will take care of empty,null and non numeric formats

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string val1 = e.Row.Cells[3].Text; //Gets the value in Column 1
            string val2 = e.Row.Cells[4].Text; //Gets the value in Column 2
            string val3 = e.Row.Cells[5].Text; //Gets the value in Column 3
            Label lblTotal = (Label)e.Row.Cells[6].FindControl("Label1"); //
          
             int _val1, _val2, _val3;
             int.TryParse(val1, out _val1); 
             int.TryParse(val2, out _val2);
             int.TryParse(val3, out _val3);

            int sum = _val1 + _val2 + _val3;

            lblTotal.Text += sum.ToString();

        }
    }
 
Share this answer
 
Comments
Member 13044689 24-Mar-17 5:29am    
thx sir its work
Karthik_Mahalingam 24-Mar-17 5:30am    
welcome
Quote:
Input string was not in a correct format.

this message tells you that there is something wrong or unexpected in the data you try to handle. We can't check that for you, use the debugger and inspect variables when you get the error message.

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
v2
Comments
Member 13044689 24-Mar-17 5:31am    
thx sir , but we didnt born just knowing everything we learn and learn just make it polite or don't comment aim new at this without asking here and there will never learn
thx

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