1

an error shows saying that I have invalid parameters for this code... can anyone tell me whats wrong? Im supposed to get the image assigned to the clientID.

private void button1_Click(object sender, EventArgs e)
{
        MySqlConnection conn = new MySqlConnection(mycon);
        MySqlCommand cmd = new MySqlCommand("SELECT clientImage FROM client WHERE clientID='" + label2.Text + "'", conn);

        conn.Open();
        MySqlDataReader myReader = null;
        myReader = cmd.ExecuteReader();

        while (myReader.Read())
        {
            byte[] imgg = (byte[])(myReader["clientImage"]);
            if (imgg == null)
            {
                pictureBox1.Image = null;
            }
            else
            {
                MemoryStream mstream = new MemoryStream(imgg);
                pictureBox1.Image = System.Drawing.Image.FromStream(mstream);
            }
        }
        conn.Close();
}
2
  • At which line do you get the error? Commented Aug 24, 2015 at 14:34
  • SQL Injection alert - you should not concatenate together your SQL statements - use parametrized queries instead to avoid SQL injection Commented Aug 24, 2015 at 14:57

3 Answers 3

1

This piece of code might come in handy. I have tried it.

byte[] imagedata = (byte [])dataGridView1[4, dataGridView1.SelectedRows[0].Index].Value;
            using (System.IO.MemoryStream ms = new System.IO.MemoryStream(imagedata, 0, imagedata.Length))
            {
                ms.Write(imagedata, 0, imagedata.Length);
                //Set image variable value using memory stream.
                image = Image.FromStream(ms, true );
            }
Sign up to request clarification or add additional context in comments.

Comments

0

this code works. MySql Wamp.

using System.IO;

string appPath = Path.GetDirectoryName(Application.Executable) + @"/student Images/";
string imagename;

//put this code below to load form.
getimage();
if(File.Exist(appPath + imagename)
{
PictureBox1.Image = Image.FromFile(appPath + imagename);
}
else
{
PictureBox1.Image = Properties.Resources.Image_notfound;
}

private void getimage()
{
MySqlConnection connect = new MySqlConnection(con);
MySqlCommand cmd = new MySqlCommand("SELECT PictureName as 'pic' FROM userprofile where ID='" + datagrid.CurrentRow.Cells["ID"].ToString() + "'");
cmd.CommandType = CommandType.Text;
cmd.Connection = connect;
connect.Open();
Try
{
MySqlDataReader dr = cmd.ExecuteReader();
while(dr.Read())
{
imagename = dr.GetString("pic");
}
dr.Close();
}
catch(Exception ee)
{
Console.WriteLine(ee.ToString());
}
finally
{
connect.Close();
}

Comments

0

Controller

using System.IO;

using (TESTDBEntities db = new TESTDBEntities())
{
   // first create var 
    var item = (from d in db.Hunger_tbl_MainCategory select d).ToList();
    return View(item); // your view
}

View

@model List<WebApplication1.Models.TEST_tbl_IMG`enter code here`>

@foreach (var item in Model)
{
    <div class="cards-list">
        <div class="card 1">
            @{ 
                var base64 = Convert.ToBase64String(item.CategoryImage);
                var imgsrc = string.Format("data:image/png;base64,{0}", base64);
            }
            <div class="card_image"> <img src='@imgsrc'/></div>
        </div>
    </div>
}

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.