0

I am working with Windows Form Application and I have SQL Database with table members and the columns: id, fname, lname, gender, telephone, created, email and picture (the pictures are saved . I am using bound DataGrid with all the columns except the id column. I want the picturebox to show the image from the selected row, I tried everything that I found online but nothing.

This is the table query:

CREATE TABLE [dbo].[members](
[id] [int] IDENTITY(1,1) NOT NULL,
[fname] [varchar](50) NOT NULL,
[lname] [varchar](50) NOT NULL,
[gender] [varchar](10) NOT NULL,
[telephone] [varchar](20) NOT NULL,
[created] [date] NULL,
[picture] [image] NULL,
[email] [varchar](50) NULL,

CONSTRAINT [PK__members__3213E83F00551192] PRIMARY KEY CLUSTERED

And this is the code for showing the columns:

DataGridViewRow row = dataGridView1.Rows[e.RowIndex];

        nameText.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
        lnameText.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
        genderText.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
        telephoneText.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString();
        datecreaText.Text = dataGridView1.CurrentRow.Cells[4].Value.ToString();
        emailText.Text = dataGridView1.CurrentRow.Cells[5].Value.ToString();



        var data = (Byte[])(row.Cells[6].Value);
        var stream = new MemoryStream(data);
        pictureBox1.Image = Image.FromStream(stream);

Can someone help me 3 days into this problem and I can't solve it.

moxydoxy
  • 3
  • 2
  • Why are you taking image data from row and not from `dataGridView1.CurrentRow.Cells[6].Value`? – Samvel Petrosov Apr 27 '18 at 15:11
  • 1
    Probably not the issue, but [Image](https://learn.microsoft.com/en-us/sql/t-sql/data-types/ntext-text-and-image-transact-sql?view=sql-server-2017) type not recommended. If you can you should probably use varbinary. – Jacob H Apr 27 '18 at 15:11
  • 1
    Seems like you should be working with the datasource rather than the user interface display element. Note that `I have SQL Database` says nothing - a great many DBs use SQL. Please read [ask] and take the [tour] – Ňɏssa Pøngjǣrdenlarp Apr 27 '18 at 15:13
  • So, is there a `row.Cells[6].Value` ? Use the debugger to check its size etc..! – TaW Apr 27 '18 at 15:15

0 Answers0