ASP.NET - total+running

Asked By Jahir on 18-Feb-12 07:43 AM
Err:

Error:System.FormatException: Input string was not in a correct format. at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) at System.Number.ParseSingle(String value, NumberStyles options, NumberFormatInfo numfmt) at System.Single.Parse(String s, NumberStyles style, NumberFormatInfo info) at System.Single.Parse(String s) at _Default.Button1_Click(Object sender, EventArgs e) in d:\running\Default.aspx.cs:line 46

cmd = new SqlCommand("select sum(amount) AS total from amount", con);

dr = cmd.ExecuteReader();

dr.Read();


if (dr.HasRows)

{

total = float.Parse(dr[0].ToString());//46 err

float f = total + float.Parse(TextBox1.Text);

dr.Close();

cmd = new SqlCommand("insert into amount(amount,total)values('" + TextBox1.Text + "','" + f + "')", con);

cmd.ExecuteNonQuery();

TextBox1.Text =

"";

Response.Write("<script>alert('Successfull');</script>");

}


else

{

dr.Close();

}

Sreekumar P replied to Jahir on 18-Feb-12 07:52 AM
Hi,

Try this code

      cmd = new SqlCommand("select SUM(ISNULL(amount,0)) AS total from amount", con);
      dr = cmd.ExecuteReader();
      dr.Read();
      if (dr.HasRows)
      {
        if (dr[0] != DBNull.Value)
        {
          total = Convert.ToSingle(dr[0]);
        }
        else
          total = 0;
        float f = total + float.Parse(TextBox1.Text);
        dr.Close();
        cmd = new SqlCommand("insert into amount(amount,total)values('" + TextBox1.Text + "','" + f + "')", con);
        cmd.ExecuteNonQuery();
        TextBox1.Text ="";
        Response.Write("<script>alert('Successfull');</script>");
      }
      else
      {
        dr.Close();
      }
     
Jahir replied to Sreekumar P on 18-Feb-12 08:04 AM
your given coding is perfectly executing thanks for your coding: but i want to update means what i do here ? if i update amount means total i mean running total also want to correctly updated  ?

cmd = new SqlCommand("select SUM(ISNULL(amount,0)) AS total from amount", con);

dr = cmd.ExecuteReader();

dr.Read();


if (dr.HasRows)

{


if (dr[0] != DBNull.Value)

{

amount =

Convert.ToSingle(dr[0]);

}

else

amount = 0;

float f = amount + float.Parse(TextBox1.Text);

dr.Close();

cmd =

new SqlCommand("insert into amount(amount,total)values('" + TextBox1.Text + "','" + f + "')", con);

cmd.ExecuteNonQuery();

TextBox1.Text = "";

Response.Write(

"<script>alert('Successfull');</script>");

}

else

{

dr.Close();

}

Sreekumar P replied to Jahir on 18-Feb-12 08:26 AM
Hi,

First of all I  don't understand wht is ur "Running Total".

If u want to update means, den u should have a primary key or some other reference key


cmd = new SqlCommand("select SUM(ISNULL(amount,0)) AS total from amount", con);
 
dr = cmd.ExecuteReader();
 
dr.Read();
 
  
 
if (dr.HasRows)
 
{
 
  
if (dr[0] != DBNull.Value)
 
{
 
amount =
 
Convert.ToSingle(dr[0]);
 
}
 
else
 
amount = 0;
float f = amount + float.Parse(TextBox1.Text);
 
dr.Close();
 
cmd =
 
new SqlCommand("update amount set amount='" + TextBox1.Text + "',total='" + f + "' where somekey=value", con);
 
cmd.ExecuteNonQuery();
TextBox1.Text = "";
 
Response.Write(
 
"<script>alert('Successfull');</script>");
 
}
 
else
 
{
 
dr.Close();
 
}



by,
Sreekumar
Request : Please mark helpful if it seems to be that
Jahir replied to Sreekumar P on 18-Feb-12 08:31 AM
This is my coding for running total:

cmd = new SqlCommand("select SUM(ISNULL(amount,0)) AS total from amt", con);

dr = cmd.ExecuteReader();

dr.Read();


if (dr.HasRows)

{


if (dr[0] != DBNull.Value)

{

amount =

Convert.ToSingle(dr[0]);

}

else

amount = 0;

float f = amount + float.Parse(TextBox1.Text);

dr.Close();

cmd =

new SqlCommand("insert into amt(amount,total)values('" + TextBox1.Text + "','" + f + "')", con);

cmd.ExecuteNonQuery();

TextBox1.Text = "";

Response.Write(

"<script>alert('Successfull');</script>");

bindgrid();

}

else

{

dr.Close();

}



this output is:


ID amount total EDIT/UPDATE DELETE
1 100 100 Edit Delete
6 100 200 Edit Delete
7 300 500 Edit Delete

my updating coding is:

else if (e.CommandName == "upd")

{


LinkButton lkbupd = (LinkButton)e.CommandSource as LinkButton;

GridViewRow row = lkbupd.NamingContainer as GridViewRow;

Label lbid = (Label)row.FindControl("lblid");

TextBox txamount = (TextBox)row.FindControl("txtamount");

cmd =

new SqlCommand("update amt set amount='" + txamount.Text + "' where id ='" + lbid.Text + "'", con);

cmd.ExecuteNonQuery();

GridView1.EditIndex = -1;

Response.Write("Updated Successfully");

bindgrid();

}


but here its updating only amount, there is no change in total, but i want to change total as like given update amount.

pls change in my codings and give solution for me ?

i want output like this:



ID amount total EDIT/UPDATE DELETE
1 200 200 Edit Delete
6 100 300 Edit Delete
7 300 800 Edit Delete
if i update in 1st row amount 100 as 200 means it want to change like this in output ?
give codings for me ? to update like this ...
Jahir replied to Sreekumar P on 18-Feb-12 08:38 AM
some key i dont under stand ? how to declare key there
this is my sql query

what i want to add here for key ?

create table amt(id bigint primary key identity, amount bigint,total bigint)

Chintan Vaghela replied to Jahir on 18-Feb-12 09:20 AM


Hello,

try to change your update logic  code as following way

Decimal amount = 0;
foreach (GridViewRow tt in GridView1.Rows)
{
    if (tt.RowType == DataControlRowType.DataRow)
    {       
     Label lbid = (Label)tt.FindControl("lblid");
 
     TextBox txamount = (TextBox)tt.FindControl("txamount");
     amount = amount + Convert.ToDecimal(txtamount.Text);
     new SqlCommand("update amt set amount='" + txamount.Text + "', total='" + amount.ToString() + "' where id ='" + lbid.Text + "'", con);
    cmd.ExecuteNonQuery();
 
   }
 

 
 
}

hope this helpful