Nubkadiya
Nubkadiya

Reputation: 3475

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?

Upvotes: 0

Views: 4290

Answers (3)

user240141
user240141

Reputation:

Use this

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

Upvotes: 1

abatishchev
abatishchev

Reputation: 100248

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

Upvotes: 1

Flipster
Flipster

Reputation: 4401

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.

Upvotes: 1

Related Questions