locked
How to bind Image.Source from Database ? RRS feed

  • Question

  • User369473 posted

    This is my code but is doesn't work ,

    My model

    C# code

    In the User table , Photo attribute is stored the path ,that I want to get !! How can i give the stored path to the Image Source

    Wednesday, September 5, 2018 11:37 AM

All replies

  • User261729 posted

    Hi, In your model, add one more property of type ImageSource. Like; public ImageSource ImgSrc { get{ return ImageSource.FromUri(new Uri(UserPhoto)); } }

    Now assign this property ImgSrc to source of your image.

    Wednesday, September 5, 2018 1:03 PM
  • User53115 posted

    conn.Table<User>.ToString() is entirely wrong. That's just going to return you something like "SQLite.Table`1[User]".

    You need to select a specific user from the database, then just grab the UserPhoto property. Then you create an ImageSource from that somehow depending on what that string actually is.

    Wednesday, September 5, 2018 1:40 PM
  • User369473 posted

    @JoeManke said: conn.Table<User>.ToString() is entirely wrong. That's just going to return you something like "SQLite.Table`1[User]".

    You need to select a specific user from the database, then just grab the UserPhoto property. Then you create an ImageSource from that somehow depending on what that string actually is.

    How to get a specific user from database ?

    Wednesday, September 5, 2018 6:36 PM
  • User76049 posted

    You really need to read the guides of components your using.

    https://developer.xamarin.com/guides/android/data-and-cloud-services/data-access/part-3-using-sqlite-orm/

    var apple = from s in db.Table<Stock>() where s.Symbol.StartsWith ("A") select s;

    var stock = db.Get<Stock>(5); // primary key id of 5 var stockList = db.Table<Stock>();

    Wednesday, September 5, 2018 7:19 PM
  • User367517 posted

    Yeah like NMackay said, reading up on the guides will be vital to your needs here. I would also suggest that you need to be able to visualize mentally what the database looks like and how you are going to extract data from it before you can actually do so. The code you have doesn't make sense if you have a clear understanding of what the database looks like. AFAIK, there is no tooling that lets you see the contents of a SQLite DB on a device, so I would suggest looking at Microsoft's SQL Management Studio, or even something like Microsoft's Access, and study up on basic query operations. As far as the solution to this problem:

    //You'll need to have the UserID to query the appropriate user.
    int userId = 1;
    
    // Extract the appropriate User (ROW) from the User Table in the DB.
    User _user = await conn.Table<User>().Where(x => x.UserID == userId).FirstOrDefaultAsync();
    
    // The UserPhoto field is a questionable name. Implies to me that you are saving an image in the DB which isn't 
    // necessarily best practice, but I digress.
    UserImage.Source = _user.UserPhoto;
    
    Wednesday, September 5, 2018 9:18 PM