Here users browse to the Excel file from there system and then data is uploaded to sql database table.
I get this Error in Following Code : The Microsoft Jet database engine could not find the object 'C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\Student Registration1.xls'. Make sure the object exists and that you spell its name and the path name correctly.
I have no idea why it is asking for this Path ? C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\
protected void import_xls_Click(object sender, EventArgs e)
{
try
{
if (xmlupload.HasFile)
{
string path = xmlupload.PostedFile.FileName;
string excelConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1'";
OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);
excelConnection.Open();
OleDbCommand cmd = new OleDbCommand("select * from [Sheet2$]", excelConnection);
OleDbDataReader dReader = cmd.ExecuteReader();
SqlBulkCopy sqlBulk = new SqlBulkCopy(SqlConnectionstring.mainConnectionString, SqlBulkCopyOptions.KeepIdentity);
sqlBulk.DestinationTableName = "dbo.studentA";
sqlBulk.ColumnMappings.Add("UserName", "UserName");
sqlBulk.ColumnMappings.Add("Password", "Password");
sqlBulk.ColumnMappings.Add("Name", "Name");
sqlBulk.ColumnMappings.Add("Standard", "Standard");
sqlBulk.ColumnMappings.Add("Division", "Division");
sqlBulk.ColumnMappings.Add("SchoolName", "SchoolName");
sqlBulk.ColumnMappings.Add("Language", "Language");
sqlBulk.ColumnMappings.Add("ExamStatus", "ExamStatus");
sqlBulk.ColumnMappings.Add("Result", "Result");
sqlBulk.WriteToServer(dReader);
lblmsg.Visible = true;
lblmsg.Text = "Your data uploaded successfully";
excelConnection.Close();
}
}
catch (Exception ex)
{
lblmsg.Visible = true;
lblmsg.Text = ex.Message.ToString();
}
SqlConnectionstring.cs :
public class SqlConnectionstring
{
public SqlConnectionstring()
{
//
// TODO: Add constructor logic here
//
}
public static readonly string mainConnectionString = ConfigurationManager.ConnectionStrings["constring"].ToString();
}
path
var you're using in the OleDBCommand call? Pretty much guaranteed that if it's not an absolute path, C# will use the current working directory of your program, which is almost certainly wherever the .exe for your compiled app is going. - Marc BThe file name that the FileName property returns does not include the path of the file on the client.
- Steve