how to update a table

some1some1 Member ✭✭✭
edited May 7 in Xamarin.Android

I have inserted data to model COMPANY_MASTER in page1.xaml expect in two fields OpeningCash and AvailableCash .
i want to update is value from another page . But updation is not working . how will i update it.

this is my model **COMPANY_MASTER **

`
public class COMPANY_MASTER : INotifyPropertyChanged

{

    public event PropertyChangedEventHandler PropertyChanged;

    [PrimaryKey, AutoIncrement]

    public int CompanyId { get; set; }

    public string _name { get; set; }

    public string _address1 { get; set; }

    public string _address2 { get; set; }

    public string _address3 { get; set; }

    public string _email { get; set; }

    public string _phoneNo { get; set; }

    public string _type { get; set; }

    public string _wing { get; set; }

    public string _year { get; set; }

    public decimal _openingcash { get; set; }

    public decimal _availablecash { get; set; }

    public decimal Available_Cash
    {
        get { return _availablecash; }
        set
        {
            if (_availablecash == value)

                return;
            _availablecash = value;
            OnPropertyChanged();
        }
    }

    public decimal Opening_Cash
    {
        get { return _openingcash; }
        set
        {
            if (_openingcash == value)

                return;
            _openingcash = value;
            OnPropertyChanged();
        }
    }

    public string Name
    {
        get { return _name; }
        set
        {
            if (_name == value)

                return;
            _name = value;
            OnPropertyChanged();
        }
    }
    public string Year
    {
        get { return _year; }
        set
        {
            if (_year == value)

                return;
            _year = value;
            OnPropertyChanged();
        }
    }

    public string Wing
    {
        get { return _wing; }
        set
        {
            if (_wing == value)

                return;
            _wing = value;
            OnPropertyChanged();
        }
    }
     public string Type
    {
        get { return _type; }
        set
        {
            if (_type == value)

                return;
            _type = value;
            OnPropertyChanged();
        }
    }

    public string PhoneNo
    {
        get { return _phoneNo; }
        set
        {
            if (_phoneNo == value)

                return;
            _phoneNo = value;
            OnPropertyChanged();
        }
    }

    public string Address1
    {
        get { return _address1; }
        set
        {
            if (_address1 == value)

                return;
            _address1 = value;
            OnPropertyChanged();
        }
    }

    public string Address2
    {
        get { return _address2; }
        set
        {
            if (_address2 == value)

                return;
            _address2 = value;
            OnPropertyChanged();
        }
    }
    public string Address3
    {
        get { return _address3; }
        set
        {
            if (_address3 == value)

                return;
            _address3 = value;
            OnPropertyChanged();
        }
    }

    public string Email
    {
        get { return _email; }
        set
        {
            if (_email == value)

                return;
            _email = value;
            OnPropertyChanged();
        }
    }

    private void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}` 

in page1.xaml, code for saving data to COMPANY_MASTER is

`
private async void OnSave(object sender, EventArgs e)
{
try
{
if (pickCompnyType.SelectedIndex == 0)
{
type = "Academic";
}

else if(pickCompnyType.SelectedIndex == 1)
{
type = "Financial";

}           

if (pickYear.SelectedIndex == 0)
{
year =" April - March";
}

else if(pickYear.SelectedIndex==1)
{
year = "January - December";
}

            var cmpny = new COMPANY_MASTER
            {
                Name = ECompnyName.Text,
                PhoneNo = ECompnyPhone.Text,
                Email = ECompnyEmail.Text,
                Address1 = ECompnyAdd1.Text,
                Address2 = ECompnyAdd2.Text,
                Address3 = ECompnyAdd3.Text,
                Type = type,
                Wing = EWing.Text,
                Year =year


            };
            var checker = await _connection.InsertAsync(cmpny);
            if (checker == 1)
            {
                await DisplayAlert("Message", "Data saved successfully.", "Ok");
                await Navigation.PopAsync();
            }
        }
        catch
        {
            await DisplayAlert("Error", "Error in data saving. Please retry later.", "Ok");
        }
    }`

in page2.xaml , i wrote update like this but it doesnot work

`

try

{

var expense = new COMPANY_MASTER

            {

                Opening_Cash = Convert.ToDecimal(EOCash.Text),
                Available_Cash = Convert.ToDecimal(EACash.Text)


            };
            var checker = await _connection.UpdateAsync(expense);
            if (checker == 1)
            {
                await DisplayAlert("Message", "Data saved successfully.", "Ok");
                await Navigation.PopAsync();
            }
        }
        catch
        {
            await DisplayAlert("Error", "Error in data saving. Please retry later.", "Ok");
        }`

Best Answer

Answers

  • JarvanJarvan Member, Xamarin Team Xamurai

    Is there any exception occured? And what's the value of 'checker'?
    You can add breakpoints to debug to check the values and find which line of code cause the problem.

  • DimChrisDimChris USMember ✭✭✭

    I lost you, what are you trying to update an sqlite database? You list Collection? Your xml file?

  • some1some1 Member ✭✭✭

    @yelinzh i have added breakpoints and checked the values .
    value of checker is 0.No exception is showing .

    look this screenshot , here availablecash and opening cash has value but CompanyId is 0.
    In COMPANY_MASTER ,i have only one value in this table with CompanyId is 1 , i want to update this data corresponding to this data.

  • some1some1 Member ✭✭✭

    @DimChris i want to update openingcash and availablecash fields in COMPANY_MASTER

  • SreeeeSreeee INMember ✭✭✭✭✭

    Please go through this blog for saving and updating data to local DB.

  • DimChrisDimChris USMember ✭✭✭
    Where is the screenshot? I cant fully understand what exactly are you mean. But you need when you insert company id for first time to be setted as value 1. I can help through a remote control. Pm me if you want
  • some1some1 Member ✭✭✭
    edited May 8

    @DimChris @yelinzh
    this is screenshot

  • JarvanJarvan Member, Xamarin Team Xamurai

    Check your Model(expense). There is something wrong with database.UpdateAsync();

Sign In or Register to comment.