locked
how to save picture to a table in database RRS feed

  • Question

  • could anyone help me how to save a picture or more to a table in a database? i use "OpenFileDialog" to get my picture, but the problem always say that my foto field does not allow null value. i change to allow null value in table definition, just the same. i change the data type of my foto field from picture to another data type, just the same. i think it is wierd if i make employee database without foto. pleas help
    Thursday, April 16, 2009 2:23 AM

Answers

  • i'm beginner here mr. ganesh, how to save picture on server? and how to store the path? any link to the tutorial you said?

    Hi,
    Please visit the links below it would be helpful for you definitely,
    http://www.atalasoft.com/KB/Article.aspx?id=10018
    http://www.shabdar.org/store-save-images-in-sql-server.html
    http://www.aspnettutorials.com/tutorials/database/Save-Img-ToDB-Csharp.aspx
    http://www.csharpfriends.com/Forums/ShowPost.aspx?PostID=33564

    As far as saving images on server is concern that varies from scenario to scenario. Sometimes you need to store images in database(s) and cache them but if your scenario is not complicated, you can keep the images on server and store image name and path in Database that will be good enough.

    Please feel free to discuss further. If you need help in writing code, please feel free to ask for that. Just paste ur effort here and experts are here to help you out :)
    Adil Mughal - MCP http://adilamughal.blogspot.com
    • Proposed as answer by Adil Mughal Thursday, April 16, 2009 4:20 AM
    • Marked as answer by winarz Thursday, April 16, 2009 4:35 AM
    Thursday, April 16, 2009 4:20 AM
  • Hi,
    You can simply load image in picture box like this:
    OpenFileDialog openFileDialog = new OpenFileDialog();
    openFileDialog.Filter = "BMP File (*.bmp)|*.bmp|JPG File (*.jpg)|*.jpg";
    
    if (openFileDialog.ShowDialog() == DialogResult.OK)
    {
    pictureBox1.ImageLocation = openFileDialog.FileName;
    }

    Adil Mughal - MCP http://adilamughal.blogspot.com
    • Marked as answer by winarz Thursday, April 16, 2009 4:55 AM
    Thursday, April 16, 2009 4:33 AM
  • I think you should serialize you picture to a binary stream and store it as a blob type. In Sql Server it should be VarBinary (MAX)
    AlexB
    • Marked as answer by winarz Thursday, April 16, 2009 4:57 AM
    Thursday, April 16, 2009 3:16 AM
  • Hi,

    If you are using sql server as your database then follow the link below..

    http://www.shabdar.org/store-save-images-in-sql-server.html
    • Marked as answer by winarz Thursday, April 16, 2009 4:35 AM
    Thursday, April 16, 2009 4:14 AM

  • But performance wise, wont it be better to save the pictures on the server and store the path in DB?
    Ganesh Ranganathan
    [Please mark the post as answer if you find it helpful]
    • Marked as answer by winarz Thursday, April 16, 2009 4:35 AM
    Thursday, April 16, 2009 3:38 AM

  • Mr. Alex i just did what you told, just the same. i can not save and when i try to add new item, error occure "column foto does not allow null", even i change it to allow null in table definition.

    this what i wrote:

    private

     

    void button1_Click(object sender, EventArgs e)

    {

       openFileDialog1.Title =

    "File Foto Pegawai";

       openFileDialog1.Filter =

    "BMP File (*.bmp)|*.bmp|JPG File (*.jpg)|*.jpg";

       openFileDialog1.FileName =

    "";

       openFileDialog1.FilterIndex = 0;

       openFileDialog1.InitialDirectory =

    "MyDocuments";

       openFileDialog1.CheckFileExists =

    true;

       openFileDialog1.CheckPathExists =

    true;

     

       if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)

      {

         fotoPictureBox.Load(openFileDialog1.FileName);

      }

    • Marked as answer by winarz Thursday, April 16, 2009 4:35 AM
    Thursday, April 16, 2009 3:47 AM
  • i'm beginner here mr. ganesh, how to save picture on server? and how to store the path? any link to the tutorial you said?
    • Marked as answer by winarz Thursday, April 16, 2009 4:35 AM
    Thursday, April 16, 2009 4:03 AM
  • i thank you all very much for the info. exploring now....

    • Marked as answer by winarz Thursday, April 16, 2009 4:35 AM
    Thursday, April 16, 2009 4:34 AM

