1

I want to multiply the text value and combo box value and display it in a orderprice textbox the data types that I use in sql are 'float' for all of these

private void cb_oqty_SelectedIndexChanged(object sender, EventArgs e)
{
    int orderprice;
    int proprice=Convert.ToInt16(txt_oprice.Text);
    int quantity = Convert.ToInt16(cb_oqty.SelectedItem);

    orderprice = proprice * quantity;
    txt_orderprice.Text = Convert.ToString(orderprice);
    txt_orderprice.Update();                                
}
5
  • You need to put breakpoints and debug individual line. Only that way you can find out which statement is throwing the exception. Commented Mar 8, 2016 at 10:36
  • I think when combo fill all items that particular time it throws exception. it comes before selecting combo value or after selecting? Commented Mar 8, 2016 at 10:38
  • i am getting error on this line int proprice=Convert.ToInt16(txt_oprice.Text); Commented Mar 8, 2016 at 10:43
  • @AlyGoreja it comes after selecting combo value or before? where you give data to combo box?? i.e in form load? Commented Mar 8, 2016 at 10:50
  • when i select value from combo box after it and it does not show the value tgrows an exception Commented Mar 8, 2016 at 10:50

2 Answers 2

1

Use int.TryParse to see if the text can be converted first. That error seems to point to text not being able to be converted to int.

int orderprice = 0;
int proprice = 0;
int quantity = 0;

if (int.TryParse(txt_oprice.Text, out proprice) && int.TryParse(cb_oqty.SelectedValue.ToString(), out quantity))
{
    // It was assigned.
    orderprice = proprice * quantity;
}
else
{
    //Error
}

Also the cb_oqty.SelectedItem will not convert to an int as it is an Object. you need to use SelectedValue.

Sign up to request clarification or add additional context in comments.

7 Comments

it is giving error at int.tryparse(cb_oqty.selectedvalue, out quantity)
int.TryParse(cb_oqty.SelectedValue.ToString(), out quantity). It needs to be SelectedValue.ToString()
error object rerference not set to be an instance of an object at tryparse line
and after i select value from combo box
Put a break point on the if statement. View in the debugger watch what the combo box values have.
|
0

If your SQL datattype is float, your C# datatype should be float as well:

    private void cb_oqty_SelectedIndexChanged(object sender, EventArgs e)
    {
        var proprice = Convert.ToSingle(txt_oprice.Text);
        var quantity = Convert.ToSingle(cb_oqty.SelectedItem);

        var orderprice = proprice * quantity;
        txt_orderprice.Text = orderprice.ToString();

        // this does not do what you think it does. You can remove this line.
        txt_orderprice.Update();
    }

Better would be using the TryParse method.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.