Am trying to export data from excel sheet to sql server.
am getting error like this
System.Data.SqlClient.SqlException: Incorrect syntax near ')'.
Below is my code.Please Check and help me to fix this problem ...please
Thanks in advance.....
protected void btnUpload_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(constr);
// FilePath with File Name
String file = FileUpload1.PostedFile.FileName;
string path = Server.MapPath(System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName));
FileUpload1.PostedFile.SaveAs(path);
// Check for File Existence in the path
if (FileUpload1.HasFile)
{
// Declaring Excel object
Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
// Opening the Excel File
Microsoft.Office.Interop.Excel.Workbook workBook = app.Workbooks.Open(path, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
// get the active worksheet using sheet name or active sheet
Microsoft.Office.Interop.Excel.Worksheet workSheet = (Microsoft.Office.Interop.Excel.Worksheet)workBook.ActiveSheet;
Microsoft.Office.Interop.Excel.Range range;
range = workSheet.UsedRange;
SqlCommand cmd = new SqlCommand();
for (int rCnt = 2; rCnt <= range.Rows.Count; rCnt++)
{
cmd.Connection = conn;
cmd.CommandText = "insert into ExcelTable(Name,Desig) values('" + (range.Cells[rCnt, 2] as Microsoft.Office.Interop.Excel.Range).Value2 + "',"
+ (range.Cells[rCnt, 3] as Microsoft.Office.Interop.Excel.Range).Value2 + ")";
conn.Open();
int i = cmd.ExecuteNonQuery(); //Am getting error in this line only
conn.Close();
}
}
}