Share via


UpdateAsync not working for sqlite database

Question

Sunday, August 23, 2020 6:46 PM

Hi I am a novice and developing an IoT app wherein I need to do some crud operations This code successfully inserts into the database but does not update I need only one row in the database for my app logic
Any help is appreciated

All replies (26)

Monday, August 24, 2020 3:18 AM âś…Answered

Update instruction won't add new items to the database. Try to retrieve the items from database using:

var liv = await conn.Table<LivingRoom>().ToListAsync();

And there's no need to create a table again every time you want to manipulate the database. Refer to this doc: https://docs.microsoft.com/en-us/xamarin/get-started/quickstarts/database?pivots=windows to build a utility class to manipulate the database.


Monday, August 24, 2020 3:48 AM

thanks will try


Monday, August 24, 2020 4:13 AM

But how will I take the input from user and then update those changes, actually updateLiving.xaml contains stack layout with labels and entries can It be done using SQL command


Monday, August 24, 2020 5:21 AM

also do I need to use Inotifyproperty changed


Monday, August 24, 2020 5:38 AM

But how will I take the input from user and then update those changes This references to data-binding: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/xaml/xaml-basics/data-bindings-to-mvvm If you want to change the values dynamically, we have to implement the INotifyPropertyChanged on your view model.


Monday, August 24, 2020 6:21 AM

No i mean what argument should I pass for UpdateAsync() for a particular Id


Monday, August 24, 2020 6:28 AM

You have initialized an object with the primary key(LId) like: var app = new LivingRoom() { ... LId = 1 }; We only need to pass this app as the parameter.


Tuesday, August 25, 2020 6:43 AM

Hey I tried this but it generates an exception saying System.NullReferenceException Message=Object reference not set to an instance of an object. thanks for patiently helping


Tuesday, August 25, 2020 6:56 AM

Which line throws this exception? Have you created table before using it:

_database.CreateTableAsync<Note>().Wait();

Please do not post separate files here. If you want to post the code, try the format:


Tuesday, August 25, 2020 7:12 AM

        string sql = $"UPDATE LivingRoom SET fan='{app.fan}' WHERE LId='{app.LId}' ";
        int rows = await conn.ExecuteAsync(sql);
            `

Tuesday, August 25, 2020 7:13 AM

Is the implementation of var app thing correct


Tuesday, August 25, 2020 7:15 AM

You could use UpdateAsync api directly instead of using this SQL language.


Tuesday, August 25, 2020 7:18 AM

but that does nt work either and generates same exception


Tuesday, August 25, 2020 7:21 AM

Could you please share a sample here so that I could help you look into this issue.


Tuesday, August 25, 2020 7:32 AM

surprisingly it does nt break now but does not update either can I share u a screen recording here


Tuesday, August 25, 2020 7:43 AM

Here is forum support. If you want one to one support, try to open a support ticket here: https://support.microsoft.com/en-us/supportforbusiness/productselection?sapId=211dd84f-3474-c3c5-79bf-66db630c92a6. If the record you stored had the same primary key(LId in your case), the updating should work.


Tuesday, August 25, 2020 8:30 AM

base.OnAppearing(); var liv = await conn.Table<LivingRoom>().ToListAsync(); list.ItemsSource = liv ;


Tuesday, August 25, 2020 8:30 AM

to list async is causing exception


Tuesday, August 25, 2020 8:32 AM

Have you called the create table api at least one time before using this?


Tuesday, August 25, 2020 1:41 PM

Ya used but that is for inserting but that is on different page


Tuesday, August 25, 2020 2:24 PM

Living.fan = fans.Text; Living.light = lights.Text; Living.nightlamp = nightlamps.Text; Living.geyser = geysers.Text; Living.router = routers.Text; Living.bathroomlight = bathrooms.Text;


Tuesday, August 25, 2020 2:25 PM

I had updated this and then got the null reference exception


Tuesday, August 25, 2020 3:41 PM

I removed the tolistasync to sql command


Tuesday, August 25, 2020 3:43 PM

string s = "SELECT * FROM LivingRoom "; var liv = await conn.QueryAsync<LivingRoom>(s) list.ItemsSource = liv ;


Wednesday, August 26, 2020 5:14 AM

Hey I fixed the issue and now it works good thanks for your help you made my day


Wednesday, August 26, 2020 5:36 AM

Glad it helped you.