3 Replies - 680 Views - Last Post: 06 February 2016 - 02:34 AM Rate Topic: -----

#1 cross101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 06-January 15

displaying a database value in a label.Text text property

Posted 05 February 2016 - 04:23 PM

Hello!

A am trying to make a Contact Book, in windows forms, on the first tab i have 2 listBoxes. First displays the contact names from a database, and inside the second listBox, when i select a contact(in the first listBox) there are displayed the conversations i had with the selected contact, by the discussion's subject/title.

My database has 3 tables: 1 for contacts, 1 for discussions, 1 for creating a relation between the first 2 tables.

What i am trying to do is, when i select a conversation from the listBox, i want change the text of a label with the conversation's subject/title, and to display inside a richTextBox the conversation's description.

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();
    }
}


This is how the method for displaying the conversation listBox looks:
Code where i populate my "Conversation List" listBox:

        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;
         }
    }


I get the

Quote

'System.InvalidCastException'
Failed to convert parameter value from a DataRowView to a Int32.
exception.

What am i doing wrong?
Help please!

Is This A Good Question/Topic? 0
  • +

Replies To: displaying a database value in a label.Text text property

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 6158
  • View blog
  • Posts: 21,236
  • Joined: 05-May 12

Re: displaying a database value in a label.Text text property

Posted 05 February 2016 - 04:35 PM

Which line is throwing that exception?

Why are you trying to pass in a DataRowView to something that is expecting an Int32?
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6530
  • View blog
  • Posts: 14,446
  • Joined: 02-June 10

Re: displaying a database value in a label.Text text property

Posted 05 February 2016 - 06:32 PM

Quote

in windows forms

That's your first mistake. Its not 1999 anymore.

http://www.dreaminco...-to-wpf-part-1/
Was This Post Helpful? 1
  • +
  • -

#4 cross101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 06-January 15

Re: displaying a database value in a label.Text text property

Posted 06 February 2016 - 02:34 AM

View PostSkydiver, on 05 February 2016 - 04:35 PM, said:

Which line is throwing that exception?

Why are you trying to pass in a DataRowView to something that is expecting an Int32?


The line that throws the exception is
SqlDataReader rdr = command.ExecuteReader();


I am trying get the following result: when i select a conversation from the conversation listBox, to change the text of a label and to display some text in a richTextBox. The text i want to display in the label and richTextBox represents a database entry in my "LastConversation" table.

I am passing a DataRowView because i want to get the selected object(represented by the selected listBox field)and if i use "listboxName.SelectedIndex" it doesnt seem to work.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1