Marcin Kapcia
Marcin Kapcia

Reputation: 11

Visual Studio Database issue with Adapter

When I try to run the program, I get this error and I cant solve it. Is it because I am trying to get the data from the same method.

rivate void populateTableTopping()
        {
            string query = "SELECT a.Name FROM Toppings a  " +
                "INNER JOIN Table b ON a.Id = b.ToppingsId " +
                "WHERE b.TypeId = @TypeId";


            using (Connection = new SqlConnection(connectionstring))
            using (SqlCommand command = new SqlCommand(query, Connection))
            using (SqlDataAdapter adapter = new SqlDataAdapter(command))
            {
                command.Parameters.AddWithValue("@TypeId", sizeTypeListBox.SelectedValue);

                DataTable tableToppingTable = new DataTable();
                adapter.Fill(tableToppingTable);

                tableToppingListBox.DisplayMember = "Name";
                tableToppingListBox.ValueMember = "Id";
                tableToppingListBox.DataSource = tableToppingTable;
            }
        }

and get this error: in Adapter.fill System.ArgumentException: 'No mapping exists from object type System.Data.DataRowView to a known managed provider native type.'

Upvotes: 0

Views: 27

Answers (1)

nvoigt
nvoigt

Reputation: 77304

sizeTypeListBox.SelectedValue is a System.Data.DataRowView and TypeId is not. They don't match.

Do you want the displayed text?

var text = sizeTypeListBox.GetItemText(sizeTypeListBox.SelectedItem); 
command.Parameters.AddWithValue("@TypeId", text);

Upvotes: 1

Related Questions