Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
im trying to display image from any access database to a picturebox1 but picturebox1 is empty not showning anything is what im doing right or there other way to do it

What I have tried:

C#
OleDbConnection cn = new OleDbConnection();
string a = textBox5.Text;
string b = textBox6.Text;
string c = textBox7.Text;
cn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + a;
OleDbCommand cmd = new OleDbCommand();

cn.Open();



cmd.Connection = cn;


cmd.CommandText = "Select "+c+"  from "+b+" where y="+label9.Text;
OleDbDataReader reader = cmd.ExecuteReader();

DataTable tbl = new DataTable();
DataRow drw;
for (int i = 1; i < tbl.Rows.Count -1; i++)
{
    drw = tbl.Rows[i];
    Byte[] content = new Byte[0];
    content = (Byte[])(drw["image"]);
    MemoryStream stream = new MemoryStream(content);
    pictureBox1.Image = Image.FromStream(stream);
}
while (reader.Read())
{

    label1.Text = reader.GetString(reader.GetOrdinal("name"));


}


cn.Close();
Posted
Updated 26-Oct-19 8:27am
v2

Don't do it like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

When you have fixed that throughout your app, start looking at this - but look closely at what you are doing. Where do you get the image data from? tbl is a new DataTable - it contains no rows, so the count will always be zero.
You don't do anything useful with your DataReader, either!

And please, do yourself a favour: stop using Visual Studio default names for everything - you may remember that "TextBox8" is the mobile number today, but when you have to modify it in three weeks time, will you then? Use descriptive names - "tbMobileNo" for example - and your code becomes easier to read, more self documenting, easier to maintain - and surprisingly quicker to code because Intellisense can get to to "tbMobile" in three keystrokes, where "TextBox8" takes thinking about and 8 keystrokes...
 
Share this answer
 
Comments
Member 14630006 26-Oct-19 11:21am    
thanks for helping! , ik what u mean im testing the project now this not a main code so i use anything to get to image then i will organize my code and im using a test database right now to see what problem i will have
OriginalGriff 26-Oct-19 11:24am    
Don't do that: Always assume that what you are writing is "real code" - because it will be, you won't always get back to revise it once it works. And then you have to deal with the crapshow that results from code you knew how to do right but didn't, and that can take days or even weeks in the real world.
Member 14630006 26-Oct-19 11:32am    
thanks a lot i will try that

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900