Reputation: 1
I have a problem to Display image in pictureBox from Datagrid. i used the sqlconnection and get the datas using using sqldatareader. I saved the picture in systembyte format in sql server but i cant display the picture when i click the row in datagrid
please assist me
LOAD THE VALUES FROM READER TO DATAGRID
oCon.Open();
SqlCommand get_company_histroy = new SqlCommand("sp_select_company_history", oCon);
get_company_histroy.CommandType = CommandType.StoredProcedure;
get_company_histroy.Parameters.Add("@Company_Code ",txtdetailcompcode.Text);
oDr = get_company_histroy.ExecuteReader();
ArrayList sequence = new ArrayList();
while (oDr.Read())
{
Get_Histroy His = new Get_Histroy();
His.Photo = oDr[6].ToString();
sequence.Add(His);
//txtcompdetailhisfounder.Text = Convert.ToString(oDr["History_Founder"]);
//DTP_comp_details_his_since.Text=Convert.ToString (oDr["History_Since"]);
}
DG_Mas_Com_History.DataSource = sequence;
CLASS FOR GETTING VALUES FROM READER
public class Get_Histroy
{
public string Photo
{
get { return History_Photo; }
set { History_Photo=value; }
}
}
DATAGRID CLICK EVENT
private void DG_Mas_Com_History_CellClick(object sender, DataGridViewCellEventArgs e)
{
try
{
if (e.RowIndex >= 0)
get_company_histroy.Parameters.Add("@Company_Code ", txtdetailcompcode.Text);
oDr = get_company_histroy.ExecuteReader();
byte[] picarr = (byte[])DG_Mas_Com_History.Rows[e.RowIndex].Cells[4].Value;
ms = new MemoryStream(picarr);
ms.Seek(0, SeekOrigin.Begin);
PBcompdetailhisphoto.Image = System.Drawing.Image.FromStream(ms);
}
I am getting error :
Unable to cast object of type 'System.String' to type 'System.Byte[]'.)
from line
byte[] picarr = (byte[])DG_Mas_Com_History.Rows[e.RowIndex].Cells[4].Value;
Upvotes: 0
Views: 1459
Reputation: 3956
This line is problematic:
His.Photo = oDr[6].ToString();
You need to get the image from database in bytes, see here Getting binary data using SqlDataReader
Upvotes: 0