try { string[] validFileTypes={"bmp","gif","png","jpg","jpeg","JPG","BMP","GIF","PNG","JPEG"}; string ext = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName); bool isValidFile = false; for (int i = 0; i < validFileTypes.Length; i++) { if (ext == "." + validFileTypes[i] ) { isValidFile = true; break; } } if (!isValidFile) { Response.Write("<script language='javascript'>alert('Invailed File')</script>"); } else if(Image2.ImageUrl==null) { //delete(); Byte[] bytes = null; if (FileUpload1.HasFile) { string filename = FileUpload1.PostedFile.FileName; string filePath = Path.GetFileName(filename); Stream fs = FileUpload1.PostedFile.InputStream; BinaryReader br = new BinaryReader(fs); bytes = br.ReadBytes((Int32)fs.Length); } SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString); SqlCommand command = new SqlCommand( "INSERT INTO " + U_name.Text + " (U_name, Image) " + "Values(@U_name, @Image)", con); command.Parameters.Add("@U_name", SqlDbType.NVarChar, 20).Value = U_name.Text.Trim(); command.Parameters.Add("@Image", SqlDbType.Binary).Value = bytes; con.Open(); command.ExecuteNonQuery(); Label2.ForeColor = System.Drawing.Color.Green; Label2.Text = "Successfully"; } else { Byte[] bytes = null; if (FileUpload1.HasFile) { string filename = FileUpload1.PostedFile.FileName; string filePath = Path.GetFileName(filename); Stream fs = FileUpload1.PostedFile.InputStream; BinaryReader br = new BinaryReader(fs); bytes = br.ReadBytes((Int32)fs.Length); } SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString); SqlCommand command = new SqlCommand("UPDATE " + U_name.Text + " SET Image=@Image " +"", con); command.Parameters.Add("@Image",SqlDbType.Binary).Value = bytes; con.Open(); command.ExecuteNonQuery(); Label2.ForeColor = System.Drawing.Color.Green; Label2.Text = "UPDATE Successfully"; } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)