Click here to Skip to main content
15,567,415 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dear Experts,

Please help,
i am unable to update the balance through my c# code in to my SQL 2005 database table.

Following is the code i am using, please let me know where am i going wrong;
my code is correct till the label text is getting updated. However, when i am trying to use this label value in to the query for updating the table, it is giving error.



cmd = new SqlCommand("insert into tb_accounts (LEDGER, TDATE, NARRATION, EXPENSES, INCOME, BALANCE) values(@LEDGER, @TDATE, @NARRATION, @EXPENSES, @INCOME, @BALANCE)", cn);

        cmd.Parameters.AddWithValue("@LEDGER", dd_income.Text);
        cmd.Parameters.AddWithValue("@TDATE", txt_transdate.Value);
        cmd.Parameters.AddWithValue("@NARRATION", txt_narration.Text);
        cmd.Parameters.AddWithValue("@EXPENSES", "0.00");
        cmd.Parameters.AddWithValue("@INCOME", txt_income.Text);
        cmd.Parameters.AddWithValue("@BALANCE", "0.00");

        int n = cmd.ExecuteNonQuery();
        if (n > 0)
            cmd = new SqlCommand("SELECT SUM(INCOME)- SUM(EXPENSES) FROM TB_ACCOUNTS", cn);
            decimal t = (decimal)cmd.ExecuteScalar();
            lbl_clbal.Text = "Current Balance = [" + t.ToString() + "]";
            cmd = new SqlCommand("update tb_accounts set balance=@t where accountid = (select max(Accountid) from tb_accounts))", cn);
            cmd.Parameters.AddWithValue("@t", lbl_clbal.Text);

[Edit] SHOUTING removed [/edit]
Updated 8-Sep-13 13:30pm
Richard MacCutchan 8-Sep-13 13:02pm    
What is the error? Please provide complete details of the problem.
OriginalGriff 8-Sep-13 14:31pm    
DON'T SHOUT! Using all upper case is considered shouting on the internet, and rude.
phil.o 8-Sep-13 18:35pm    
You forgot to describe the error. What error message? Which line in your code triggers an error?
Could it be that the "balance" column in "tb_accounts" table is a number, and you are trying to affect a string value to it?

1 solution

A guess:

Your problem lies in line
cmd.Parameters.AddWithValue("@t", lbl_clbal.Text);
The solution depends on the datatype defined for "balance" column. You may have to parse the text to a compatible type.
Something like:
int balance;
int.TryParse(lbl_clbal.Text, out balance));
cmd.Parameters.AddWithValue("@t", balance);

If the value in the TextBox is not a valid integer, then balance will be zero.

[EDIT] Changed from double to int as per OP informations [ /EDIT ]
Share this answer
Raj from Mumbai 9-Sep-13 5:51am    
thanx for your valuable reply Phil.
value that is been passed through the text box is 10000.00 while the data type for balance is integer (18,2)
i tried as u suggested hereinabove but unable to still fix the issue.
i even tried with parsing decimal but failed to resolve.


phil.o 9-Sep-13 6:48am    
So you have to cast it as an integer in the code.
Please see my updated solution. And if it has been useful, thanks to vote and mark your question as answered :)

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