【问题标题】:Null Error on WinForm ComboboxWinForm 组合框上的空错误
【发布时间】:2012-02-20 06:12:47
【问题描述】:

我在我的项目中使用 WinForms 和 MySQL。

我的表结构是……

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

  1. 城市
  2. 状态
  3. 国家

当我选择城市时,必须选择州和国家组合框。

当我选择州时,必须选择国家组合框。

当我选择国家时,不需要选择州和国家组合框。

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

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();

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

【问题讨论】:

  • 调试您的代码,找到引发异常的确切行。 ...SelectedItem 可以为空。
  • cmbo_country.SelectedItem.ToString();给出错误。如何解决这个问题?。
  • 所以,然后 ToString() 抛出错误,因为组合框为空或没有选定的项目。你应该处理这种情况,例如 - 写'if'条件。
  • 我可以去 cmbobox.Text Propriety 吗?
  • 您可以在 MSDN 中找到此信息,或者尝试测试一下。但我建议您初始化您的组合框 - 填充值并选择默认项(例如 - SelectedIndex = 0)。它将帮助您避免这个问题。

标签: c# mysql winforms combobox


【解决方案1】:

试试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.
}

【讨论】:

  • 不要忘记案件之间的“休息”! ;)
  • 一个操作需要三个Query吗?
  • 只有一个查询会被执行,它会基于条件
【解决方案2】:

尝试在combobox选中的项目代码中试一下,..

 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{}

现在此代码满足您的要求。好吗?

【讨论】:

  • -1 类似于“尝试尝试”的措辞,但这只是在隐藏问题。使用调试器找出问题所在。
  • 但它解决了错误,他会很快得到解决方案。对吗?
  • no 如果没有选择国家,它返回的第一行本身,异常
【解决方案3】:

您需要更改插入语句,以便在 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());
}

【讨论】:

    【解决方案4】:

    我有一个简单的方法,

     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();
    

    在这个你不想要任何尝试和捕捉,..

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多