2012-01-27 60 views
1

我在我的项目中使用WinForms和MySQL。WinForm Combobox上的空错误

我的表结构...

enter image description here

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

  1. 国家
  2. 国家

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

enter image description here

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

enter image description here

当我选择的国家,就没有必要选择国家和国家组合框。

enter image description here

我试图用这个代码中插入这样的数据:

MySqlConnection connection = new MySqlConnection("server=192.168.1.100;User Id=mcubic;[email protected]$;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(); 

当选择了所有组合框,此代码工作正常。但是当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;[email protected]$;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(); 

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

+0

调试你的代码,找到引发异常的确切行。 ... SelectedItem可以为null。 – Devart 2012-01-27 07:48:58

+0

cmbo_country.SelectedItem.ToString();给出错误。如何解决这个问题? – Sagotharan 2012-01-27 07:52:06

+0

因此,然后ToString()抛出错误,因为组合框是空的或没有选定的项目。你应该处理这种情况,例如 - 写'如果'的情况。 – Devart 2012-01-27 07:58:22

回答

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. 
} 
+0

不要忘记'案件之间的休息! ;) – 2012-01-27 08:02:29

+0

哦,谢谢!我更新了声明。 – 2012-01-27 08:05:53

+0

一个操作需要三个查询吗? – Sagotharan 2012-01-27 08:08:43

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

现在这个代码满足您的要求。好?

+2

-1像'尝试尝试'的措辞,但这只是隐藏了问题。使用调试器找出问题所在。 – 2012-01-27 08:04:30

+0

但它解决了错误,他将得到解决方案太快。这是对的吗? – RAGAVAN 2012-01-27 08:13:25

+0

否如果国家没有选择第一行本身它会返回,例外 – Sagotharan 2012-01-27 08:29:27

0

你需要让你不插入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()); 
} 
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;[email protected]$;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(); 

在此,你不希望任何try和catch ..