WinForm Combobox上的空错误

时间:2012-01-27 07:28:43

标签: c# mysql winforms combobox

我在项目中使用WinForms和MySQL。

我的表结构是......

enter image description here

在我的项目中,我有三个组合框和一个TextBox。 “选项”ComboBox包含三个值:

  1. 国家
  2. 国家
  3. 当我选择City,State和Country ComboBoxes时必须选择。

    enter image description here

    当我选择State时,必须选择Country ComboBoxes。

    enter image description here

    当我选择国家/地区时,无需选择州和国家组合框。

    enter image description here

    我尝试使用以下代码插入此数据:

    MySqlConnection connection = new MySqlConnection("server=192.168.1.100;User Id=mcubic;password=mcs@2011$;database=mcs;Persist Security Info=True");
    MySqlCommand command = new MySqlCommand("Insert into test (name1,option1,state,country) VALUES ('" + textBox1.Text + "','" + cmbo_Options.SelectedItem.ToString() + "','" + cmbo_state.SelectedItem.ToString() + "','" + cmbo_country.SelectedItem.ToString() + "')", connection);
    connection.Open();
    command.ExecuteNonQuery();
    connection.Close();
    

    选择所有ComboBox后,此代码正常工作。但是当未选择State ComboBox时,它会抛出NullReferenceException。

      

    对象引用未设置为对象的实例。空错误。

    所以我更新了我的代码:

    string country = cmbo_country.SelectedItem.ToString();
    string state = cmbo_state.SelectedItem.ToString();
    MySqlConnection connection = new MySqlConnection("server=192.168.1.100;User Id=mcubic;password=mcs@2011$;database=mcs;Persist Security Info=True");
    MySqlCommand command = new MySqlCommand("Insert into test (name1,option1,state,country) VALUES ('" + textBox1.Text + "','" + cmbo_Options.SelectedItem.ToString() + "','" + state + "','" + country + "')", connection);
    connection.Open();
    command.ExecuteNonQuery();
    connection.Close();
    

    我无法逃避这个错误。我的代码应该如何用于这个项目?

4 个答案:

答案 0 :(得分:2)

我有一个简单的方法,

 string country ="";
if(cmbo_country.SelectedItem != null)
country = cmbo_country.SelectedItem.ToString();
    string state = "";
if(cmbo_state.SelectedItem !=null)
state = cmbo_state.SelectedItem.ToString();
    MySqlConnection connection = new MySqlConnection("server=192.168.1.100;User Id=mcubic;password=mcs@2011$;database=mcs;Persist Security Info=True");
    MySqlCommand command = new MySqlCommand("Insert into test (name1,option1,state,country) VALUES ('" + textBox1.Text + "','" + cmbo_Options.SelectedItem.ToString() + "','" + state + "','" + country + "')", connection);
    connection.Open();
    command.ExecuteNonQuery();
    connection.Close();

在此你不想要任何尝试和Catch,..

答案 1 :(得分:0)

尝试cmbo_country.Text

最简单的解决方案是在代码中使用SWITCH

这是您的应用程序的简单Psuedocode

MySqlCommand command;
switch (cmbo_country.Text)
{
    case "City":
    {
       command = new MySqlCommand("Insert into test (name1,option1,state,country) VALUES ('','','','')");
       break;
    }
    case "State":
    {
       command = new MySqlCommand("Insert into test (name1,option1,country) VALUES ('','','')");
       break;
    }
    case "Country":
    {
       command = new MySqlCommand("Insert into test (name1,option1) VALUES ('','')");
       break;
    }
    default:
       // prompt the user to select an option.
}

答案 2 :(得分:0)

您需要更改insert语句,以便在空的情况下不插入state列:

一种简单的方法是只添加一个if语句:

string sql;
if (cmbo_State.SelectedItem == null)
{
    sql = string.Format("Insert into test (name1,option1,country) 
        Values ('{0}','{1}','{2}')", textBox1.Text, 
        cmbo_Options.SelectedItem.ToString(), cmbo_country.SelectedItem.ToString());
}
else
{
    sql = string.Format("Insert into test (name1,option1,state,country) 
        Values ('{0}','{1}','{2}','{3}')", textBox1.Text, 
        cmbo_Options.SelectedItem.ToString(), cmbo_state.SelectedItem.ToString()
        cmbo_country.SelectedItem.ToString());
}

答案 3 :(得分:-2)

尝试尝试使用组合框选择的项目代码,..

 try
    {
        string country = cmbo_country.SelectedItem.ToString();
                string state = cmbo_state.SelectedItem.ToString();

    }
    catch
    {

    }

抱歉,

你可以试试这个

try
{
    string country = cmbo_country.SelectedItem.ToString();           
}
catch{}

try
{       
    string state = cmbo_state.SelectedItem.ToString();

}
catch{}

现在这段代码符合您的要求。 OK?