locked
Get filename from table RRS feed

  • Question

  • Guys, i have been able to successfully save the filename called fileName to the table called memberinfo. i can see the filename in the table and also the picture with that name in the folder. Now I want to retrieve the filename from the table and add it to the folder so that my picturebox MemberPics can point to it and load an image from that file using this codes
    [code]
    string loadPic = "SELECT fileName from memberinfo";
    string fullpath = string.Format("d:\\Pictures\\{0}", loadPic);
    MemberPics.Image = Image.FromFile(fullpath);
    [/code]
    but am getting a FileNotFound error. What cud be the problem cos the filename is in the table and the picture isn also in the folder d:\\pictures. please help
    Thursday, October 22, 2009 6:35 PM

Answers

  • Use:

      path = path.Replace("\0", "");
    Hans Passant.
    • Marked as answer by SAINTJAB Friday, October 23, 2009 7:51 PM
    Friday, October 23, 2009 5:35 PM

All replies

  • Put a breakpoint on the last line you listed.  Hover your mouse over the fullpath variable.  What does it say?  That should give you a hint into it.

    My guess is that it says:

    "D:\\Pictures\\SELECT fileName from memberinfo"

    I doubt your picture is located there.

    You need to execute the query, not just assign it's text to a string variable.
    Coding Light - Illuminated Ideas and Algorithms in Software
    Coding Light WikiLinkedInForumsBrowser
    Thursday, October 22, 2009 6:39 PM
  • @David: This is exactly what I had but since am new in C# and Mysql, could you please give me a code for me to be able to get the image
    Thursday, October 22, 2009 6:51 PM
  • Can anyone please tell me how to execute the query after i have successfully retrieved the filename from the database as shown

    [code]
    string loadPic = "SELECT fileName from memberinfo";
    string fullpath = string.Format("d:\\Pictures\\{0}", loadPic);
    MemberPics.Image = Image.FromFile(fullpath);
    [/code]
    Friday, October 23, 2009 8:08 AM
  • Hi,

    If you've already been able to save the file name to the database then you should know how.

    1. You need to create a connection object (using the appropriate connection string for the server and database you are connecting to)
    2. Then create a command object and assign your command and connection to it
    3. Then call the ExecuteScalar method and cast the result to a string
    4. Finally embed the result in fullpath string using stringformat, instead of embedding the sql statement.

    The article that David linked to should explain each step in detail.

    It's hard for us to provide exact code, because even if we did we don't know the connection string to provide you etc.

    Friday, October 23, 2009 9:07 AM
  • Below is the code to read from the database into the controls i have on the form. everything is reading except the image. For that i get "Illegal characters in path" exception. can anyone help with the reader for the image?
    1. public void loaddetails( )
    2. {
    3.  
    4. MySqlDataReader reader = null ;
    5.  
    6. try
    7. {
    8. accessDB.connection ( ) ;
    9. //this.reset();
    10.  
    11. String search = "SELECT memberSname,memberFname,memberOname,membersex,fileName FROM memberInfo WHERE memberid = '" + txtmid.Text + "'" ;
    12.  
    13. accessDB.myCmd ( search) .ExecuteNonQuery ( ) ;
    14.  
    15. reader = accessDB.myCmd ( search) .ExecuteReader ( ) ;
    16.  
    17. reader.Read ( ) ;
    18. //while ()
    19. //{
    20. txtmSname.Text = reader[ "memberSname" ] .ToString ( ) ;
    21. txtFname.Text = reader[ "memberFname" ] .ToString ( ) ;
    22. txtmOname.Text = reader[ "memberOname" ] .ToString ( ) ;
    23. dateDOB.Text = reader[ "memberDOB" ] .ToString ( ) ;
    24. cmbGender.Text = reader[ "membersex" ] .ToString ( ) ;
    25. string img = reader[ "fileName" ] .ToString ( ) ;
    26.  
    27. try
    28. {
    29. String path = ( "d:\\ Pictures\\ " + img + ".jpg" ) ;
    30. path = path.Trim ( Path.GetInvalidFileNameChars ( ) ) ;
    31. path = path.Trim ( Path.GetInvalidPathChars ( ) ) ;
    32. MemberPics.Image = Image.FromFile ( path) ;
    33. }
    34. catch ( Exception ex)
    35. {
    36.  
    37. MessageBox.Show ( "Sorry, the pics could not be loaded " + ex.ToString ( ) ) ;
    38. }
    39.  
    40. reader.Close ( ) ;

    I just want to read the filename and assign it to the path
    Friday, October 23, 2009 10:58 AM
  • After running the code, I see path = "d:\\Pictures\\DA\0\0\0\0\0\0\0\0\0\0\0.jpg" instend of d:\\Pictures\\DA.jpg. Can someone help me to trim the unwanted characters from the path cos the fileName is DA so the path should read d:\\Pictures\DA.jpg. And I see only DA in the table not DA\0\0\0\0\0\0\0\0\0\0\0.
    Friday, October 23, 2009 4:18 PM
  • Use:

      path = path.Replace("\0", "");
    Hans Passant.
    • Marked as answer by SAINTJAB Friday, October 23, 2009 7:51 PM
    Friday, October 23, 2009 5:35 PM
  • @Nobugz: Thanks man. U saved my day
    Friday, October 23, 2009 7:52 PM
  • Thank you everyone for helping me out. Now I can enjoy my weekends. Thank God it's frrrridaaaaay
    Friday, October 23, 2009 7:53 PM