I need to select an image from a SQL server table and then show it in a picture box on a form.
- private void btnSearch_Click(object sender, EventArgs e)
-
{
-
SearchFunctions sf = new SearchFunctions(txtPracCode.Text,txtMRN.Text, txtFName.Text, txtLName.Text, txtDOB.Text, txtSSN.Text, 0);
-
long PersonID = Convert.ToInt32(sf.PersonSearch());
-
if (PersonID != 0)
-
{
-
sf.SetPersonID(PersonID);
-
-
if (sf.CheckPicture() != 0)
-
{
-
MessageBox.Show("Picture exists");
-
SqlConnection conn = null;
-
SqlDataReader rdr = null;
-
try
-
{
-
conn = new SqlConnection("Server=Global2;DataBase=PictureCapture;Integrated Security=True");
-
conn.Open();
-
string sqlstring = "SELECT BinaryChunk FROM Person_Picture WHERE ID=" + PersonID + ";";
-
SqlCommand cmd = new SqlCommand(sqlstring, conn);
-
-
rdr.Read();
-
if (rdr[0] != DBNull.Value)
-
{
-
pbImage.Image = (rdr[0]);
-
}
-
}
-
finally
-
{
-
if (conn != null)
-
{
-
conn.Close();
-
}
-
if (rdr != null)
-
{
-
rdr.Close();
-
}
-
}
-
}
-
-
else
-
MessageBox.Show("Picture does not exist");
-
-
}
-
else
-
{
-
MessageBox.Show("Patient does not exist");
-
}
-
}
I am getting the error on the line
pbImage.Image = (rdr[0]);
I know that it is because I cannot convert this to an image, but I don't know how to convert it so that it will work.