Hi
I have two btn s one is insert btn another one is fileupload btn.In the fileupload btn i creat a code in blow.I can upload the file suecessfully .i need to insert a file in sql by using insert btn.for that i can creat a insert query every thiong but i can not acess the string filename value in the insert btn. so how to i get the string value for insert btn.i mentioned the insert query also.
protected void UploadButton_Click(object sender, EventArgs e)
{
string filePath = FileUploadControl.PostedFile.FileName;
string filename = Path.GetFileName(filePath);
string ext = Path.GetExtension(filename);
string contenttype = String.Empty;
switch (ext)
{
case ".doc":
contenttype = "application/vnd.ms-word";
break;
case ".docx":
contenttype = "application/vnd.ms-word";
break;
case ".xls":
contenttype = "application/vnd.ms-excel";
break;
case ".xlsx":
contenttype = "application/vnd.ms-excel";
break;
case ".jpg":
contenttype = "image/jpg";
break;
case ".png":
contenttype = "image/png";
break;
case ".gif":
contenttype = "image/gif";
break;
case ".pdf":
contenttype = "application/pdf";
break;
}
if (contenttype != String.Empty)
{
Stream fs = FileUploadControl.PostedFile.InputStream;
BinaryReader br = new BinaryReader(fs);
Byte[] bytes = br.ReadBytes((Int32)fs.Length);
StatusLabel.ForeColor = System.Drawing.Color.Green;
StatusLabel.Text = "File Uploaded Successfully";
}
else
{
StatusLabel.ForeColor = System.Drawing.Color.Red;
StatusLabel.Text = "File format not recognised." +
" Upload Image/Word/PDF/Excel formats";
}
Insert Btn:
protected void btnInsert_Click(object sender, EventArgs e)
{
cmd = new SqlCommand("insert into emp(FileName,ContanType, Data)values(@FileName, @ContanType, @Data)", con);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@FileName", filename);
cmd.Parameters.AddWithValue("@ContanType", contenttype);
cmd.Parameters.AddWithValue("@Data",bytes);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
Response.Redirect("Default.aspx");
}
}
Thanks