简体   繁体   中英

How to bind an image into picture box from SQL Server along with a textbox and combo box on C#?

So I have a table called Bicycle inside BikeStore database on sql server, consists of the following columns: BID, BName, Description, BType, Brand, Origin, Price, Stock, BImage

Successfully bind all textboxes and one combo box with my data from sql server just by clicking cells on datagridview. Below is the code:

private void dg1_CellClick(object sender, DataGridViewCellEventArgs e)
{
        bidTxt.Text = dg1.SelectedRows[0].Cells[0].Value.ToString();
        bnameTxt.Text = dg1.SelectedRows[0].Cells[1].Value.ToString();
        bdescrichTxt.Text = dg1.SelectedRows[0].Cells[2].Value.ToString();
        typeCB.Text = dg1.SelectedRows[0].Cells[3].Value.ToString();
        brandTxt.Text = dg1.SelectedRows[0].Cells[4].Value.ToString();
        originTxt.Text = dg1.SelectedRows[0].Cells[5].Value.ToString();
        priceTxt.Text = dg1.SelectedRows[0].Cells[6].Value.ToString();
        stockTxt.Text = dg1.SelectedRows[0].Cells[7].Value.ToString();

But it still generates an error everytime I tried to load the image too. Here's the code:

        sqlconn.Open();

        SqlCommand cmd = new SqlCommand("SELECT BImage FROM Bicycle WHERE BID = " + dg1.SelectedRows[0].Cells[0].Value + "", sqlconn);
        da.SelectCommand = cmd;
        DataSet ds = new DataSet();
        byte[] mydata = new byte[0];
        da.Fill(ds, "Bicycle");
        DataRow myrow;
        myrow = ds.Tables["Bicycle"].Rows[8];
        mydata = (byte[])myrow["BImage"];
        MemoryStream stream = new MemoryStream(mydata);
        BikePic.Image = Image.FromStream(stream);

        sqlconn.Close();
}

What did I do wrong?

I'm new at this.

If it just about only retrieving the BLOB on that query then you could do something more simple :

// SqlConnection creation and opening omitted
using(SqlCommand cmd = new SqlCommand("SELECT BImage FROM Bicycle WHERE BID = " + dg1.SelectedRows[0].Cells[0].Value + "", sqlconn))
{
  byte[] mydata = (byte[])cmd.ExecuteScalar();
  MemoryStream stream = new MemoryStream(mydata);
  BikePic.Image = Image.FromStream(stream);
}

Also you might want to dispose of stream after creating the Image control but I'm not completely sure.

Problem solved! Add single quotation mark on the SqlCommand line between the blockquote as it'll search up the ID you looking for, like this:

SqlCommand cmd = new SqlCommand("SELECT BImage FROM Bicycle WHERE BID = '" + dg1.SelectedRows[0].Cells[0].Value + "'", sqlconn);

Below is the code before modified . See the difference?

SqlCommand cmd = new SqlCommand("SELECT BImage FROM Bicycle WHERE BID = " + dg1.SelectedRows[0].Cells[0].Value + "", sqlconn);

Haha silly me. Thanks anyway guys!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM