Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello all,
i want show Excel column Sum in C#. i use this code

but got error...
The best overloaded method match for 'int.Parse(string)' has some invalid argument


please help...

What I have tried:

private void TodaysIncomeExpense()
        {
            string strFilename;
            strFilename = lblSelectedDatabase.Text;
            Microsoft.Office.Interop.Excel.Application xlApp;
            Microsoft.Office.Interop.Excel.Workbook xlWorkbook;
            Microsoft.Office.Interop.Excel.Worksheet xlWorksheet;
            Microsoft.Office.Interop.Excel.Range xlRange;

            xlApp = new Microsoft.Office.Interop.Excel.Application();
            xlWorkbook = xlApp.Workbooks.Open(strFilename);
            xlWorksheet = xlWorkbook.Worksheets["Sheet1"];
            xlRange = xlWorksheet.UsedRange;
            try
            {
                rowCount = xlRange.Rows.Count;
                for (index=2; index < rowCount;)
                {
                    int val = Int32.Parse(xlWorksheet.Cells[index, 5]);
                    val += val;
                    index++;
                    lblTotalIncome.Text = val.ToString();
                }
            }
            catch
            {
                //MessageBox.Show("GOT SOME ERROR");
            }
        }
Posted
Updated 15-Jul-20 2:30am
v3
Comments
Richard MacCutchan 15-Jul-20 8:06am    
"but got error"
You need to explain what error. We cannot guess, or see your screen. Also, do not code try/catch blocks and then ignore any caught exceptions.
manish kr. chaturvedi 15-Jul-20 8:23am    
Error is:
The best overloaded method match for 'int.Parse(string)' has some invalid argument.

Without the error it's hard to say, but:

lblTotalIncome.Text

looks wrong - for a standard label control it should be:

lblTotalIncome.Caption

Check the properties of the label control you are using, make sure you're writing to the right one.
 
Share this answer
 
v3
Comments
manish kr. chaturvedi 15-Jul-20 8:26am    
its a C# Window Application....
I got error in Below line
int val = Int32.Parse(xlWorksheet.Cells[index, 5]);
Quote:
but got error...

When you ask for hep, it is a good idea to give details on the error.
Try/Catch structure exist to hide errors, it is a bad idea to use them while debugging your code. Error system normally tells you the reason and position of error, it is a great help.

You need to know that an excel cell is an object, not a string or a number.
Try:
C#
int val = xlWorksheet.Cells[index, 5].Value;
 
Share this answer
 
Comments
manish kr. chaturvedi 15-Jul-20 8:39am    
thanks but i now i have a different error:
cannot implicitly convert type 'decimal' to 'int'. An explicit conversion exits (are you missing a cost?)
Patrice T 15-Jul-20 9:15am    
Use Improve question to update your question.
So that everyone can pay attention to this information.
And update your code.

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