All replies

  • I think you should serialize you picture to a binary stream and store it as a blob type. In Sql Server it should be VarBinary (MAX)
    AlexB
    • Marked as answer by winarz Thursday, April 16, 2009 4:57 AM
    Thursday, April 16, 2009 3:16 AM

  • But performance wise, wont it be better to save the pictures on the server and store the path in DB?
    Ganesh Ranganathan
    [Please mark the post as answer if you find it helpful]
    • Marked as answer by winarz Thursday, April 16, 2009 4:35 AM
    Thursday, April 16, 2009 3:38 AM

  • Mr. Alex i just did what you told, just the same. i can not save and when i try to add new item, error occure "column foto does not allow null", even i change it to allow null in table definition.

    this what i wrote:

    private

     

    void button1_Click(object sender, EventArgs e)

    {

       openFileDialog1.Title =

    "File Foto Pegawai";

       openFileDialog1.Filter =

    "BMP File (*.bmp)|*.bmp|JPG File (*.jpg)|*.jpg";

       openFileDialog1.FileName =

    "";

       openFileDialog1.FilterIndex = 0;

       openFileDialog1.InitialDirectory =

    "MyDocuments";

       openFileDialog1.CheckFileExists =

    true;

       openFileDialog1.CheckPathExists =

    true;

     

       if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)

      {

         fotoPictureBox.Load(openFileDialog1.FileName);

      }

    • Marked as answer by winarz Thursday, April 16, 2009 4:35 AM
    Thursday, April 16, 2009 3:47 AM
  • i'm beginner here mr. ganesh, how to save picture on server? and how to store the path? any link to the tutorial you said?
    • Marked as answer by winarz Thursday, April 16, 2009 4:35 AM
    Thursday, April 16, 2009 4:03 AM
  • Hi,

    If you are using sql server as your database then follow the link below..

    http://www.shabdar.org/store-save-images-in-sql-server.html
    • Marked as answer by winarz Thursday, April 16, 2009 4:35 AM
    Thursday, April 16, 2009 4:14 AM
  • i'm beginner here mr. ganesh, how to save picture on server? and how to store the path? any link to the tutorial you said?

    Hi,
    Please visit the links below it would be helpful for you definitely,
    http://www.atalasoft.com/KB/Article.aspx?id=10018
    http://www.shabdar.org/store-save-images-in-sql-server.html
    http://www.aspnettutorials.com/tutorials/database/Save-Img-ToDB-Csharp.aspx
    http://www.csharpfriends.com/Forums/ShowPost.aspx?PostID=33564

    As far as saving images on server is concern that varies from scenario to scenario. Sometimes you need to store images in database(s) and cache them but if your scenario is not complicated, you can keep the images on server and store image name and path in Database that will be good enough.

    Please feel free to discuss further. If you need help in writing code, please feel free to ask for that. Just paste ur effort here and experts are here to help you out :)
    Adil Mughal - MCP http://adilamughal.blogspot.com
    • Proposed as answer by Adil Mughal Thursday, April 16, 2009 4:20 AM
    • Marked as answer by winarz Thursday, April 16, 2009 4:35 AM
    Thursday, April 16, 2009 4:20 AM
  • Hi,
    You can simply load image in picture box like this:
    OpenFileDialog openFileDialog = new OpenFileDialog();
    openFileDialog.Filter = "BMP File (*.bmp)|*.bmp|JPG File (*.jpg)|*.jpg";
    
    if (openFileDialog.ShowDialog() == DialogResult.OK)
    {
    pictureBox1.ImageLocation = openFileDialog.FileName;
    }

    Adil Mughal - MCP http://adilamughal.blogspot.com
    • Marked as answer by winarz Thursday, April 16, 2009 4:55 AM
    Thursday, April 16, 2009 4:33 AM
  • i thank you all very much for the info. exploring now....

    • Marked as answer by winarz Thursday, April 16, 2009 4:35 AM
    Thursday, April 16, 2009 4:34 AM
  • cancel exploring....your the man mr. Adil. i'm so happy like a kid got a sweet candy. you did it. this problem stop me working and always exploring. now...i'm back to bussiness cos of you mr. Adil. you solved my problem. the beginner happy, God bless you...
    Thursday, April 16, 2009 4:55 AM
  • error occure "column foto does not allow null"

    Go to Sql Server and modify the database schema. remove NOT from the columns in Sqcript Table To query, change CREATE to MODIFY and run it. Your columns will allow nulls. You cannot do this to columns with primary keys set on them.
    AlexB
    Thursday, April 16, 2009 3:03 PM
  • Hello Winarz,
    You are most welcome man! I'm also happy to help you out :)

    Good Luck for future (Y). Don't forget to check my blog at http://adilamughal.blogspot.com :)


    Adil Mughal - MCP http://adilamughal.blogspot.com
    Thursday, April 16, 2009 3:12 PM