Hi guys
As the title suggests, I am using ASP to upload an Excel file, whose data needs to be bulk copied to my database sitting in SQL server.
I have the following code to upload:
<asp:Panel ID= "PanelUpload" runat="server"
Please select an Excel file to import: <br/>;
asp:FileUpload ID="FileUploadExcel"; runat="server";
<br\>;
<asp:Button ID="ButtonUploadFile"; runat="server" Text="Upload File" onclick="ButtonUploadFile_Click />
<br />;
<asp:Label ID="LabelUpload"; runat="server" Text=""></asp:Label>;
</asp:Panel>
protected void ButtonUploadFile_Click(object sender, EventArgs e)
{
if (FileUploadExcel.HasFile)
{
LabelUpload.Text = "File uploaded is "; + FileUploadExcel.PostedFile.FileName;
}
}
string path = Path.GetDirectoryName(FileUploadExcel.FileName);
string selectedServer = cmbServer.Text;
serverConnectionString = BC_Users.SelectServer(selectedServer);
string selectedTable = cmbTable.SelectedValue;
OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OleDb.4.0;Data Source= " + path + ";Extended Properties=Excel 8.0");
con.Open();
OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Sheet1$]", con);
OleDbDataAdapter adapt = new OleDbDataAdapter(cmd);
DataTable dtable = new DataTable();
adapt.Fill(dtable);
I keep getting errors where i have "string path = ..." saying that the path is not of a legal form, and if its not that, i get an error at con.Open() that the argument is invalid. My BulkCopy is after this, but i dont think that bit will be a problem. It doesnt even reach it when i run anyway.
This is the first time i am using excel with ASP, as well as the OleDB class. So please can you tell me what to do in the simplest way!
Thanks!