How to bind Image.Source from Database ?

Endrit14Endrit14 Member ✭✭✭

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

Answers

  • ManojkumarMaliManojkumarMali USMember ✭✭✭

    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.

  • JoeMankeJoeManke USMember ✭✭✭✭✭
    edited September 2018

    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.

  • Endrit14Endrit14 Member ✭✭✭

    @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 ?

  • NMackayNMackay GBInsider, University mod

    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>();
    
  • Daxton47Daxton47 Member ✭✭

    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;
    
Sign In or Register to comment.