开发者

ComboBox1.SelectedItem.ToString() is not working

String cmbvalue = comboBox1.SelectedItem.ToString();
if (cmbvalue == "Income")
{
    curvalu = int.Parse(txtbalance.Text);
    finalvalu = curvalu + int.Parse(txtIncomeExpense.Text);
    MessageBox.Show(comboBox1.SelectedItem.ToString());
    SqlCommand sqlcomm = new SqlCommand("INSERT INTO IncomeGenerator (Income, Date, Balance, Description) VALUES ('" + txtIncomeExpense.Text + "', '" + Convert.ToDateTime(dateTimePicker1.Text) + "' , " + finalvalu +  " ,  '" + txtDescription.Text + "')", sqlCon);
    sqlcomm.ExecuteNonQuery();
    sqlCon.Close();
}
else if (cmbvalue == "Expenses")
{
    SqlCommand sqlcomm 开发者_如何学编程= new SqlCommand("INSERT INTO IncomeGenerator (Expense, Date, Description) VALUES ('" + txtIncomeExpense.Text + "', '" + Convert.ToDateTime(dateTimePicker1.Text) + "' , '" + txtDescription.Text + "')", sqlCon);
    sqlcomm.ExecuteNonQuery();
    sqlCon.Close();
}
else
{
    MessageBox.Show("Sorry Wrong Input Selected");
}  

All this is done inside a submit button. can someone help me y its not going to the first statement. even when i select the correct Income ComboBox item. meanwhile the ComboBox style is DropDownList.

Can someone guide me please?


Use this

string balance = cmbBalance.SelectedItem.Text;
switch(balance.tolower())
{
   case "income":
    //your code
    break;
   case "expenses":
    //your code
    break;
   default:
   break;
}


You can't do a MessageBox.Show in a server side component.

You'll have to do

Response.Write(comboBox1.SelectedItem.ToString());

to send it down to the client. Or, use your debugger to analyze the value.


Use:

if (String.Equals(value, "..", StringComparison.OrdinalIgnoreCase))
{
    // ...
}

Also use:

int balance;
if (Int32.TryParse(txtBalance.Text, out balance)
{
    // use balance variable
}
else
{
    throw new InvalidOperationException("Wrong input! So on..");
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