none
C# code to load image from SQL Server database into a picture box RRS feed

  • Question

  • i have a form that stores data to a sqlserver databse. i am trying to code the search button whereby when one clicks the search button an inputbox will appear and prompt the user to enter the team code(teamcode is the primary key of the sql server database table) then it will search the records from the database and show them in the appropriate textboxes in the user interface as shown below.

    i have written a code to try and search for the records from the database and display them but i am having trouble how to load the image in the picturebox. the code is working well so all i need is the code to display the image.here is the code i am using please help!

     private void btnsearch_Click(object sender, EventArgs e)
            {
                string search;
                MemoryStream ms = new MemoryStream();
                SqlDataReader dr;
                SqlConnection connection = new SqlConnection("Server = COLLOHDICEY-PC; Database = football; Integrated security = true");
                connection.Open();
                 search= Microsoft.VisualBasic.Interaction.InputBox("ENTER THE TEAM CODE", "Please fill in the field below");
                SqlCommand cmd = new SqlCommand("select * from teamreg where TEAMCODE = ' " + search + " ' ", connection);
                dr = cmd.ExecuteReader();

                if (dr.HasRows)
                {
                    while ((dr.Read()))
                    {
                        txttname.Text = dr["TNAME"].ToString();
                        txtmname.Text = dr["TMANAGER"].ToString();
                        txtplayers.Text = dr["TPLAYERS"].ToString();
                        txttcode.Text = dr["TEAMCODE"].ToString();
                        txtcoach.Text = dr["TCOACH"].ToString();
                        txtreferee.Text = dr["TREFEREE"].ToString();
                        txtfield.Text = dr["TFIELD"].ToString();
                        txtsponsor.Text = dr["TSPONSOR"].ToString();
                       // pictureBox1.Image = dr["TLOGO"] 
                    }
                }
                else
                {
                    MessageBox.Show("No Record Found");
                }
                connection.Close();
            }


    msdn account collohdicey

    Tuesday, April 17, 2018 5:02 PM

Answers

  • Hi collohdicey,

    Thank you for posting here.

    I make a simple example for your reference.

    I insert a image to database using T-SQL.

    insert into tableName (ImageColumn) 
    SELECT BulkColumn 
    FROM Openrowset( Bulk 'image..Path..here', Single_Blob) as img

    And use the code below to show the image in PictureBox.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Data.SqlClient;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace WindowsForms
    {
        public partial class load_PictureBox_image_from_database : Form
        {
            public load_PictureBox_image_from_database()
            {
                InitializeComponent();
            }
    
            SqlConnection con;
            SqlCommand cmd;
    
            private void button1_Click(object sender, EventArgs e)
            {
                pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
                con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=" + @"D:\VS2017 Project\WindowsForms\WindowsForms\Database.mdf;" + "Integrated Security=True");
                con.Open();
                cmd = new SqlCommand("select Image from Image where id=1", con);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    MemoryStream ms = new MemoryStream((byte[])ds.Tables[0].Rows[0]["Image"]);
                    pictureBox1.Image = new Bitmap(ms);
    
                }
            }
        }
    }
    

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by collohdicey Wednesday, April 18, 2018 11:04 AM
    Wednesday, April 18, 2018 8:33 AM
    Moderator

All replies

  • Hello,

    I have a MSDN code sample which can assist. There are samples for getting and inserting images. 


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, April 17, 2018 6:05 PM
    Moderator
  • still not working!

    msdn account collohdicey

    Wednesday, April 18, 2018 8:31 AM
  • Hi collohdicey,

    Thank you for posting here.

    I make a simple example for your reference.

    I insert a image to database using T-SQL.

    insert into tableName (ImageColumn) 
    SELECT BulkColumn 
    FROM Openrowset( Bulk 'image..Path..here', Single_Blob) as img

    And use the code below to show the image in PictureBox.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Data.SqlClient;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace WindowsForms
    {
        public partial class load_PictureBox_image_from_database : Form
        {
            public load_PictureBox_image_from_database()
            {
                InitializeComponent();
            }
    
            SqlConnection con;
            SqlCommand cmd;
    
            private void button1_Click(object sender, EventArgs e)
            {
                pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
                con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=" + @"D:\VS2017 Project\WindowsForms\WindowsForms\Database.mdf;" + "Integrated Security=True");
                con.Open();
                cmd = new SqlCommand("select Image from Image where id=1", con);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    MemoryStream ms = new MemoryStream((byte[])ds.Tables[0].Rows[0]["Image"]);
                    pictureBox1.Image = new Bitmap(ms);
    
                }
            }
        }
    }
    

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by collohdicey Wednesday, April 18, 2018 11:04 AM
    Wednesday, April 18, 2018 8:33 AM
    Moderator
  • @Wendy Zang thanks very much its working now.

    msdn account collohdicey

    Wednesday, April 18, 2018 11:04 AM