简体   繁体   中英

I am getting Datatype mismatch error due to blank cells in DataGridView

I have a code that takes data from excel to DataGridView and then from there saves on to an access database. I think my code is good but i keep getting

"Datatype mismatch"

I believe it is because of the the blank cells in the DataGridView. Can someone please suggest a different approach? Thanks

private void btn_sal2_Click(object sender, EventArgs e)
    {
        OleDbConnection cnEMP2 = new OleDbConnection();
        cnEMP2.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\SWFAL\Desktop\McDat2019.mdb";
        cnEMP2.Open();
        using (OleDbCommand cmdEMP2 = new OleDbCommand())
        {
            foreach (DataGridViewRow EMPRow in this.dgvsal.Rows)
            {
                for (int i = 0; i <= EMPRow.Cells.Count; i++)
                {
                    if (EMPRow.Cells[i].Value != null || !string.IsNullOrWhiteSpace(Convert.ToString(EMPRow.Cells[i].Value)))
                    {
                        cmdEMP2.Connection = cnEMP2;
                        string qryEMP = "UPDATE Salinity2 set Texture='" + Convert.ToString(EMPRow.Cells[1].Value) + "', EC='" + Convert.ToString(EMPRow.Cells[2].Value) + "', Cl='" + Convert.ToString(EMPRow.Cells[3].Value) + "', NO3N='" + Convert.ToString(EMPRow.Cells[4].Value) + "', pH='" + Convert.ToString(EMPRow.Cells[5].Value) + "', CO3='" + Convert.ToString(EMPRow.Cells[6].Value) + "', HCO3='" + Convert.ToString(EMPRow.Cells[7].Value) + "', Volume='" + Convert.ToString(EMPRow.Cells[8].Value) + "', [NH4-N]='" + Convert.ToString(EMPRow.Cells[9].Value) + "', Na='" + Convert.ToString(EMPRow.Cells[10].Value) + "', Ca='" + Convert.ToString(EMPRow.Cells[11].Value) + "', Mg='" + Convert.ToString(EMPRow.Cells[12].Value) + "', K='" + Convert.ToString(EMPRow.Cells[13].Value) + "', SO4='" + Convert.ToString(EMPRow.Cells[14].Value) + "', Boron='" + Convert.ToString(EMPRow.Cells[15].Value) + "', [ICAP-P]='" + Convert.ToString(EMPRow.Cells[16].Value) + "', Fe='" + Convert.ToString(EMPRow.Cells[17].Value) + "', Zn='" + Convert.ToString(EMPRow.Cells[18].Value) + "', Cu='" + Convert.ToString(EMPRow.Cells[19].Value) + "', Mn='" + Convert.ToString(EMPRow.Cells[20].Value) + "' where LabID=" + Convert.ToInt32(EMPRow.Cells[0].Value) + " ";
                        cmdEMP2.CommandText = qryEMP;
                        cmdEMP2.CommandType = CommandType.Text;
                        cmdEMP2.ExecuteNonQuery();
                    }
                    else
                    {

                    }
                }

            }
  }

To load from Excel into a dataset, try the following.

using System;
using System.Drawing;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel; 

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

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                System.Data.OleDb.OleDbConnection MyConnection ;
                System.Data.DataSet DtSet ;
                System.Data.OleDb.OleDbDataAdapter MyCommand ;
                MyConnection = new System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source='c:\\csharp.net-informations.xls';Extended Properties=Excel 8.0;");
                MyCommand = new System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyConnection);
                MyCommand.TableMappings.Add("Table", "TestTable");
                DtSet = new System.Data.DataSet();
                MyCommand.Fill(DtSet);
                dataGridView1.DataSource = DtSet.Tables[0];
                MyConnection.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show (ex.ToString());
            }
        }
   }
}

To export from datagridview to MS Access.

private void btnExportToAccess_Click(object sender, EventArgs e)
            {
                Spire.DataExport.Access.AccessExport accessExport = new             
                Spire.DataExport.Access.AccessExport();
                accessExport.DataSource = Spire.DataExport.Common.ExportSource.DataTable;
                accessExport.DataTable = this.dataGridView1.DataSource as DataTable;
                accessExport.DatabaseName = @"..\..\ToMdb.mdb";
                accessExport.TableName = "ExportFromDatatable";
                accessExport.SaveToFile();
            }

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