Hello. I have a web program that can upload a excel file to the SQL server database. So far the only error I get when I run the program is:
System.ArgumentException: 'Column 'TABLE_NAME' does not belong to table MetaDataCollections.'
Which happens on this line of code:
string sheet1 = excel_con.GetSchema().Rows[0]["TABLE_NAME"].ToString();
This only happens when I click the upload button. So I am stomped at this moment. The Excel file I am using does have small data in it. The database does not have any Tables in it.
Here is my Full Code of the HTML:
<pre><%@ Page Language="C#" AutoEventWireup="true" CodeFile="CS.aspx.cs" Inherits="CS" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button Text="Upload" OnClick = "Upload" runat="server" />
<br />
<br />
<br />
<br />
</form>
</body>
</html>
Here is the Code Behind:
using System;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class CS : System.Web.UI.Page
{
public object SqlSchemaGuid { get; private set; }
protected void Upload(object sender, EventArgs e)
{
string excelPath = Server.MapPath("~/Files/") + Path.GetFileName(FileUpload1.PostedFile.FileName);
FileUpload1.SaveAs(excelPath);
string conString = string.Empty;
string extension = Path.GetExtension(FileUpload1.PostedFile.FileName);
switch (extension)
{
case ".xls":
conString = ConfigurationManager.ConnectionStrings["AkinTestConnectionString"].ConnectionString;
break;
case ".xlsx":
conString = ConfigurationManager.ConnectionStrings["AkinTestConnectionString"].ConnectionString;
break;
case ".csv":
conString = ConfigurationManager.ConnectionStrings["AkinTestConnectionString"].ConnectionString;
break;
}
conString = string.Format(conString, excelPath);
using (SqlConnection excel_con = new SqlConnection(conString))
{
excel_con.Open();
string sheet1 = excel_con.GetSchema().Rows[0]["TABLE_NAME"].ToString();
DataTable dtExcelData = new DataTable();
dtExcelData.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
new DataColumn("Name", typeof(string)),
new DataColumn("Salary",typeof(decimal)) });
using (SqlDataAdapter reader = new SqlDataAdapter("SELECT * FROM [" + sheet1 + "]", excel_con))
{
reader.Fill(dtExcelData);
}
excel_con.Close();
string consString = ConfigurationManager.ConnectionStrings["AkinTestConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(consString))
{
using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(con))
{
sqlBulkCopy.DestinationTableName = "dbo.tblPersons";
sqlBulkCopy.ColumnMappings.Add("Id", "PersonId");
sqlBulkCopy.ColumnMappings.Add("Name", "Name");
sqlBulkCopy.ColumnMappings.Add("Salary", "Salary");
con.Open();
sqlBulkCopy.WriteToServer(dtExcelData);
con.Close();
}
}
}
}
}
What I have tried:
I have tried to change the TABLE_NAME and COMMENT the line of code out.