private void btnCARI_Click(object sender, EventArgs e)
{
tes.Open();
SqlCommand cmdSelect = new SqlCommand("SELECT gambar FROM tblBARANG WHERE id= '" + txtCARI.Text + "'", tes);
byte[] barrImg = (byte[])cmdSelect.ExecuteScalar();
if (barrImg != null)
{
string strfn = Convert.ToString(DateTime.Now.ToFileTime());
FileStream fs = new FileStream(strfn,FileMode.CreateNew, FileAccess.Write);
fs.Write(barrImg, 0, barrImg.Length);
fs.Flush();
fs.Close();
tes.Close();
pboxGAMBAR.Image = Image.FromFile(strfn);
}
else
{
pboxGAMBAR.Image = null;
}
tes.Close();
}
Asked
Active
Viewed 52 times
0
-
You dont need to create an intermediate file and one using a date string will not work, You can create a bitmap from the `byte[]` read. No "trick" – Ňɏssa Pøngjǣrdenlarp Dec 17 '16 at 15:54
1 Answers
0
private void btnCARI_Click(object sender, EventArgs e) { try { tes.Open();
//Retrieve BLOB from database into DataSet.
Com = new SqlCommand();
Com.Connection = tes;
Com.CommandText = ("select gambar from tblBARANG where no=@no");
Com.Parameters.AddWithValue("@no", Convert.ToInt32(txtCARI.Text));
var da = new SqlDataAdapter(Com);
var ds = new DataSet();
da.Fill(ds, "tblBARANG");
int count = ds.Tables["tblBARANG"].Rows.Count;
if (count > 0)
{
var data = (Byte[])(ds.Tables["tblBARANG"].Rows[count - 1]["gambar"]);
var stream = new MemoryStream(data);
pboxGAMBAR.Image = Image.FromStream(stream);
}
tes.Close();
}
catch (Exception ex)
{ MessageBox.Show(ex.Message); }
}
Error, parameter is not valid

nazaro
- 9
- 1