i am uploading csv file to sql server and at the same time binding to gridview at the same time , but i am receiving an server that ,
Could not find a part of the path 'D:\Send Free SmS\6-2-15\Files\SBM List.csv'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.IO.DirectoryNotFoundException: Could not find a part of the path 'D:\Send Free SmS\6-2-15\Files\SBM List.csv'.
Source Error:
Line 46: //Upload and save the file Line 47: string csvPath = Server.MapPath("~/Files/") + Path.GetFileName(FileUpload1.PostedFile.FileName); Line 48: FileUpload1.SaveAs(csvPath); Line 49: Line 50: DataTable dt = new DataTable();
Source File: d:\Send Free SmS\6-2-15\Add-Contact.aspx.cs Line: 48
protected void Upload(object sender, EventArgs e) { //Upload and save the file string csvPath = Server.MapPath("~/Files/") + Path.GetFileName(FileUpload1.PostedFile.FileName); FileUpload1.SaveAs(csvPath);
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3]
{
new DataColumn("Name", typeof(string)),
new DataColumn("Mobile",typeof(string)),
new DataColumn("UserId", typeof(int))});
string csvData = File.ReadAllText(csvPath);
foreach (string row in csvData.Split('\n'))
{
if (!string.IsNullOrEmpty(row))
{
dt.Rows.Add();
int i = 0;
foreach (string cell in row.Split(','))
{
dt.Rows[dt.Rows.Count - 1][i] = cell;
i++;
}
}
}
string consString = ConfigurationManager.ConnectionStrings["testsqlcon"].ConnectionString;
using (SqlConnection con = new SqlConnection(consString))
{
using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(con))
{
sqlBulkCopy.DestinationTableName = "dbo.addmobi";
con.Open();
sqlBulkCopy.WriteToServer(dt);
con.Close();
}
}
}