You must Sign In to post a response.
  • Category: ASP.NET

    In gridview need to multiply the amount one by one in runtime

    giving the value in runtime in gridview .

    my gridview data

    productname rate1 rate2 rate 3 rate4 total

    x 2 2.2 1 1 4.4

    if i give single value i got the result

    productname rate1 rate2 rate 3 rate4 total

    x 2 2.2 1 1 4.4
    x 1 2.2 1 1 2.2

    if i give the second time am getting errror

    my code

    //foreach (GridViewRow row in ddsales.Rows)
    // GridViewRow row = ddsales.Rows;
    for(int i=0;i<ddsales.Rows.Count;i++)
    {
    GridViewRow row = ddsales.Rows[i];
    {
    double mr, wt, totlrp, topr, prfet;
    mr = Convert.ToDouble((row.FindControl("lbllotno") as Label).Text);
    //wt = double.Parse(txtdefaultvalue.Text);
    wt = Convert.ToDouble((row.FindControl("txtdefaultvalue") as TextBox).Text);
    double pric = Convert.ToDouble(((TextBox)row.FindControl("txtamount")).Text);
    double psft = Convert.ToDouble(((TextBox)row.FindControl("txtsqft")).Text);
    double preat = Convert.ToDouble(((TextBox)row.FindControl("txtrate")).Text);
    totlrp = mr * wt;
    prfet = psft * preat;
    topr = totlrp * pric *prfet;


    (((TextBox)row.FindControl("txttoalamount")).Text) = Convert.ToString(topr);
    // break;
    }
    }


    rate1,rate2,rate3,rate4 all are given in runtime only
  • #764731
    Hi,

    I didn't get what exactly is the issue.

    You mentioned that "if i give single value i got the result" and "if i give the second time am getting error"???

    Second time it is failing always or is it failing only for the specific value?

    Did you try to debug the code? In which line it is breaking?
    Please provide more info.


    Regards,
    Asheej T K
    Microsoft MVP[ASP.NET/IIS]
    DotNetSpider MVM

  • #764733
    Hi selvi,

    rather than posting your unnecessary information, request you to post the error details and in which line you got error and in which scenario you got that error, request you to post those details to help you in better manner.

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #764744
    Can you share the error details?
    If you share the error details, It will be easy to identify the issue in the code.

    By Nathan
    Direction is important than speed

  • #764749
    Hi

    What is your Error? can you share snapshot here.

    I assume may be your error this

    ex:
    for(int i=0;i<ddsales.Rows.Count;i++)

    check your ddlsales count value for runtime process the loop how many rows return mostly

    looping collection we need use this

    Right format:
    =============
    for(int i=0;i<=ddsales.Rows.Count-1;i++)

    then check your controls name correct or not
    whether using findcontrol


    better share the snapshot for your problem.

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #764750
    Input string was not in a correct format.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.FormatException: Input string was not in a correct format.

    Source Error:


    Line 293: double pric = Convert.ToDouble(((TextBox)row.FindControl("txtamount")).Text);
    Line 294: double psft = Convert.ToDouble(((TextBox)row.FindControl("txtsqft")).Text);
    Line 295: double preat = Convert.ToDouble(((TextBox)row.FindControl("txtrate")).Text);
    Line 296: totlrp = mr * wt;
    Line 297: prfet = psft * preat;

    Source File: d:\Royal Enfield\Sales.aspx.cs Line: 295

    Stack Trace:


    [FormatException: Input string was not in a correct format.]
    System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) +7475735
    System.Number.ParseDouble(String value, NumberStyles options, NumberFormatInfo numfmt) +115
    System.Double.Parse(String s, NumberStyles style, NumberFormatInfo info) +192
    System.Convert.ToDouble(String value) +63
    Sales.txtrate_TextChanged(Object sender, EventArgs e) in d:\Royal Enfield\Sales.aspx.cs:295
    System.Web.UI.WebControls.TextBox.OnTextChanged(EventArgs e) +8835270
    System.Web.UI.WebControls.TextBox.RaisePostDataChangedEvent() +55
    System.Web.UI.WebControls.TextBox.System.Web.UI.IPostBackDataHandler.RaisePostDataChangedEvent() +10
    System.Web.UI.Page.RaiseChangedEvents() +165
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1485

    Version Information: Microsoft .NET Framework Version:2.0.50727.5485; ASP.NET Version:2.0.50727.5491

    if i give the value for the second row in runtime i got this error

  • #764751
    hi

    check your data numeric or character
    check them

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #764752
    1. Make sure are you passing the numerical value in the text boxes
    2. Validate the text box values before converting to double.

    you are using

    Convert.ToDouble(((TextBox)row.FindControl("txtamount")).Text);

    You can use the tryparse for conversion

    double pric = 0;
    string inputval = ((TextBox)row.FindControl("txtamount")).Text;
    double.TryParse(inputval , out pric);

    By Nathan
    Direction is important than speed

  • #764753
    hi

    check your data numeric or character
    check them

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #764757
    hi
    try this working good



    protected void Page_Load(object sender, EventArgs e)
    {
    if (!IsPostBack)
    {
    DataTable dt = new DataTable();
    dt.Columns.Add("productname");
    dt.Columns.Add("rate1");
    dt.Columns.Add("rate2");
    dt.Columns.Add("rate3");
    dt.Columns.Add("rate4");
    dt.Columns.Add("total");
    DataRow dr;
    dr=dt.NewRow();
    dr[0]="x";
    dr[1]=2;
    dr[2]=2.2;
    dr[3]=1;
    dr[4]=1;
    dr[5]=4.4;
    dt.Rows.Add(dr);

    dr=dt.NewRow();
    dr[0]="x";
    dr[1]=1;
    dr[2]=2.2;
    dr[3]=1;
    dr[4]=1;
    dr[5]=2.2;
    dt.Rows.Add(dr);


    ddsales.DataSource=dt;
    ddsales.DataBind();
    }

    }



    protected void bt1_Click(object sender, EventArgs e)
    {
    double mr=0, wt, totlrp, topr, prfet;
    for (int i = 0; i < ddsales.Rows.Count; i++)
    {
    GridViewRow row = ddsales.Rows[i];
    {
    //mr = Convert.ToDouble((row.FindControl("lbllotno") as Label).Text);
    //wt = double.Parse(txtdefaultvalue.Text);
    wt = Convert.ToDouble((row.FindControl("txtrate1") as TextBox).Text);
    double pric = Convert.ToDouble(((TextBox)row.FindControl("txtrate2")).Text);
    double psft = Convert.ToDouble(((TextBox)row.FindControl("txtrate3")).Text);
    double preat = Convert.ToDouble(((TextBox)row.FindControl("txtrate4")).Text);
    totlrp = mr * wt;
    prfet = psft * preat;
    topr = totlrp * pric * prfet;
    }
    }
    }


    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #764760
    Hi,

    Check this line


    double preat = Convert.ToDouble(((TextBox)row.FindControl("txtrate")).Text);


    what value have you passed in txtrate control, whether that is numeric or alphabet?

    As per error details "Input string is not in a correct format", when you try to convert one format to another format the converted part should be fine otherwise it will throw this type of errors, in your case if you are pass string value into textbox and you may try to convert that into double then obviously you will get this type of error, to avoid this do validations.

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #764895
    Hai Selvi,
    This is not the correct way to manipulate data during the loading of the gridview.
    You need to use the RowDataBound event handler and inside you can get the previous value and do the manipulation.
    This will be easy and you will not get any kind of error.
    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com


Sign In to post your comments