EDITED!:
I am writing a program with a function of loading a excel file into a datagridview. Excel file contains product list with ten columns. As for now it works fine. A file is loaded fine. But just after this load a program should fill a SQL Server CE database with data from datagridview. Therefore next time i open this form a datagridview should be filled with data from database. (This excel loading function is to update a product list when my company changes something).
I have a problem with inserting this data into database.
I have an error:
There was an error parsing the query. [Token line number = 1, Token line offset = 67, Token in error = Taq]
DNA is a word in a cell of Excel file (line 1 column 3 ("ITEM"). Full content of cell is AB-AB-0192/A Taq DNA Polymerase (licensed). I think that a problem is connected somehow with space before TAQ. I tested this: when i delete space there, a problem info changes from Taq to DNA. So how can I avoid this? All columns in excel file are set as text, and SQL Server CE database column is of type nvarchar.
EDIT!
Ok guys you put me on the right path:)
This works:
string strQuery = @"INSERT INTO TabelaProdukty VALUES (@VD, @ItemCode, @Item, @Qty, @Ppcur, @StandardPrice, @CeMarked, @Description, @Description2, @Edma)";
sqlconnection.Open();
using (System.Data.SqlServerCe.SqlCeCommand comm = new System.Data.SqlServerCe.SqlCeCommand(strQuery, sqlconnection))
{
comm.Parameters.Add("@VD", SqlDbType.NVarChar);
comm.Parameters.Add("@ItemCode", SqlDbType.NVarChar);
comm.Parameters.Add("@Item", SqlDbType.NVarChar);
comm.Parameters.Add("@Qty", SqlDbType.NVarChar);
comm.Parameters.Add("@Ppcur", SqlDbType.NVarChar);
comm.Parameters.Add("@StandardPrice", SqlDbType.NVarChar);
comm.Parameters.Add("@CeMarked", SqlDbType.NVarChar);
comm.Parameters.Add("@Description", SqlDbType.NVarChar);
comm.Parameters.Add("@Description2", SqlDbType.NVarChar);
comm.Parameters.Add("@Edma", SqlDbType.NVarChar);
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
comm.Parameters["@VD"].Value = dataGridView1.Rows[i].Cells["VD"].Value;
comm.Parameters["@ItemCode"].Value = dataGridView1.Rows[i].Cells["ItemCode"].Value;
comm.Parameters["@Item"].Value = dataGridView1.Rows[i].Cells["ITEM"].Value;
comm.Parameters["@Qty"].Value = dataGridView1.Rows[i].Cells["QUANTITY"].Value;
comm.Parameters["@Ppcur"].Value = dataGridView1.Rows[i].Cells["PPCUR"].Value;
comm.Parameters["@StandardPrice"].Value = dataGridView1.Rows[i].Cells["STANDARD_SELL_PRICE"].Value;
comm.Parameters["@CeMarked"].Value = dataGridView1.Rows[i].Cells["CE-MARKED"].Value;
comm.Parameters["@Description"].Value = dataGridView1.Rows[i].Cells["ITEM_DESCRIPTION"].Value;
comm.Parameters["@Description2"].Value = dataGridView1.Rows[i].Cells["ITEM_DESCRIPTION2"].Value;
comm.Parameters["@Edma"].Value = dataGridView1.Rows[i].Cells["EDMA"].Value;
comm.ExecuteNonQuery();
}
sqlconnection.Close();
Database is filled with the proper data and when I restart program database is already filled. Now I just need to clear database before adding new content.
One problem though. Getting message: The data was truncated while converting from one datatype to another. [Name of function (if known) = ]
sqladapter.InsertCommand.AddWithValue(@item, @paramvalue);instead unless also you're not telling whatValues(to add in your Insert statement