Smith
Smith

Reputation: 5961

Binding Mysqldata to datagridview cause error

I have a table in mysql with a text, id and image (blob), now i need to display the data in a datagridview. This is what i did

MySqlConnection mysqlCon = new  

MySqlConnection(MySQLConnectionString);
mysqlCon.Open();

MySqlDataAdapter MyDA = new MySqlDataAdapter();
string sqlSelectAll = "SELECT * from info";
MyDA.SelectCommand = new MySqlCommand(sqlSelectAll, mysqlCon);

DataTable table = new DataTable();
MyDA.Fill(table);

dataGridView1.DataSource = new BindingSource(table,null); // Error at this line

I get the following error

enter image description here

How do i fix this

EDIT

I discovered the error is because some of the images are empty blob, when i selected all rows with images, i no longer get the error.

Is there anyway to checkmate this during binding

Upvotes: 0

Views: 186

Answers (1)

The normal behavior for a DGV Image column for a Null value is to simply show the classic broken image:

enter image description here

So be sure you don't have code somewhere doing something to cause the error. Given the invalid parameter aksi be sure that there isn't a row with corrupted/bad data in it. You can also specify a default image to use when the data is Null:

// class var for the image
private Image noImg = Properties.Resources.exclamation;
...
// set DS and specify null image for col:
dgv2.DataSource = new BindingSource(dtSample, null);
dgv2.Columns[2].DefaultCellStyle.NullValue = noImg;

enter image description here

This may not do anything to solve your problem if the data is actually corrupted or if you have code that is doing something to cause the error (like a custom formatter).

Upvotes: 2

Related Questions