Image from Access database to C# picturebox

The database won't be giving you a stream so I expect you need to load the data into a byte array first and then copy that to the an image. Once you have the array use:

Dim mybit As Image = Byte_To_Image(Image_Data(j))

Public Function Byte_To_Image(ByRef ByteArray() As Byte) As Image
Dim ImageStream As MemoryStream
Try
If ByteArray.GetUpperBound(0) > 0 Then
ImageStream = New MemoryStream(ByteArray)
Byte_To_Image = Image.FromStream(ImageStream)
Else
Byte_To_Image = Nothing
End If
Catch ex As Exception
Byte_To_Image = Nothing
End Try
End Function
 
I somehow found an easier way. :)

picLaptops.Image = Image.FromFile(ds1.Tables["Laptops"].Rows[inc].ItemArray.GetValue(2).ToString());

I do however have a new question.

The tutorial for finding records at the same site only searches one field and displays one thing in a messagebox.

http://www.homeandlearn.co.uk/csharp/csharp_s12p12.html

I worked out how to display multiple stuff on new lines

MessageBox.Show("Maker - " + (dr1[1].ToString() + "\n" + "Model - " + (dr1[2].ToString() + "\n" + "Current Price - £" + (dr1[3].ToString()))));

How do I search all fields? (eg I will only get a result from typing in the Maker and not price or model number) Relevant code is this.

string searchFor = txtSearch.Text;
returnedRows = ds1.Tables["Laptops"].Select("Maker='" + searchFor + "'");
 
Last edited:
I tried this,

returnedRows = ds1.Tables["Laptops"].Select("Maker='" + "ID='" + searchFor + "'");

Where ID and Maker are rows. It fails. I have tried different combinations of using AND aswell. Nothing works.

*searchFor is equal to a textbox where the user inputs a word to check against database*
 
Last edited:
So are you allowing the user to enter a search criteria, and this might be EITHER a Maker name or an ID, and you want your query to return both?

If you take your earlier query, the text within the Select() looks something like this:
Maker='<searchFor>'

To expand on this and allow a second term to be matched, you'd want to construct the criteria like (assuming your ID columns contain or can match against strings):
Maker='<searchFor>' or ID='<searchFor>'

For that, you'd want to have your code concatenate the pieces that make up the criteria like this:

returnedRows = ds1.Tables["Laptops"].Select("Maker='" + searchFor + "' OR ID='" + searchFor + "'");

If that's not exactly what you're after, compare the code above with what I expect it to generate, so you can adapt it correctly for your needs.

Just for comparison purposes, your own code would currently produce this, which isn't likely what you intended:
Maker='ID='<searchFor>'
 
I got it working. :)

I however have a new issue. I am trying to implement a Skip function on the ID field.

I have this for my skip button.

int id = Int32.Parse(txtSkip.Text);

DataRow row = ds1.Tables["Laptops"].Rows.OfType<DataRow>()
.SingleOrDefault(r => (int)r.ItemArray[0] == id);

processRow(row);

process row is this.

void processRow(DataRow row)
{
txtMaker.Text = row.ItemArray.GetValue(1).ToString();
txtModel.Text = row.ItemArray.GetValue(2).ToString();
txtPrice.Text = row.ItemArray.GetValue(3).ToString();
txtBids.Text = row.ItemArray.GetValue(4).ToString();
txtScreen.Text = row.ItemArray.GetValue(5).ToString();
txtCPU.Text = row.ItemArray.GetValue(6).ToString();
txtMemory.Text = row.ItemArray.GetValue(7).ToString();
txtHD.Text = row.ItemArray.GetValue(8).ToString();
picLaptops.Image = Image.FromFile(row.ItemArray.GetValue(9).ToString());
}

That code will show the information the user enters in the textboxes (from the ID number) but the problem is, it does not update the inc variable which still holds 0 when you clicked the Next Item button. For example, if I type in an ID of 7 (7th in the list) and then click Next, I will be taken to the 2nd item in the list since Next still thinks I am on row 1. How can I make the inc update to what ID I skip to? So that in the example given it would go to record 8 when I click Next?
 
Last edited:
Back
Top Bottom