TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
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
<asp:FileUpload ID=
"FileUpload1"
runat=
"server"
/>
<asp:Button ID=
"Button1"
runat=
"server"
Text=
"Upload Spreadsheet"
OnClick=
"Upload"
/>
<asp:Label ID=
"lblMessage"
runat=
"server"
Text=
"Label"
></asp:Label>
Code Behind
protected
void
Upload(
object
sender, EventArgs e)
{
string
filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
string
contentType = FileUpload1.PostedFile.ContentType;
using
(Stream fs = FileUpload1.PostedFile.InputStream)
{
using
(BinaryReader br =
new
BinaryReader(fs))
{
byte
[] bytes = br.ReadBytes((Int32)fs.Length);
string
constr = ConfigurationManager.ConnectionStrings[
"SalesConnectionString"
].ConnectionString;
using
(SqlConnection con =
new
SqlConnection(constr))
{
con.Open();
string
query =
"INSERT INTO Upload VALUES (@Name, @ContentType)"
;
using
(SqlCommand cmd =
new
SqlCommand(query))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue(
"@Name"
, filename);
cmd.Parameters.AddWithValue(
"@ContentType"
, contentType);
cmd.ExecuteNonQuery();
con.Close();
}
}
}
}
}
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?
Reply
Answers (
2
)
asp.net core foreach and if not working properly
How to create cascading dropdownlist in asp.net mvc