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["constr"].ConnectionString; using (SqlConnection con = new SqlConnection(constr)) { string query = "insert into tblFiles values (@Name, @ContentType, @Data)"; using (SqlCommand cmd = new SqlCommand(query)) { cmd.Connection = con; cmd.Parameters.AddWithValue("@Name", filename); cmd.Parameters.AddWithValue("@ContentType", contentType); cmd.Parameters.AddWithValue("@Data", bytes); con.Open(); cmd.ExecuteNonQuery(); con.Close(); } } } } Response.Redirect(Request.Url.AbsoluteUri); } private void BindGrid() { string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString; using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand()) { cmd.CommandText = "select Id,Name,Data from tblFiles"; cmd.Connection = con; con.Open(); GridView1.DataSource = cmd.ExecuteReader(); GridView1.DataBind(); con.Close(); } } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)