简体   繁体   中英

Parameter is not valid for accessing image from database through blob

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();
            }
        }

    }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM