Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在列表框中显示查询结果?_C#_Sql_Database_Wpf_Tsql - Fatal编程技术网

C# 如何在列表框中显示查询结果?

C# 如何在列表框中显示查询结果?,c#,sql,database,wpf,tsql,C#,Sql,Database,Wpf,Tsql,我有一个数据库,其中包含实体AuthorId(int)(AuthorId是主键)、Name(nvarchar)和national(nvarchar)。我还有一个列表框,它显示来自该数据库的名称,还有三个文本框,我想在其中显示列表框中所选项目的Id、名称和国籍,每个文本框一个。根据我现在拥有的,我得到以下错误: 将varchar值转换为数据类型int时,转换失败 以下是我获取数据并将其显示在列表框中的方法: public void GetAuthorData() { string conn

我有一个数据库,其中包含实体
AuthorId
(int)(AuthorId是主键)、
Name
(nvarchar)和
national
(nvarchar)。我还有一个列表框,它显示来自该数据库的名称,还有三个文本框,我想在其中显示列表框中所选项目的Id、名称和国籍,每个文本框一个。根据我现在拥有的,我得到以下错误:

将varchar值转换为数据类型int时,转换失败

以下是我获取数据并将其显示在列表框中的方法:

public void GetAuthorData()
{
    string connectionString = //connection string here
    using (SqlConnection con = new SqlConnection(connectionString))
    {
        con.Open();
        var query = "SELECT * FROM Author";
        using (SqlCommand cmd = new SqlCommand(query, con))
        using (SqlDataReader reader = cmd.ExecuteReader())
        {
            while (reader.Read())
            {
                Author a = new Author();
                a.Id = reader.GetInt32(0);
                a.Name = reader.GetString(1);
                a.Nationality = reader.GetString(2);

                AuthorListBox.Items.Add(a);
            }
        }
    }
}
下面是我在文本框中显示数据的方式:

private void AuthorListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    DeleteAuthortButton.IsEnabled = true;
    SaveChangesButton.IsEnabled = true;

    var aa = sender as ListBox;
    if (aa.SelectedItem != null)
    {
       var author = aa.SelectedItem.ToString();
        string connectionString = //connection string here
        using (SqlConnection con = new SqlConnection(connectionString))
        {
            con.Open();
            var query = "SELECT * FROM Author where Name = '" + author + "'";
            using (SqlCommand cmd = new SqlCommand(query, con))
            using (SqlDataReader reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    Author a = new Author();
                    a.Id = reader.GetInt32(0);
                    a.Name = reader.GetString(1);
                    a.Nationality = reader.GetString(2);

                    IdTextBox.Text = a.Id.ToString();
                    AuthorNameTextBox.Text = a.Name;
                    NationalityTextBox.Text = a.Nationality;
                }
            }
        }
    }
}

Sql
Number
不必对应.Net
int

      a.Id = reader.GetInt32(0); // May fail even if Id is Number
尝试使用
转换

      // Whatever Author.Id is 
      // (e.g. Number(10) which will be mapped to Int64, not int or
      //       Number(30) which can be mapped to String), 
      // try convert it into `Int32 == int`
      a.Id = Convert.ToInt32(reader[0]);
      a.Name = Convert.ToString(reader[1]);
      a.Nationality = Convert.ToString(reader[2]);

您不需要检索数据库数据。关于作者的所有信息都在列表中

    private void AuthorListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        DeleteAuthortButton.IsEnabled = true;
        SaveChangesButton.IsEnabled = true;

        var aa = sender as ListBox;
        if (aa.SelectedItem != null)
        {
           var author = aa.SelectedItem as Author;

           IdTextBox.Text = author.Id.ToString();
           AuthorNameTextBox.Text = author.Name;
           NationalityTextBox.Text = author.Nationality; 
        }
    }

a.Id=Convert.ToString(读卡器[0])您已经将作者设置为列表项,所以请使用它
var author=aa。选择editem作为作者并且您不需要再次从DB中选择任何内容。@DmitryBychenko我得到“无法隐式转换类型'string'到'int'”当我使用thisWell时,我没有收到任何错误消息,但它没有在文本框中打印出任何内容。@anek05:您是否正确填写了
AuthorListBox
对不起,正确填写是什么意思?我在数据库中有三个项目,如果这是您的意思,它们会打印在列表框中。
public void GetAuthorData()
填充
authordistbox
列表框:
。。。AuthorListBox.Items.Add(a)是否已填充?将断点放在
var query=“SELECT*FROM Author where Name=”+Author+“”您在
何处
中输入的
作者
值是多少?