C#显示sql数据库中的字符串

时间:2016-02-06 10:35:15

标签: c# mysql sql-server database listbox

我正在尝试制作一个小型联系簿,其中包含来自mssql数据库的联系人详细信息。它有3个表:contacts,last_talk(上次我与联系人交谈+讨论的简短描述),以及另一个表(即有前两个表中的两个主键)

在我显示联系人的表格(tabcontrol选项卡)上,我添加了2个列表框,一个加载并显示联系人姓名,第二个列表框为我选择的每个联系人加载“最后一次谈话”列表,具体取决于多少我与一位联系人进行了“谈话”。

现在,当我在“对话列表”列表框中选择一个字段时,我正试图在我的数据库中显示label.Text和richTextBox.Text内部。

我的数据库有3个表:1个用于联系人,1个用于讨论/会话,1个用于创建前2个表之间的关系。

以下是代码的一部分:

private void PopulateSelectedTalk()
    {
        string query = "SELECT * FROM LastTalk WHERE Id = @ID";

        using (connection = new SqlConnection(connectionString))
        using (SqlCommand command = new SqlCommand(query, connection))
        using (SqlDataAdapter adapter = new SqlDataAdapter(command))
        {
            command.Parameters.Add("@ID", SqlDbType.Int);
            command.Parameters["@ID"].Value = lstConversationList.SelectedValue;


            connection.Open();
            SqlDataReader rdr = command.ExecuteReader();

            while (rdr.Read())
            {
                lblLastTalk.Text = rdr["LastTalkSubject"].ToString();
                rtxtLastTalkDescription.Text = rdr["LastTalkDescription"].ToString();

            }
            rdr.Close();
            connection.Close();

        }
    }
    private void lstConversationList_SelectedIndexChanged(object sender, EventArgs e)
    {
        PopulateSelectedTalk();
    }

我遇到了一个非常类似的问题:C# Displaying a sql database object's name in a label.Text property on "SelectedIndexChanged" Event

在我添加

之后
command.Parameters.Add("@ID", SqlDbType.Int);
        command.Parameters["@ID"].Value = lstConversationList.SelectedValue;

它有效。

然而,现在我正在尝试使用lstConversationList.SelectedValue;我得到以下异常

  

System.Data.dll中发生了'System.InvalidCastException'类型的异常,但未在用户代码中处理   附加信息:无法将参数值从DataRowView转换为Int32。

此行抛出异常:SqlDataReader rdr = command.ExecuteReader();

当我在上一期的调试模式中将鼠标悬停在“Listbox.SelectedValue”上时,我得到的值为“1”,当我将鼠标悬停在我上面发布的代码中的“Listbox.SelectedValue”时“System.DataRowView”

以下是上一个问题的代码(可行):

private void PopulateContactLabels()
    {
        string query = "SELECT * FROM Contact WHERE Id = @ID";

        using (connection = new SqlConnection(connectionString))
        using (SqlCommand command = new SqlCommand(query, connection))
        using (SqlDataAdapter adapter = new SqlDataAdapter(command))
        {
            command.Parameters.Add("@ID", SqlDbType.Int);
            command.Parameters["@ID"].Value = lstContactList.SelectedValue;

            connection.Open();
            SqlDataReader rdr = command.ExecuteReader();

            while (rdr.Read())
            {
                lblContactName.Text = rdr["Name"].ToString();
                lblCompany.Text = rdr["Company"].ToString();
                lblOccupation.Text = rdr["Occupation"].ToString();
                lblPhoneNumber.Text = rdr["PhoneNumber"].ToString();
                lblEmail.Text = rdr["Email"].ToString();
            }
            rdr.Close();
            connection.Close();

        }
    }

我错过了什么/做错了什么? 请帮忙!

编辑:设置lstConversationlist

中项目的代码
private void PopulateTalkList()
    {
        string query = "SELECT a.LastTalkSubject FROM LastTalk a " + "INNER JOIN ContactLastTalk b ON a.Id = b.LastTalkId " +
            "WHERE b.ContactId = @ContactId";

        using (connection = new SqlConnection(connectionString))
        using(SqlCommand command = new SqlCommand(query,connection))
        using (SqlDataAdapter adapter = new SqlDataAdapter(command))
        {
            command.Parameters.AddWithValue("@ContactId", lstContactList.SelectedValue);
            DataTable lastTalkTable = new DataTable();
            adapter.Fill(lastTalkTable);

            lstConversationList.DisplayMember = "LastTalkSubject";
            lstConversationList.ValueMember = "Id";
            lstConversationList.DataSource = lastTalkTable;
         }
    }

2 个答案:

答案 0 :(得分:0)

Value应该是字符串,所以事情将是

lstConversationList.SelectedValue.toString();

答案 1 :(得分:0)

问题源于您将lstConversationList的DataSource设置为DataTable并忘记将其ValueMember属性设置为包含要从SelectedValue检索的值的列的名称属性。

将DataSource属性设置为DataTable时,ListBox.Items集合中的每个项目都是DataRowView。如果您未设置ValueMember属性,则SelectedValue无法知道哪个列读取其返回值。它只返回整个DataRowView实例,当然,这不是您要搜索的整数。

所以当你填写lstConversationList时别忘了写

private void PopulateTalkList()
{

    ' NOTE. you need to add also the ID field to the select query

    string query = @"SELECT a.Id, a.LastTalkSubject 
                     FROM LastTalk a INNER JOIN ContactLastTalk b 
                     ON a.Id = b.LastTalkId
                     WHERE b.ContactId = @ContactId";

    using (connection = new SqlConnection(connectionString))
    using(SqlCommand command = new SqlCommand(query,connection))
    using (SqlDataAdapter adapter = new SqlDataAdapter(command))
    {
        command.Parameters.AddWithValue("@ContactId", lstContactList.SelectedValue);
        DataTable lastTalkTable = new DataTable();
        adapter.Fill(lastTalkTable);

        lstConversationList.DisplayMember = "LastTalkSubject";
        lstConversationList.ValueMember = "Id";
        lstConversationList.DataSource = lastTalkTable;
     }
}