0

I'm creating project in c# desktop application. I want to add functionality like when I select row from datagrid view then image from database is shown into pichure box. but there is some error like :

"Parameter is not valid"

my code is ..

private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView1.SelectedRows)
            {
                // display content
                string value1 = row.Cells[0].Value.ToString();
                string value2 = row.Cells[1].Value.ToString();
                label2.Text = value1;
                label4.Text = value2;

            //Display Image
            SqlConnection cn = new SqlConnection();
            string str = "Data Source=.\\SQLEXPRESS;AttachDbFilename=D:\\PROJECT\\NEW\\CASTING CALCULATING SYSTEM\\CASTING CALCULATING SYSTEM\\DB_CASTING.MDF;Integrated Security=True;Connect Timeout=30;User Instance=True;";
            cn.ConnectionString = str;
            SqlCommand cmd = new SqlCommand ();
            cmd.Connection = cn;
            string strsql = "select image from EmpMaster WHERE Fname = '" +value2+ "'";
            cmd.CommandText = strsql ;
            cn.Open();
            SqlDataReader dr;

            try
            {

              dr = cmd.ExecuteReader();
              if (dr.Read())
              {
                 byte[] picarr = (byte[])dr["image"];
                 MemoryStream ms = new MemoryStream(picarr);
                 ms.Seek(0, SeekOrigin.Begin);
                 pictureBox1.Image = Image.FromStream(ms);
              }
            }
            catch (Exception ex)
            {
               MessageBox.Show(ex.Message);
            }
            finally
            {
              cn.Close();
            }
        }

    }
4
  • 2
    Change the Query to utilize Parameters use the syntax cmd.Paramater.AddwithValue("@ParamName", Paramvale); which will be value2 Also a question many will ask and that is Have you stepped through the code with the Debugger and what are the values of value1 & value1 Commented Apr 1, 2013 at 14:34
  • 1
    also in your code.. you have SqlDataReader dr where are you creating a NEW Instance of SqlDataReader change that line to either wrap it around a using(){} or change it to SqlDataReader dr = new(SqlDataReader(); also put in some code to properly Dispose of that newly created Object Instance all of the code above the try needs to be wrapped in a using(){} as well in my opinion Commented Apr 1, 2013 at 14:43
  • DJ KRAZE's point about using parameters is a good one. You appear open to SQL injection exploits with the current code. Commented Apr 1, 2013 at 14:52
  • Agreed, parameters == good, concatenating text == BAD Commented Apr 1, 2013 at 14:55

1 Answer 1

0

It is most likely a memory cap issue, see this thread for an example.

So what that says is basically that the issue is on the Image side, not your SQL. It might be different reasons than pure memory, there are many reasons the picture constructor could fail.

Also, DJ KRAZE is right, you need to do some work on your SQL queries. As a minimum use parameters (significantly reduces the risk of SQL injection attacks) and put that SqlConnection and SqlDataReader into a using block to ensure they are properly disposed.

Sign up to request clarification or add additional context in comments.

2 Comments

i was trying to create bitmap but i m failed for create bitmap. but when i change input type(in database i stored as an jpeg image) they then no output is come...
My first suggestion would then be to save the image to disk and confirm that you're reading it properly from the database. Have you confirmed that the stored BLOB data is a proper image? I typically create a small Crystal report linked to the BLOB data, that way I know the image data is fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.