locked
Input string was not a correct format. RRS feed

  • Question

  • Please solve my problem of ASP.net with C#. When i get the value from Data Reader the error is occurred "Input string was not a correct format" Thanks in advance.

    string obal ;

                double obalss = 0.00;

                sconnection c = new sconnection();

                string cus_id = Session["cusid"].ToString();

                DateTime maxdate = DateTime.Parse(fromdt.Text, new System.Globalization.CultureInfo("en-US"));

     

        string query_sl = "select sum(amount) as amount from sale where cusid = " + cus_id + " and invdate < "+fromdt.Text; 

                SqlDataReader dr = c.reader(query_sl);

                if (dr.Read())

                {

                    obal = dr[0].ToString();

                    if(obal != null)

                    {

                        obalss = double.Parse(obal);

                    }

                    else

                    {

                        obalss = 0.00;        

                    }

                }

                else

                {

                    obalss = 0.00;

                }

                    dr.Close();

                    dr.Dispose();

     

                string obalcr = "Db";

                ReportDocument rpt = new ReportDocument();

                rpt.Load(Server.MapPath("Reports") + "/" + "ledger.rpt");

                rpt.SetDatabaseLogon("sa", "123", @"vxdatabase", "VECTORX");

                rpt.SetParameterValue("fdt", Convert.ToDateTime(fromdt.Text));

                rpt.SetParameterValue("tdt", Convert.ToDateTime(todt.Text));

                rpt.SetParameterValue("cid", Session["cusid"].ToString());

                rpt.SetParameterValue("obl", obalss);

                rpt.SetParameterValue("oblcd", obalcr.ToString());

                CrystalReportViewer1.ReportSource = rpt;

     

    Monday, August 8, 2011 5:40 PM

Answers

  • 1. I think the value of obal is not valid number. put a break point at that number and see whether obal is really returning a number.

    2. Try double.Parse ( obal.ToString(), System.globalization.NumberStyles.AllowDecimalPoint )

    3. I suggest using Convert.ToDouble(obal) instead of Double.Parse(obal) because it is coming from database and can be null.


    Please mark this post as answer if it solved your problem. Happy Programming!

    • Marked as answer by Min Zhu Thursday, August 11, 2011 6:04 AM
    Monday, August 8, 2011 6:33 PM
  • Actually, you should use the following code to get the value for obalss:
    ...
    obalss = (dr.IsDBNull(0) ? default(double) : dr.GetDouble(0));
    ...

    Make sure the field in your database is actually a double.  Also, I recommend that you use parameters instead of concatenating a SQL string.  Especially, in an ASP.NET application like yours.  Concatenated SQL strings equal SQL Injection attacks!
    • Proposed as answer by RohitArora Tuesday, August 9, 2011 6:24 AM
    • Marked as answer by Min Zhu Thursday, August 11, 2011 6:04 AM
    Monday, August 8, 2011 7:04 PM

All replies

  • Can you change the query as :

    select ISNULL(sum(amount), 0) as amount from sale where cusid = 
    
    

     


    • Edited by sambeet Monday, August 8, 2011 6:29 PM junk html characters
    Monday, August 8, 2011 6:29 PM
  • 1. I think the value of obal is not valid number. put a break point at that number and see whether obal is really returning a number.

    2. Try double.Parse ( obal.ToString(), System.globalization.NumberStyles.AllowDecimalPoint )

    3. I suggest using Convert.ToDouble(obal) instead of Double.Parse(obal) because it is coming from database and can be null.


    Please mark this post as answer if it solved your problem. Happy Programming!

    • Marked as answer by Min Zhu Thursday, August 11, 2011 6:04 AM
    Monday, August 8, 2011 6:33 PM
  • Actually, you should use the following code to get the value for obalss:
    ...
    obalss = (dr.IsDBNull(0) ? default(double) : dr.GetDouble(0));
    ...

    Make sure the field in your database is actually a double.  Also, I recommend that you use parameters instead of concatenating a SQL string.  Especially, in an ASP.NET application like yours.  Concatenated SQL strings equal SQL Injection attacks!
    • Proposed as answer by RohitArora Tuesday, August 9, 2011 6:24 AM
    • Marked as answer by Min Zhu Thursday, August 11, 2011 6:04 AM
    Monday, August 8, 2011 7:04 PM