locked
Image data out of an SQL Database into an pictureBox RRS feed

  • Question

  • Hello love community, As the title says, I loaded an image from a SQL database into a Picturebox via bindingsource,

    which usually works, the image in the database does not work. Does somebody has any idea? with friendly greetings Pascal





    • Edited by Pascal_1991 Wednesday, May 23, 2018 11:02 AM
    Wednesday, May 23, 2018 10:38 AM

Answers

  • I think you are better off assking in a .NET forum about this. The only thing I can say from an SQL Server perspective is that:
                cmd = new SqlCommand("select Articleimage from Article where ArticleID='" + txtAID.Text + "'", con);

    should be:

        cmd = new SqlCommand("select Articleimage from dbo.Article where ArticleID= @articleid, con);
        cmd.Parameters.Add("@articleid", SqlDbTypes.VarChar, 20).Value = txtAID.Text;

    You should never build SQL strings from input, but always use parameterised statements. This is easier, safer (no risk for SQL injection) and is nicer to SQL Server as it does not pollute the caceh.

    • Proposed as answer by Xi Jin Wednesday, May 30, 2018 9:44 AM
    • Marked as answer by Olaf HelperMVP Thursday, June 14, 2018 10:38 AM
    Thursday, May 24, 2018 9:39 PM

All replies

  • Hello Pascal,

    This is an English spoken Forum.

    Which programming language and what exactly do you mean with don't work; do you get an error message?


    Olaf Helper

    [ Blog] [ Xing] [ MVP]

    Wednesday, May 23, 2018 10:42 AM
  • i mean i want to get out an image data from my sql database and this i want to realize with the bindingsource this is the code i have momentally.:
                try
                {
                    using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "JPEG|*.jpg", ValidateNames = true, Multiselect = false })
                    {
                        if (ofd.ShowDialog() == DialogResult.OK)
                            Image.FromFile(ofd.FileName);
    
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
    
                    throw;
                }


    Wednesday, May 23, 2018 10:52 AM
  • i mean i want to get out an image data from my sql database and this i want to realize with the bindingsource this is the code i have momentally.:
                try
                {
                    using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "JPEG|*.jpg", ValidateNames = true, Multiselect = false })
                    {
                        if (ofd.ShowDialog() == DialogResult.OK)
                            Image.FromFile(ofd.FileName);
    
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
    
                    throw;
                }


    Looks like .Net programming

    see if this helps

    https://www.c-sharpcorner.com/UploadFile/8ea152/fetch-image-from-database-in-windows-form-application/


    Please Mark This As Answer if it solved your issue
    Please Vote This As Helpful if it helps to solve your issue
    Visakh
    ----------------------------
    My Wiki User Page
    My MSDN Page
    My Personal Blog
    My Facebook Page

    Wednesday, May 23, 2018 11:11 AM
  • Is there not a simply solution ?

    Now he shows me this error Code:

    "Unavailable Parameter."

                SqlCommand cmd;
                con.Open();
    
                cmd = new SqlCommand("select Articleimage from Article where ArticleID='" + txtAID.Text + "'", 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][0]); 
    
                    pictureBox.Image = new Bitmap(ms); // unavailable parameter
    
    

    Thursday, May 24, 2018 12:17 PM
  • I think you are better off assking in a .NET forum about this. The only thing I can say from an SQL Server perspective is that:
                cmd = new SqlCommand("select Articleimage from Article where ArticleID='" + txtAID.Text + "'", con);

    should be:

        cmd = new SqlCommand("select Articleimage from dbo.Article where ArticleID= @articleid, con);
        cmd.Parameters.Add("@articleid", SqlDbTypes.VarChar, 20).Value = txtAID.Text;

    You should never build SQL strings from input, but always use parameterised statements. This is easier, safer (no risk for SQL injection) and is nicer to SQL Server as it does not pollute the caceh.

    • Proposed as answer by Xi Jin Wednesday, May 30, 2018 9:44 AM
    • Marked as answer by Olaf HelperMVP Thursday, June 14, 2018 10:38 AM
    Thursday, May 24, 2018 9:39 PM