I have a file upload control and an "upload" button in my page. I am trying to write codes for uploading word files to my db.
Please find below the code:-
if (FileUpload1.HasFile)
{
string fileextention = System.IO.Path.GetExtension(FileUpload1.FileName);
if (fileextention == ".doc" || fileextention == ".docx")
{
string filename = System.IO.Path.GetFileName(FileUpload1.FileName);
//Upload file
FileUpload1.SaveAs(Server.MapPath("~/DocumentCollection/") + filename);
string fullpath = "DocumentCollection/" + filename;
string query = "insert into dbo.DocumentMaster(Name,Path) values(" + filename + "," + "DocumentCollection/" + filename + ");";
SqlDataAdapter objda = new SqlDataAdapter(query, objcon);
objcon.Open();
objda.SelectCommand.ExecuteNonQuery();
objcon.Close();
Label2.Text = " successfully uploaded.";
}
else
{
Label2.Text = "Selected File is not a word document file.";
}
My DB structure:-
CREATE TABLE [dbo].[DocumentMaster](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](100) NULL,
[Path] [varchar](100) NULL,
CONSTRAINT [PK_DocumentMaster] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
But I am encountering an error when i am trying to upload a word.docx file. The word.docx has been successfully uploaded to the DocumentCollection folder in my application.
The multi-part identifier "word.docx" could not be bound. Invalid column name 'DocumentCollection'. The multi-part identifier "word.docx" could not be bound.
Please help me.