将数据加载到没有空白的组合框 [英] loading data to combo box with out blanks
本文介绍了将数据加载到没有空白的组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一些代码将问题从我的MSSQL服务器加载到组合框中。代码工作,它加载数据,但当我从下拉列表中选择项目时,它会从SQL表中加载所有空白区域。
这是我的代码:
private void loadlocationcombobox()
{
string constr = ConfigurationManager.ConnectionStrings [ SQLServer]。ConnectionString;
string query = 从dbo中选择location_name .Location跨度>;
SqlConnection con = new SqlConnection(constr);
SqlCommand command = new SqlCommand(query,con);
con.Open();
SqlDataReader sqlreader = command.ExecuteReader();
尝试
{
while (sqlreader.Read())
{
comboLocation.Items.Add(sqlreader [ 0 ]);
}
}
catch (Exception ex)
{
MessageBox.Show( 发生错误: + ex.Message, error,MessageBoxButtons.OK,MessageBoxIcon.Error);
}
最后
{
sqlreader.Close();
con.Close();
}
}
任何帮助都会很棒,因为我一直都很棒查看代码,但无法看到如何修复它。
解决方案
尝试将char转换为varchar,如下所示
选择 CAST(RTRIM(location_name) AS VARCHAR ( 50 ))来自 dbo.Location
如果我理解你的问题,你想限制加载空格到组合
选择 location_name 来自 dbo.Location where isnull(location_name,' ')<> ' '跨度>
Hi,
im have a problem with some code to load data from my MSSQL server in to a combo box. the code works and it loads the data but when i select the item from the drop down it loads all the blank space from the SQL table aswell.
this is my code:
private void loadlocationcombobox()
{
string constr = ConfigurationManager.ConnectionStrings ["SQLServer"].ConnectionString;
string query = "select location_name from dbo.Location";
SqlConnection con = new SqlConnection(constr);
SqlCommand command = new SqlCommand(query, con);
con.Open();
SqlDataReader sqlreader = command.ExecuteReader();
try
{
while (sqlreader.Read())
{
comboLocation.Items.Add(sqlreader[0]);
}
}
catch (Exception ex)
{
MessageBox.Show("A error has occurred: " + ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
sqlreader.Close();
con.Close();
}
}
any help would be great as i have been looking at the code but just cant see how to fix it.
解决方案
try converting char to varchar as follows
select CAST(RTRIM(location_name) AS VARCHAR(50)) from dbo.Location
If I understood your problem correctly, you want to restrict loading blank spaces from to the combo
select location_name from dbo.Location where isnull(location_name ,'')<>''
这篇关于将数据加载到没有空白的组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文