简体   繁体   中英

No mapping exists from object type System.Windows.Forms.DataGridViewTextBoxCell to a known managed provider native type

I have created a datagridview with a column for a 'Submit' button. In this process, I am trying to run a SQL query given several parameters. When I do so, I am getting the following:

No mapping exists from object type System.Windows.Forms.DataGridViewTextBoxCell to a known managed provider native type.

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    try
    {
        Image img2;
        string imagePath = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
        img2 = Image.FromFile(@"C:\receipts\" + imagePath);
        var senderGrid = (DataGridView)sender;
        if (senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn && e.RowIndex >= 0)
        {
            string SqlAttach = "INSERT INTO WeeklyReceipts " +
                                        "(StaffID,RU,ReceiptImage,Amount,ReceiptDate,Category,Comments,OriginalFileName) " +
                                " VALUES " +
                                        "( '" + "@StaffID, @RU, @ReceiptImage, @Amount, @ReceiptDate, @Category, @Comments, @OriginalFileName)";
            SqlCommand sc = new SqlCommand(SqlAttach, conFB);
            conFB.Open();
            sc.Parameters.Clear();
            sc.Parameters.AddWithValue("@StaffID", comboBox2.Text);                                                 
            sc.Parameters.AddWithValue("@RU", comboBox1.Text);                                                      
            sc.Parameters.AddWithValue("@Amount", dataGridView1.Rows[e.RowIndex].Cells[3]);                  
            sc.Parameters.AddWithValue("@ReceiptDate", dataGridView1.Rows[e.RowIndex].Cells[1]);        
            sc.Parameters.AddWithValue("@Category", dataGridView1.Rows[e.RowIndex].Cells[2]);            
            sc.Parameters.AddWithValue("@Comments", dataGridView1.Rows[e.RowIndex].Cells[4]);              
            sc.Parameters.AddWithValue("@OriginalFileName", dataGridView1.Rows[e.RowIndex].Cells[0]);

            sc.ExecuteNonQuery();

            conFB.Close();
        }
    }
    catch (Exception ex3)
    {
        MessageBox.Show(ex3.Message);
        conFB.Close();
    }
}

dataGridView1.Rows[e.RowIndex].Cells[3] (and others) returns an object of type DataGridTextBoxCell. You need to get the Value property so you're passing a string.

– madreflection

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM