Not able to get more then one results with images in WPF datagrid from Mysql Database in C#

3143 views c#
4

With this code i am able to get one image from Mysql database in C# in WPF. But i have a table in database that contaon all countries's names and their flag etc. When i query to select many or all countries folling error occur "Cannot set the initializing state more than once" at this line "bi.BeginInit()". Thanks in advance.

    string co = null;
    string na = null;
    string le = null;
    string gn = null;
    string A = "pak";
    BitmapImage bi = new BitmapImage();

    try
    {
        MySqlCommand cmd = new MySqlCommand("Select Code,Name,LifeExpectancy,GNP,flg from country where Name REGEXP '" + A + "'", connection);
        MySqlDataReader dataReader = cmd.ExecuteReader();
        while (dataReader.Read())
        {

            co = dataReader["Code"].ToString();
            na = dataReader["Name"].ToString();
            le = dataReader["LifeExpectancy"].ToString();
            gn = dataReader["GNP"].ToString();
            Byte[] bindata = (Byte[])dataReader["flg"];
            MemoryStream strm = new MemoryStream();
            strm.Write(bindata, 0, bindata.Length);
            strm.Position = 0;
            System.Drawing.Image img = System.Drawing.Image.FromStream(strm); 
            bi.BeginInit();
            MemoryStream ms = new MemoryStream();
            img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            ms.Seek(0, SeekOrigin.Begin);
            bi.StreamSource = ms; 
            bi.EndInit();
            dt.Rows.Add(co, na, le, gn, bi);
            dataGridCustomers.ItemsSource = dt.DefaultView;
        }
    }
    catch (MySqlException ex)
    {
           MessageBox.Show(ex.ToString());
    }

answered question

1 Answer

3

The reason for this is because you are creating the BitmapImage outside of the loop. So Once you get in the loop it keeps trying to set the initializing state on the same image. As your error states you can't do this. What you need to do is create a new BitmapImage on each iteration. Which can be accomplished by moving the line

BitmapImage bi = new BitmapImage();

To the inside of the loop. Unless there is a specific reason you are initializing it where you are.

string co = null;
string na = null;
string le = null;
string gn = null;
string A = "pak";

try
{
    MySqlCommand cmd = new MySqlCommand("Select Code,Name,LifeExpectancy,GNP,flg from country where Name REGEXP '" + A + "'", connection);
    MySqlDataReader dataReader = cmd.ExecuteReader();
    while (dataReader.Read())
    {
        BitmapImage bi = new BitmapImage();

        co = dataReader["Code"].ToString();
        na = dataReader["Name"].ToString();
        le = dataReader["LifeExpectancy"].ToString();
        gn = dataReader["GNP"].ToString();
        Byte[] bindata = (Byte[])dataReader["flg"];
        MemoryStream strm = new MemoryStream();
        strm.Write(bindata, 0, bindata.Length);
        strm.Position = 0;
        System.Drawing.Image img = System.Drawing.Image.FromStream(strm); 
        bi.BeginInit();
        MemoryStream ms = new MemoryStream();
        img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
        ms.Seek(0, SeekOrigin.Begin);
        bi.StreamSource = ms; 
        bi.EndInit();
        dt.Rows.Add(co, na, le, gn, bi);
        dataGridCustomers.ItemsSource = dt.DefaultView;
    }
}
catch (MySqlException ex)
{
       MessageBox.Show(ex.ToString());
}

posted this

Have an answer?

JD

Please login first before posting an answer.