2012-01-27 65 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 ..