protected void Button1_Click(object sender, EventArgs e) { SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["PlantConnectionString"].ToString()); con.Open(); SqlCommand cmd = con.CreateCommand(); cmd.CommandText = "insert into gallery (imagename,imageurl) values (@imagename,@imageurl)"; cmd.Parameters.AddWithValue("imagename", TextBox1.Text); string imgurl = ImageUpload1.PostedFile.FileName.ToString(); ImageUpload1.SaveAs(Server.MapPath("~/images/gallery/" + imgurl)); string urlimage = "~/images/gallery/" + ImageUpload1.PostedFile.FileName.ToString(); cmd.Parameters.AddWithValue("imageurl", urlimage); cmd.ExecuteNonQuery(); cmd.Dispose(); con.Close(); lblmsg.Text = "Success"; TextBox1.Text = ""; }
protected void SubmitButton1_Click(object sender, EventArgs e) { try { DateTime dob = DateTime.Parse(Request.Form[TextBox1.UniqueID]); byte[] bytes; string filename = Path.GetFileName(ImageUpload1.PostedFile.FileName); string contentType = ImageUpload1.PostedFile.ContentType; using (Stream fs = ImageUpload1.PostedFile.InputStream) { using (BinaryReader br = new BinaryReader(fs)) { bytes = br.ReadBytes((Int32)fs.Length); } } Random random = new Random(); int randomNumber = random.Next(0, 10000); string base64String = Convert.ToBase64String(bytes, 0, bytes.Length); ProfileImage.ImageUrl = "data:image/png;base64," + base64String; string connstr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString; dtb.Columns.Add("UserName", typeof(string)); dtb.Columns.Add("EmailID", typeof(string)); dtb.Columns.Add("PhoneNumber", typeof(string)); dtb.Columns.Add("FBLink", typeof(string)); //save Image to a folder if (ImageUpload1.FileName != string.Empty)//ImageUpload1.HasFile { // Your code to save the file ImageUpload1.SaveAs(Server.MapPath("~/Images/" + filename)); } //Write code for DB connection and saving rows into a table in DB using (MySqlConnection con = new MySqlConnection(connstr)) { using (MySqlCommand cmd = new MySqlCommand("INSERT INTO sociallist.sociallisttable (Name,DateofBirth,EMailID,Phone,FaceBook,ProfilePicture)VALUES (@Name,@DateofBirth,@Email,@Phone,@URI,@ProfilePicture)")) { cmd.Parameters.AddWithValue("@Name", TxUserName.Text); cmd.Parameters.AddWithValue("@DateofBirth", dob); cmd.Parameters.AddWithValue("@Email", TxEmail.Text); cmd.Parameters.AddWithValue("@Phone", TxPhone.Text); cmd.Parameters.AddWithValue("@URI", TxFB.Text); cmd.Parameters.AddWithValue("@ProfilePicture", bytes); using (MySqlDataAdapter sda = new MySqlDataAdapter()) { cmd.Connection = con; sda.SelectCommand = cmd; con.Open(); cmd.ExecuteNonQuery(); } } } dtb.Rows.Add(TxUserName.Text, TxEmail.Text, TxPhone.Text, TxFB.Text); GenerateTable(dtb); if (TxEmail.Text != string.Empty) { if (TxFB.Text != string.Empty || TxUserName.Text != string.Empty || TxPhone.Text != string.Empty) { TxUserName.Focus(); TxPhone.Text = string.Empty; TxFB.Text = string.Empty; } } } catch (Exception ex) { ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + ex.Message + "');", true); } }