944,043 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 5493
  • C# RSS
You are currently viewing page 3 of this multi-page discussion thread; Jump to the first page
Oct 23rd, 2009
0
Re: Save and Retrive Image
We should probably determine why you got those null characters ( \0 stored for your "filename" in the first place. What is the code you are using to INSERT or UPDATE the "filename" field?
Reputation Points: 341
Solved Threads: 233
Posting Shark
DdoubleD is offline Offline
984 posts
since Jul 2009
Oct 23rd, 2009
0
Re: Save and Retrive Image
I used this code to save the image to the folder
C# Syntax (Toggle Plain Text)
  1. string chosen_file = "";
  2. string file = "";
  3. chosen_file = openFD.FileName;
  4. string fileName = System.IO.Path.GetFileNameWithoutExtension(chosen_file);
  5. MemberPics.Image = Image.FromFile(chosen_file);
  6. MemberPics.Image.Save(string.Format("d:\\Pictures\\{0}.jpg", fileName), System.Drawing.Imaging.ImageFormat.Jpeg);
  7. file = fileName;

Then this code to insert the filename (fileName into the mysql database

C# Syntax (Toggle Plain Text)
  1. string strSQL = "INSERT INTO memberinfo " + "(memberid,memberSname,memberFname, fileName)" + "VALUES( '" + txtmid.Text + "','" + txtmSname.Text + "','" + txtFname.Text + "', '" + file +"')";
  2.  
  3. accessDB.myCmd(strSQL).ExecuteNonQuery();
which was successful. So after I was able to save the file into the folder and the fileName(not including the weird character) into the db, I expected to find only the filename not other characters attached to the fileName
Reputation Points: 10
Solved Threads: 0
Newbie Poster
SAINTJAB is offline Offline
22 posts
since Sep 2009
Oct 23rd, 2009
0
Re: Save and Retrive Image
I am going to BUMP on this one because nothing I can see to cause that string assignment to occur that way.

I assume you added the following lines to try to elliminate the problem:

C# Syntax (Toggle Plain Text)
  1. path = path.Trim(Path.GetInvalidFileNameChars());
  2. path = path.Trim(Path.GetInvalidPathChars());

but, you shouldn't need to be doing that either given the code I've seen. Also, I have not used those calls in a string.Trim before--have you tried it without those two lines?

Anyway, I've got somewhere I need to get to... Just post back a response and someone else will surely look into this before I get back.
Reputation Points: 341
Solved Threads: 233
Posting Shark
DdoubleD is offline Offline
984 posts
since Jul 2009
Oct 23rd, 2009
0
Re: Save and Retrive Image
The column in your database that holds the filename might be a char data type column (or wchar, etc), if you change it to varchar (or nvarchar, etc) then it will let you use variable length strings and not give you all those extra zeros.

The other choice (off the top of my head) is to change line 29 to this:
C# Syntax (Toggle Plain Text)
  1. String path = ("d:\\Pictures\\" + img.Trim() + ".jpg");

Edit: (for some reason I think the char data type pads with spaces, not zeros, so the column data type couple be wrong. Also, I forget if Trim() removes zeros like this, you might need to call
C# Syntax (Toggle Plain Text)
  1. img.Trim(new char[] {'\0'})
instead which specifically removes the zeros ... I think
Last edited by mikiurban; Oct 23rd, 2009 at 4:53 pm. Reason: Disclaimer: I just ate a giant chicken parmesan, I blame all errors to impending food coma :)
Reputation Points: 27
Solved Threads: 17
Junior Poster in Training
mikiurban is offline Offline
63 posts
since Oct 2009
Oct 23rd, 2009
0
Re: Save and Retrive Image
path = path.Replace("\0", "");
Reputation Points: 10
Solved Threads: 0
Newbie Poster
SAINTJAB is offline Offline
22 posts
since Sep 2009
Oct 23rd, 2009
0
Re: Save and Retrive Image
What in the world are you doing? Try to use the code I provided you and get it working. Then modify the code to do what you want it to .. and find out where it breaks.
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Oct 23rd, 2009
1
Re: Save and Retrive Image
sknake has a point (and no, he doesn't pay me to agree with him), calling .Replace() will fix this problem for now, but what you really want to do is understand why it's happening in the first place, or else your program will have a million Replace() commands. My money is on the data stored as char instead of varchar in the DB.

I doubt this is the problem, but you might want to be careful with this line:
C# Syntax (Toggle Plain Text)
  1. string strSQL = "INSERT INTO memberinfo " + "(memberid,memberSname,memberFname, fileName)" + "VALUES( '" + txtmid.Text + "','" + txtmSname.Text + "','" + txtFname.Text + "', '" + file +"')";

If any of those text boxes have an apostrophe in there, the insert will break. I suggest using
C# Syntax (Toggle Plain Text)
  1. OleDbCommand cmd = new OleDbCommand(...);
  2. cmd.Parameters.AddWithValue(...)
(or OdbcCommand, I forget which is for MS Access) to prevent any kind of query hacking shenanigans.
Reputation Points: 27
Solved Threads: 17
Junior Poster in Training
mikiurban is offline Offline
63 posts
since Oct 2009
Oct 24th, 2009
0
Re: Save and Retrive Image
Click to Expand / Collapse  Quote originally posted by mikiurban ...
sknake has a point (and no, he doesn't pay me to agree with him), calling .Replace() will fix this problem for now, but what you really want to do is understand why it's happening in the first place, or else your program will have a million Replace() commands. My money is on the data stored as char instead of varchar in the DB.
mikiurban has hit the nail on the head. On another note you need to handle DBNull.Value . I call Convert.ToString() on all string values brought back and the NULL will equate to string.Empty .
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: how to filter strange characters?
Next Thread in C# Forum Timeline: insert combo box value into database table in oracle





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC