1

I have a database named "Student" and a table named "GeneralStudent". This table contains just two columns named "Id" and "photo". I have already successfully inserted some data into the "GeneralStudent" table. The Photos value is binary data. But when I want to retrieve the images by the following code, then a message appears:

Parameter is not valid.

How can I solve this issue?

private void searchButton_Click(object sender, System.EventArgs e)
{
    SqlConnection con1 =new SqlConnection(@"server=RATHIN-PC\SQLEXPRESS;database=Student; integrated security=true");
    con1.Open();

    SqlDataReader myReader = null;
    SqlCommand myCommand = new SqlCommand( "select photo from GeneralStudent where id='" + searchTextBox.Text + "'", con1);

    if (searchTextBox.Text == "")
    {
        MessageBox.Show("Please Enter Studet Id.\nYou Entered Null Value\nThank You", "Error Message Window", MessageBoxButtons.OK,MessageBoxIcon.Error);
    }
    else
    {
        myReader = myCommand.ExecuteReader();
        myReader.Read();

        if (myReader.HasRows)
        {
            byte[] img = (byte[])(myReader["photo"]);

            if (img == null)
            {
                pictureBox3.Image = null;
            }
            else
            {
                MemoryStream mstrm = new MemoryStream(img);
                pictureBox3.Image = Image.FromStream(mstrm);
            }
        }
        else 
        {
            textBox1.Test= "Database is Empty";
        }
    }        
    myReader.Close();
}
ArK
  • 20,698
  • 67
  • 109
  • 136

0 Answers0