-1

How to use fileupload control to upload an image and save it in a table in MySQL workbench database? and how to create the connection. the name of tabel is invoicesfp

public partial class Invoices : System.Web.UI.Page
{
    MySql.Data.MySqlClient.MySqlConnection conn ;
    MySql.Data.MySqlClient.MySqlCommand cmd;
    String querystr;

    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            //save image into database
            string str = FileUpload1.FileName;
            FileUpload1.PostedFile.SaveAs(Server.MapPath(".") + "//Invoices//" + str);
            string path = "~//Invoices//" + str.ToString();
            MySqlConnection conn = new MySqlConnection("Server=127.0.0.1;Database=admindb;Uid=root;Pwd=8888;");
            MySqlCommand cmd = new MySqlCommand("insert into invoicesfp values (@v1)", conn);
            conn.Open();
            cmd.Parameters.AddWithValue("v1", TextBox1.Text);

            cmd.ExecuteNonQuery();
            conn.Close();
            Label4.Text = "Image uploaded sucessfully";
        }
        else
        {
            Label4.Text = " Please, upload the image ";
        }
    }
}
Tore Østergaard
  • 3,962
  • 3
  • 27
  • 41
y.kh
  • 51
  • 7
  • is it necessary to upload the image into your database? its waste of space i'd like to say and waste of performance, save the images in your root of application and save the address to the file into your database, so any time you needed to query it wont blow your database. if you are still insist to save your image into your database i'd suggest you look at this http://stackoverflow.com/questions/3651183/save-stream-as-image – Masoud Andalibi Dec 11 '16 at 08:26
  • Ok, how to do what you have said that "save the images in your root of application and save the address to the file into your database, so any time you needed to query it wont blow your database" , means what is the code for it?. I want the image be saved in a folder that can be opened on the website I created – y.kh Dec 11 '16 at 08:29
  • @y.kh checkout the answer. – Masoud Andalibi Dec 11 '16 at 08:46

1 Answers1

1

So here is the code i wrote for your saving images into your roots, you have to create a folder in your root of application for example upload:

            #region fileupload
            string fn = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);
            string ret = Rename.ChangeName();
            string SaveLocation = Server.MapPath("Upload") + "\\" + ret;

            try
            {
                FileUpload1.PostedFile.SaveAs(SaveLocation);
            }
            catch (Exception ex)
            {
                if (ex is ArgumentNullException || ex is NullReferenceException)
                {
                    throw ex; 
                }
            }
            string PicAddress = "~/Upload/" + ret;

            #endregion

as you can see there is a method for change name, assuming you just want to save jpg files, for other files you can use extension:

    public static string ChangeName()
    {
        return Guid.NewGuid().ToString("N") + ".jpg";
    }

Simply after these code ran, you can easily save the PicAddress as a string into your database, so make for example nvarchar field for it. anytime you want to show the image you just need to reference the address of the image to your image tag:

<img src="~/Upload/etcetc.jpg">
Masoud Andalibi
  • 3,080
  • 4
  • 15
  • 39
  • ok I'll try it, thank you a lot , and where to put the code for creating the connection toMysql database and what the code should be? – y.kh Dec 11 '16 at 09:04
  • @y.kh same as above you need to open connection and excute your insert query, something like this: SqlConnection sqlConnection1 = new SqlConnection("Your Connection String"); SqlCommand cmd = new SqlCommand(); cmd.CommandText = "insert into tablename(colname1) values(val1)"; cmd.CommandType = CommandType.Text; cmd.Connection = sqlConnection1; sqlConnection1.Open(); cmd.ExecuteNonQuey(); sqlConnection1.Close(); – Masoud Andalibi Dec 11 '16 at 09:40
  • @y.kh you're welcome =) accept the answer if it was the solution :) – Masoud Andalibi Dec 12 '16 at 06:50