locked
Error occur the path format is not supported in excel import data to gridview? RRS feed

  • Question

  • User-1026236167 posted

    i want to import data from excel to gridview but an error occur path format is not supported 

    please execute them

    here is my code

    aspx

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm65.aspx.cs" Inherits="WebApplication14.WebForm65" %>

    <!DOCTYPE html>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title></title>
    </head>
    <body>

    <form id = "form1" runat="server">
    <div>
    <b>Please Select Excel File: </b>
    <asp:FileUpload ID = "fileuploadExcel" runat="server" />&nbsp;&nbsp;
    <asp:Button ID = "btnImport" runat="server" Text="Import Data" OnClick="btnImport_Click" />
    <br />
    <asp:Label ID = "lblMessage" runat="server" Visible="False" Font-Bold="True" ForeColor="#009933"></asp:Label><br />
    <asp:GridView ID = "grvExcelData" runat="server">
    <HeaderStyle BackColor = "#df5015" Font-Bold="true" ForeColor="White" />
    </asp:GridView>
    </div>
    </form>
    </body>
    </html>

    cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    using System.Data.SqlClient;
    using System.Configuration;
    using System.Data.OleDb;
    using System.IO;

    namespace WebApplication14
    {
    public partial class WebForm65 : System.Web.UI.Page
    {
    SqlCommand cmd = new SqlCommand();
    SqlConnection con = new SqlConnection();
    string connection = System.Configuration.ConfigurationManager.AppSettings["con"].ToString();


    public void EstablishConnection(string storeprocedure)
    {
    con.ConnectionString = connection;
    cmd.Connection = con;
    cmd.Connection.Open();
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = storeprocedure;
    }


    public void CloseConnection()
    {
    cmd.Connection.Close();
    cmd.Connection.Dispose();
    con.Close();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
    if (!Page.IsPostBack)
    {
    //BindGrid();

    }
    }
    //public void BindGrid()
    //{
    // using (hospitalEntities ctx = new hospitalEntities())
    // {
    // grvExcelData.DataSource = (from hospitalEntities in ctx.tb_department_designation
    // select hospitalEntities).ToList();
    // grvExcelData.DataBind();
    // }
    //}

    protected void btnImport_Click(object sender, EventArgs e)
    {
    string connString = "";
    string strFileType = Path.GetExtension(fileuploadExcel.FileName).ToLower();
    //string path = fileuploadExcel.PostedFile.FileName;

    string path = Server.MapPath("~/C:/Users/Prabhjot Aulakh/OneDrive/Desktop/" + fileuploadExcel.FileName);
    fileuploadExcel.SaveAs(path);

    //Response.Write("path=" + path);

    //string path = Server.MapPath("~/UploadDir/" + fileuploadExcel.FileName);
    //fileuploadExcel.SaveAs(path);

    //Response.Write("path=" + path); return;

    //Connection String to Excel Workbook
    if (strFileType.Trim() == ".xls")
    {
    connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
    }
    else if (strFileType.Trim() == ".xlsx")
    {
    connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
    }

    //string query = "SELECT [country],[University Type],[University Name],[Date of Contract],[Expiry Date],[Contact Person],[Contact No.],[Email-Id] FROM [International details$]";
    string query = "select * from [Sheet1$]";
    OleDbConnection conn = new OleDbConnection(connString);
    if (conn.State == ConnectionState.Closed)
    conn.Open();
    OleDbCommand cmd = new OleDbCommand(query, conn);
    OleDbDataAdapter da = new OleDbDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds);
    grvExcelData.DataSource = ds.Tables[0];
    grvExcelData.DataBind();
    da.Dispose();
    conn.Close();
    conn.Dispose();

    //BindGrid();
    }

    }
    }

    Thursday, June 18, 2020 6:46 AM

Answers

  • User-719153870 posted

    Hi prabhjot1313,

    an error occur path format is not supported 

    string path = Server.MapPath("~/C:/Users/Prabhjot Aulakh/OneDrive/Desktop/" + fileuploadExcel.FileName);
    fileuploadExcel.SaveAs(path);

    Regarding to the error message itself, it indicates that the path parameter for Server.MapPath() is invalid, the path parameter should be virtual path rather than physical one.

    "Returns the physical file path that corresponds to the specified virtual path."

    You can check this thread for some good examples.

    As for the SaveAs() method, this method is used to save client side file to server.

    "Saves the contents of an uploaded file to a specified path on the Web server."

    According to the current code provided, the user uploads excel file with FileUpload control, while the sample path "~/C:/Users/Prabhjot Aulakh/OneDrive/Desktop/" looks like a client side path, and then the program SaveAs to the client side path? It seems doesn't make much sense.

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 18, 2020 8:17 AM