2

I'm using VS2013 to develop an windows form application with SQL Server database.
I have a column in database table to store the image name :
enter image description here

In my application, I creat a button to select image from my computer and save that's image to application startup path :

private void buttonX1_Click(object sender, EventArgs e)
    {
        try
        {
        OpenFileDialog dlg = new OpenFileDialog();
        dlg.Filter = "Image only. | *.jpg; *.jpeg; *png; *.gif;";
        dlg.InitialDirectory = @"E:\";
        dlg.Multiselect = false;
        string a = null;
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                string[] tmp = dlg.FileNames;
                foreach (string i in tmp)
                {
                FileInfo fi = new FileInfo(i);
                string[] xxx = i.Split('\\');
                string des = Application.StartupPath + @"\Images\" + xxx[xxx.Length - 1];
                string desfolder = Application.StartupPath + @"\Images\";
                imagename = xxx[xxx.Length - 1].ToString();
                System.IO.Directory.CreateDirectory(desfolder);
                File.Delete(des);
                imageuploaded.Image = Image.FromFile(dlg.FileName);
                //over.
                fi.CopyTo(des);
                imageList1.Images.Add(imagename, Image.FromFile(des));
                //Process.Start("explorer.exe", desfolder);
                }
                MessageBox.Show("Thành công ");
            }
        }
        catch (Exception ex) { MessageBox.Show(ex.Message); }
    }  

That code will load the image from computer, save to folder "images" in startup path and add image to imagelist1, too.
After that, I have a button to insert imagename to "Images" column (in SQL server database). I have this code to use imagelist for my grid :

public PrdMan()
    {
        InitializeComponent();
        GridPanel panel = superGridControl1.PrimaryGrid;
        GridColumn column = panel.Columns["image"];
        column.EditorType = typeof(MyGridImageEditControl);
        column.EditorParams = new object[] { imageList1, ImageSizeMode.Zoom };
        //superGridControl1.PrimaryGrid.Columns[8].Visible = false;
        //superGridControl1.PrimaryGrid.Columns[2].CellStyles = "dd/MM/yyyy";
        //styleManager1.ManagerStyle = eStyle.Metro;
        // 
        //this.Location = new Point(0, 0);
        //this.Size = Screen.PrimaryScreen.WorkingArea.Size;
    }  

And code to load grid :

this.mPhamTableAdapter.Fill(this.beautyMaDataSet.MPham);
            this.superGridControl1.PrimaryGrid.DataSource = this.beautyMaDataSet.MPham;  

My problem is : When I load image, and insert it into database's "Images" columns : It's success and grid will display image. But When I close app (after published) or stop debug (in VS) then re-open ( or debug again). The grid will not display my image
enter image description here

even though the image was still in folder :
enter image description here
And imagelist have no image in list :
enter image description here

I don't know what is my problem. Can you support me how to :
1/ Add image from PC to startup path's folder using C# and save image name to SQL server (to bind image to grid).
2/ Bind image from startup path's folder to imagelist and use it.
Thanks for support.

Community
  • 1
  • 1

1 Answers1

0

I sense two problems in your code, maybe some of them cause your problem.

First, if you load a table to a DataGridView (or something else), and update the GridView with your new image, but not write back to the database manually, the modification will not save to database.

As you said,

After that, I have a button to insert imagename to "Images" column (in SQL server database). I have this code to use imagelist for my grid

I guess one reason of your problem may relative to here.

Second, you used a dialog to select an image and copy to path "APP\Images", and store name to database. How will you do when the user run you application next time? Load the table and get image names, and find those images from the path? Please provide the relative codes to solve your problem.

BTW, if the database is local, off-line DB, and also you won't store too many records, you may consider store your image raw-data in DB, too. (I know some people do not recommend this way, but in my opinion, it's one way to solve your problem too.)

J.C
  • 633
  • 1
  • 13
  • 27