Chris Johnson

Chris Johnson

  • NA
  • 41
  • 4.5k

Uploading an Excel File to MS ACCESS Table using FileUpload

Feb 19 2021 12:53 PM
Hi all,
I think I am very close to suceeding but I am getting an error message.
The Scenario is I am using the FileUpload tool, Grabbing the Excel File and hitting an upload button.
Here is the code...
ASP
  1. <asp:FileUpload ID="FileUpload1" runat="server" />  
  2. <asp:Button ID="Button1" runat="server" Text="Upload Spreadsheet" OnClick="Upload"/>  
  3. <asp:Label ID="lblMessage" runat="server" Text="Label"></asp:Label>  
Code Behind
  1. protected void Upload(object sender, EventArgs e)    
  2. {    
  3.     string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);    
  4.     string contentType = FileUpload1.PostedFile.ContentType;    
  5.     using (Stream fs = FileUpload1.PostedFile.InputStream)    
  6.     {    
  7.         using (BinaryReader br = new BinaryReader(fs))    
  8.         {    
  9.             byte[] bytes = br.ReadBytes((Int32)fs.Length);    
  10.             string constr = ConfigurationManager.ConnectionStrings["SalesConnectionString"].ConnectionString;    
  11.             using (SqlConnection con = new SqlConnection(constr))    
  12.             {    
  13.                 con.Open();    
  14.                 string query = "INSERT INTO Upload VALUES (@Name, @ContentType)";    
  15.                 using (SqlCommand cmd = new SqlCommand(query))    
  16.                 {    
  17.                     cmd.Connection = con;    
  18.                     cmd.Parameters.AddWithValue("@Name", filename);    
  19.                     cmd.Parameters.AddWithValue("@ContentType", contentType);  
  20.                     cmd.ExecuteNonQuery();    
  21.                     con.Close();    
  22.                 }    
  23.             }    
  24.         }    
  25.     }  
  26. } 
However I am getting the Error
 
Exception Details: System.Data.SqlClient.SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
 
Is it because I am pointing at my C: Drive rather than a Network?

Answers (2)