Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to delete a record from a datagridview via a button in a windows forms app but am getting an error message when attempting. The error is "No mapping exists from object type System.Windows.Forms.DataGridViewSelectedRowCollection to a known managed provider data type."

What I have tried:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace JacobsPokedexMaster
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'pokedexDataSet.Cards' table. You can move, or remove it, as needed.
            this.cardsTableAdapter.Fill(this.pokedexDataSet.Cards);

        }

        private void searchByNameTextBox_TextChanged(object sender, EventArgs e)
        {
            BindingSource bs = new BindingSource();
            bs.DataSource = dataGridView1.DataSource;
            bs.Filter = "pokemon_name like '%" + searchByNameTextBox.Text + "%'";
            dataGridView1.DataSource = bs.DataSource;
        }

        private void addCardFormButton_Click(object sender, EventArgs e)
        {
            addCardForm AddCardForm = new addCardForm();
            AddCardForm.Show();
        }

        private void deleteCardButton_Click(object sender, EventArgs e)
        {
            string connectionString = null;
            string sql = null;
            connectionString = "Data Source=DESKTOP-NU2DVAO\\SQLEXPRESS;Initial Catalog=Pokedex;Integrated Security=True";
            sql = "DELETE from Cards WHERE pokemon_name = @pokemon_name";
            using (SqlConnection cnn = new SqlConnection(connectionString))
            {
                try
                {
                    cnn.Open();
                    using (SqlCommand cmd = new SqlCommand(sql, cnn))
                    {
                        cmd.Parameters.AddWithValue("@pokemon_name", SqlDbType.NVarChar).Value = dataGridView1.SelectedRows;

                        int rowsRemoved = cmd.ExecuteNonQuery();
                        if (rowsRemoved > 0)
                            MessageBox.Show("Card removed from Pokedex");
                        else
                            MessageBox.Show("Card not removed.");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error:" + ex.Message);
                }
            }
        }
    }
}
Posted
Updated 13-Oct-21 19:57pm
Comments
BillWoodruff 13-Oct-21 22:59pm    
set breakpoints, and then single-step (F11) through the code in deleteCardButton_Click ... determine exactly where the errior occurs. add that information to your post here.

1 solution

Look at your code; specifically, look at the line where the error occurs.
I think it's this one:
C#
cmd.Parameters.AddWithValue("@pokemon_name", SqlDbType.NVarChar).Value = dataGridView1.SelectedRows;
You are trying to pass an entire collection of DataGridViewRow items to SQL as a name (I.e. a text string)!

You will need a loop of some form, and to select the correct column in the table, then get the value from that column as well instead of sending a whole row!
I'd also recommend that you use an ID column instead of a Name to select your delete candidates: if your pack of cards can contain duplicates (and I have no idea or wish to know if that is the case with Pokemon) then just saying "delete this name" like that will remove all matching rows, not just a single selection.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900