[英]Null Error on WinForm Combobox
我在我的项目中使用WinForms和MySQL。
我的表结构是...
在我的项目中,我有三个组合框和一个TextBox。 “选项”组合框包含三个值:
当我选择城市,州和国家组合框时,必须选中。
当我选择州时,必须选择国家/地区组合框。
当我选择国家/地区时,无需选择州和国家/地区组合框。
我试图使用以下代码插入此数据:
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();
选择所有ComboBoxes后,此代码即可正常工作。 但是,当未选择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();
我无法摆脱这个错误。 我的代码应如何用于该项目?
我有一个简单的方法
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();
在这种情况下,您不需要任何尝试和捕获。
尝试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.
}
您需要更改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());
}
尝试在组合框中选择项目代码。
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{}
现在,此代码可以满足您的要求。 好?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.