Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
this is my code and it work normaly i have 10 contacts and when i press button the lbl19
increase to show the next ID but when the lbl19 is 11 above contacts ID the form stop and get an error Value cannot be null i try solve it like when the project get a null value it show message say this is max of ur contacts, but didnt' work can u help me? and im getting error on ImageByte.value ik it wrong but this what i get to

What I have tried:

if (ImageByte.Value = null)
           {
               MessageBox.Show("This is max of ur contacts");
           }
           else
           {
               string sql = "select Attachments.FileData from Contacts where ID =" + lbl19.Text;//Attachments.FileData
               OleDbCommand vcom = new OleDbCommand(sql, cn);
               ImageByte = (byte[])vcom.ExecuteScalar(); //contains 20 extra bytes
               MemoryStream MemStream = new MemoryStream(ImageByte.Skip(20).ToArray()); //Read bytes starting at position 20
               Image image = Image.FromStream(MemStream); //Will work now
               pictureBox1.Image = image;
           }
Posted
Updated 3-Nov-19 1:40am
v2
Comments
Richard Deeming 5-Nov-19 15:55pm    
string sql = "select Attachments.FileData from Contacts where ID =" + lbl19.Text;

Don't do it like that!

While you're probably safe in this case, since the user (presumably) can't change the value of your label, using string concatenation to build SQL queries can and will lead to SQL Injection[^] vulnerabilities.

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

const string sql = "select Attachments.FileData from Contacts where ID = ?";
using (OleDbCommand vcom = new OleDbCommand(sql, cn))
{
    vcom.Parameters.AddWithValue("ID", lbl19.Text);
    ImageByte = (byte[])vcom.ExecuteScalar();
}


Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
Query Parameterization Cheat Sheet | OWASP[^]
Richard Deeming 5-Nov-19 15:57pm    
Also, do yourself a favour and give your controls meaningful names, rather than accepting Visual Studio's default names.

Sure, you might remember what lbl42 represents today; but in six weeks time, when you come back to edit your code, you'll have forgotten. Whereas if it's called something like lblTheAnswer, you'll at least have a clue. :)

1 solution

C#
if (ImageByte.Value = null) // *** this sets ImageByte.Value to null.
// it should be 
if (ImageByte.Value == null) // equality test, not assignment
 
Share this answer
 
Comments
Member 14630006 3-Nov-19 8:13am    
the second one the test one i got error on value
[no name] 3-Nov-19 8:15am    
But if (ImageByte.Value = null) would even not compile with c#. Anyway a 5.
Richard MacCutchan 3-Nov-19 9:31am    
I often wonder how some people manage to build code that we know the compiler would normally reject.
Member 14630006 3-Nov-19 8:17am    
how reach to the value null that error said
Richard MacCutchan 3-Nov-19 9:23am    
Change the operator to the double == as I already showed you. You really need to get these operators fixed in your head, so you avoid simple mistakes like this. It is something we have all done in the past, but reading through the code carefully is a useful method of finding the obvious errors.

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