anek05
anek05

Reputation: 19

How do I display a query result in listboxes?

I have a database with entities AuthorId (int) (AuthorId is the primary key), Name(nvarchar) and Nationality(nvarchar). I also have a listbox, which displays the names from that database, and three textboxes where I want to display the Id, Name and Nationality, one for each textbox, of the selected item in the listbox. With what I have it now I get the following error:

Conversion failed when converting the varchar value to data type int.

Here's how I get the data and display it in the listbox:

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

And here's how I display the data in the textboxes:

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

Upvotes: 1

Views: 554

Answers (2)

Nenad J.
Nenad J.

Reputation: 341

You do not need to retrieve database data. All the information about the author is in the list.

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

Upvotes: 1

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186668

Sql Number doesn't necessary corresponds to .Net int:

      a.Id = reader.GetInt32(0); // May fail even if Id is Number

Try using Convert:

      // 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]);

Upvotes: 0

Related Questions