ComboBox1.SelectedItem.ToString()不起作用

时间:2011-01-16 20:07:57

标签: c# combobox selecteditem

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");
}  

所有这一切都在提交按钮内完成。有人可以帮助我,不会发表第一个声明。即使我选择了正确的Income ComboBox项目。同时ComboBox样式是DropDownList。

有人可以指导我吗?

3 个答案:

答案 0 :(得分:1)

您无法在服务器端组件中执行MessageBox.Show

你必须要做

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

将其发送给客户端。或者,使用调试器来分析值。

答案 1 :(得分:1)

使用:

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

也可以使用:

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

答案 2 :(得分:1)

使用此

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