in c sharp win-forms
i have added a combo box control to my form and added items accordingly into the combo box m trying to the item on select-index is assigned to a string which is passed as parameter to function declared in the following manner:
private void cmbPayment_SelectedIndexChanged(object sender, EventArgs e)
{
string pm = cmbLocation.SelectedItem.ToString();
payment(pm);
}
THE FUNCTION:
public void payment(string pym)
{
jd.PaymentMode = pym;
}
alt text http://img42.imageshack.us/img42/8691/adssd.png
Looks to me like "cmbLocation" should be "cmbPayment" ?
It looks like there is no item selected on cmbLocation. If there is no item selected, the SelectedItem property will be null, and you can't call toString on null.
Do you mean cmbLocation or cmbPayment? Because this happens in a cmbPayment event.
One sollution would be to check for null:
private void cmbPayment_SelectedIndexChanged(object sender, EventArgs e)
{
if(!cmbPayment.SelectedItem == null)
{
string pm = cmbLocation.SelectedItem.ToString();
payment(pm);
}
}
This is good practice anyway to prevent NullPointerExceptions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With