如何使用字符串列表作为列表框的数据源

发布于 2024-11-30 05:03:06 字数 1072 浏览 1 评论 0原文

这是我编写的代码,它通常可以工作,但有时会失败(或多或少有四分之一):

...
List<string> _items = new List<string>(); 
...
using (SqlCeConnection con = new SqlCeConnection(Globals.conString))
{
    string codbultocomp = null;
    con.Open();
    using (SqlCeCommand cmd = new SqlCeCommand("SELECT codbultocomp FROM envios WHERE codigodestino=@codigodestino AND estado=@pendiente", con))
    {
        cmd.Parameters.AddWithValue("@codigodestino", codigoDestino);
        cmd.Parameters.AddWithValue("@pendiente", "pendiente");

        SqlCeDataReader reader = cmd.ExecuteReader();

        while (reader.Read())
        {
            codbultocomp = reader["codbultocomp"].ToString();
            _items.Add(codbultocomp);
        }
        reader.Close();
    }
    listBox1.DataSource = _items;
} 

当它失败时,应用程序会冻结,如果我暂停调试,它会在最后一个大括号中停止。我尝试使用 try/catch 块显示错误,但它没有显示任何内容并停在同一个地方。我还尝试观察列表框数据源在“观察”列表中显示此错误:

Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.

知道我做错了什么吗?

Here is the code I made, it usually works, but sometimes fails (1 out of 4 times more or less):

...
List<string> _items = new List<string>(); 
...
using (SqlCeConnection con = new SqlCeConnection(Globals.conString))
{
    string codbultocomp = null;
    con.Open();
    using (SqlCeCommand cmd = new SqlCeCommand("SELECT codbultocomp FROM envios WHERE codigodestino=@codigodestino AND estado=@pendiente", con))
    {
        cmd.Parameters.AddWithValue("@codigodestino", codigoDestino);
        cmd.Parameters.AddWithValue("@pendiente", "pendiente");

        SqlCeDataReader reader = cmd.ExecuteReader();

        while (reader.Read())
        {
            codbultocomp = reader["codbultocomp"].ToString();
            _items.Add(codbultocomp);
        }
        reader.Close();
    }
    listBox1.DataSource = _items;
} 

When it fails the application freezes, and if I pause the debug it is stopped in the last brace. I tried to show an error using a try/catch block but it didn't show anything and stopped in the same place. I also tried to watch the listbox datasource shows this error in the "watch" list:

Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.

Any idea of what I'm doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心碎无痕… 2024-12-07 05:03:06

using 之后调用它,所有 IDisposable 对象都将在 using 之后被释放。

...
List<string> _items = new List<string>(); 
...
using (SqlCeConnection con = new SqlCeConnection(Globals.conString))
{
     ...
} 

listBox1.DataSource = _items;

Call it after your using, all IDisposable objects will be disposed after using.

...
List<string> _items = new List<string>(); 
...
using (SqlCeConnection con = new SqlCeConnection(Globals.conString))
{
     ...
} 

listBox1.DataSource = _items;
轻许诺言 2024-12-07 05:03:06

为什么不尝试这样的事情:

为了清晰的代码,创建一个类似的方法:

public List<string> getItems(string codigodestino, string pendiente)
{
    List<string> _items = new List<string>();
    SqlCeConnection con = new SqlCeConnection(Globals.conString);
    string Qyery = "SELECT codbultocomp FROM envios WHERE codigodestino='" + codigodestino + "' AND estado='" + pendiente +"'";
    try
    {
        con.Open();
        SqlCeCommand cmd = new SqlCeCommand(Qyery, con);
        cmd.CommandType = CommandType.Text;
        SqlCeDataReader rdr = cmd.ExecuteReader();
        while (rdr.Read())
        {
            _items.Add((string)reader["codbultocomp"]);
        }
        con.Close();
        return _items;
    }
    catch (Exception ex)
    {
        throw ex;
    }
    finally
    {
        con.Dispose();
        con.Close();
    }
}

然后使用:

listBox1.DataSource = getItems(codigoDestino, "pendiente");

Why don't you try something like this:

For a clear code create a method like:

public List<string> getItems(string codigodestino, string pendiente)
{
    List<string> _items = new List<string>();
    SqlCeConnection con = new SqlCeConnection(Globals.conString);
    string Qyery = "SELECT codbultocomp FROM envios WHERE codigodestino='" + codigodestino + "' AND estado='" + pendiente +"'";
    try
    {
        con.Open();
        SqlCeCommand cmd = new SqlCeCommand(Qyery, con);
        cmd.CommandType = CommandType.Text;
        SqlCeDataReader rdr = cmd.ExecuteReader();
        while (rdr.Read())
        {
            _items.Add((string)reader["codbultocomp"]);
        }
        con.Close();
        return _items;
    }
    catch (Exception ex)
    {
        throw ex;
    }
    finally
    {
        con.Dispose();
        con.Close();
    }
}

and then just use:

listBox1.DataSource = getItems(codigoDestino, "pendiente");
雨夜星沙 2024-12-07 05:03:06

您只需关闭 con 对象即可。也无需关闭阅读器。 con 对象的使用要小心。不需要休息。

You just close the con object. No need to close the reader as well. The using of con object takes care. Rest using isnt required.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文