Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

Using a value in a SQLite column to get display name on submit.

$
0
0

I have a User table in my SQLite database that holds a single row of data that I am wanting to use to set variables throughout my app. This table gets rebuild every time a user logs in, just in case the account number changes in the API.

#region User class
public class User
    {
        [PrimaryKey, AutoIncrement]
        public int Id { get; set; }
        public string DisplayName { get; set; }
        public string AcctNumb { get; set; }
    }
#endregion

I am wanting to pull the DisplayName from the table to call into a JSON string once you click the submit button. Here is a stripped down version of my xaml:

    <ListView x:Name="peopleList" HasUnevenRows="true"
            Grid.Row="0">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell Height="300">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                            </Grid.RowDefinitions>
                            <StackLayout Padding="5">
                                <Label x:Name="UserDisplayName" Text="{Binding DisplayName}"/>
                            </StackLayout>
                        </Grid>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

    <Entry x:Name="newPerson"
        Grid.Row="1"
        Placeholder="Email"/>

    <Entry x:Name="password"
        Grid.Row="2"
        Placeholder="Password"/>

    <Button Text="Add Person"
        Grid.Row="3"
        Clicked="OnNewButtonClicked" />

    <Label x:Name="statusMessage"
        Grid.Row="4" />

The listView is populated at page initialize:

List people = App.UserDatabase.GetAllPeople();
peopleList.ItemsSource = people;

The UserDatabase just returns all data:

    #region Get All People
    public List<User> GetAllPeople()
    {
        try
        {
            return database.Table<User>().ToList();
        }
        catch (Exception ex)
        {
            StatusMessage = 1;
        }

        return new List<User>();
    }
    #endregion

Once the OnNewButtonClicked is clicked I am wanting to pull the listView DisplayName to put into the jsonObject.

 public async void OnNewButtonClicked(object sender, EventArgs args)
    {
        statusMessage.Text = "";
        var httpClient = new HttpClient();
        var jsonObject = new List<AddUser>();
        jsonObject.Add(new AddUser() { Email = newPerson.Text, Password = password.Text, CreatedBy =  UserDisplayName.Text}); //The UserDisplayName.Text is erroring.
        var json = JsonConvert.SerializeObject(jsonObject);
        HttpContent httpContent = new StringContent(json);
        httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
        var result = await httpClient.PostAsync(WebAPI, httpContent);
        statusMessage.Text = App.UserDatabase.StatusMessage;
    }

The UserDisplayName.Text shows not to exist in the current context. I need to add this user's display name when they add a user. How do I pull the value in the DisplayName column to populate this value automatically? Or is SQLite not the best way to go about this?

Any help on this is greatly appreciated!


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>