rai nalasa
rai nalasa

Reputation: 859

Loading Sum on label

I am trying to load the sum of txtQuantity.Text and lblPice.Text on lblPriceToBe..I'm loading my lblprice with this code.

private void GetData()
{
    SqlConnection connection = new SqlConnection("Data Source = localhost\\SQLEXPRESS;Initial Catalog = MejOnlineManagementDB00;Integrated Security=True;");
    connection.Open();
    SqlCommand sqlCmd = new SqlCommand(@"SELECT price,productType
                                        FROM Products3
                                        WHERE productName='" + DropDownList1.SelectedItem.Value.ToString() + "'", connection);
    SqlDataReader rdr = sqlCmd.ExecuteReader();
    if (rdr.HasRows)
    {
        while (rdr.Read())
        {
            lblPrice.Text = rdr.GetValue(0).ToString(); //  if price is  string use GetString(0))
            lblProdType.Text = rdr.GetValue(1).ToString();
        }
    }
    connection.Close();
    //DropDownList End of Area
}

After I got the price on mysql database I multiply it with txtQuantity

protected void btnCalculate_Click(object sender, EventArgs e)
{
    double Quantity = Convert.ToDouble(txtQuantity.Text);
    double Price = Convert.ToDouble(lblPrice.Text);
    double sum;
    sum = Quantity * Price;
    //Output
    sum = Convert.ToDouble(lblPriceToBe.Text);
}

and then I get this error

An exception of type 'System.FormatException' occurred in mscorlib.dll but was not handled in user code

Additional information: Input string was not in a correct format.

that point on this code:

sum = Convert.ToDouble(lblPriceToBe.Text);

Upvotes: 2

Views: 854

Answers (2)

Cru Dy
Cru Dy

Reputation: 11

Run the Query On your Sql/mysql Server And check that return a value , if it does try

declare those variables on the public of the class

public double Quantity;
public double Price;
public double Sum;

Then give their value by the query :

 Quantity = Convert.ToDouble(rdr.GetValue("columns name"));
 Price = Convert.ToDouble(rdr.GetValue("columns name"));
 Sum = Quantity * Price ;

Then use them in your textboxes

Upvotes: 0

sujith karivelil
sujith karivelil

Reputation: 29006

That means the value in the lblPriceToBe.Text is not convertible to double. it may be empty or any other values which we cannot convert to double. In such particular scenarios double.TryParse will help you to determine whether the input is convertible or not, it will also give you the converted result if the conversion is successful( else it will be 0.0); SO what you want to do is:

protected void btnCalculate_Click(object sender, EventArgs e)
{
    double Quantity,Price,sum;
    bool canProcess=true;
    if(!double.TryParse(txtQuantity.Text,out Quantity)
    {
      // conversion failed
      lblPriceToBe.Text="Invalid quantity"
      canProcess=false;
    }
    if(!double.TryParse(lblPriceToBe.Text,out Price)
    {
      // conversion failed
      lblPriceToBe.Text="Invalid Price"
      canProcess=false;
    }
    if(canProcess)
    {
    sum = Quantity * Price;
    //Output
    lblPriceToBe.Text=sum.ToString();
    }

}

Upvotes: 1

Related Questions