SQLite Query Results to Entry Boxes

Hello,

I am working on a project which requires me to query my database from the title that was tapped in the picker and then pull the full name, username and date to their respective entry boxes.

Example:

Picker on top of entry boxes displays titles, user picks one

Entry boxes below :

Title:
Title goes here
Full Name:
Full name goes here
Username:
Username goes here
Date and Time
Date goes here

The data is successfully being inserted to the database so this is the only part where I am stuck.

Any help would be great!

Thank you,

Moose!

Best Answer

  • Moose6Moose6 ✭✭
    Accepted Answer

    Hey Everyone,

    I found a solution works like a charm, see below!

            try
            {
                string entrydb = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), 
               "Entry.db");
                var db = new SQLiteConnection(entrydb);
    
    
                var getdata= db.Query<CreatedEntry>("SELECT Password FROM CreatedEntry WHERE (Title='" + 
                TitleViewList.SelectedItem + "')");
                foreach (var s in getdata)
                {
                    DisplayAlert("success", s.Password, "ok");
                }
    
            }
            catch
            {
    
            }
    

Answers

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭
    Binding
  • Moose6Moose6 Member ✭✭

    Do you have any references that show some examples?

  • Moose6Moose6 Member ✭✭
    edited June 3

    So I get that I have to bind the data but how do I get it from my db to bind it?

    Is there something in Xamarin C# like this, please see below?

            if(reportsCombo.Enabled == true)
            {
                getconnectionstring();
                // string connetionString = null;
                SqlConnection connection;
                SqlCommand command;
                string sql = null;
                SqlDataReader dataReader;
    
                sql = "SELECT     Title, Investigator, UserAssociated, IRDate, IncidentNum, Description, Summary, 
                Analysis,Conclusion,  ID FROM            IR_Template WHERE (Title='" + reportsCombo.Text + "')";
                connection = new SqlConnection(connetionString);
                try
                {
                    connection.Open();
                    command = new SqlCommand(sql, connection);
                    dataReader = command.ExecuteReader();
                    while (dataReader.Read())
                    {
                        // MessageBox.Show(dataReader.GetValue(0) + " - " + dataReader.GetValue(1)) ;// + " - " + 
                        dataReader.GetValue(2));
                        titleTXT.Text = dataReader.GetValue(0).ToString();
                        InvestigatorTXT.Text = dataReader.GetValue(1).ToString();
                        userAsscTXT.Text = dataReader.GetValue(2).ToString();
                        IRDateTXT.Text = dataReader.GetValue(3).ToString();
                        IRNumTXT.Text = dataReader.GetValue(4).ToString();
                        DescriptionTXT.Text = dataReader.GetValue(5).ToString();
                        summaryTXT.Text = dataReader.GetValue(6).ToString();
                        AnalysisTXT.Text = dataReader.GetValue(7).ToString();
                        ConclusionTXT.Text = dataReader.GetValue(8).ToString();
    
                    }
                    dataReader.Close();
                    command.Dispose();
                    connection.Close();
                }
                catch (Exception)
                {
                    MessageBox.Show("Can not open connection ! ");
                }
            }
    
  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    this is not Binding, but it could works, have you tried?

  • Moose6Moose6 Member ✭✭
    Yeah I tried it and since it's not a sqlite reader it wouldn't work. I'm still researching but that's essentially what I'm trying to do.
  • Moose6Moose6 Member ✭✭

    So I think I am getting closer :o , just have to figure out how to write each value to their entry boxes:

    string entrydb = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal),"Entry.db");
    var db = new SQLiteConnection(entrydb);

    var getdata = db.Query("SELECT Title, Username, Password, URL, Notes FROM entrydb WHERE (Title='" + TitleViewList.SelectedItem + "')");

  • yelinzhyelinzh Member, Xamarin Team Xamurai
    edited June 4

    The data is successfully being inserted to the database so this is the only part where I am stuck.

    A basic demo about date binding.
    page.xaml.cs

    public partial class Page2 : ContentPage
    {
        ObservableCollection<model> models = new ObservableCollection<model>();
        public Page2()
        {
            InitializeComponent();
    
            models.Add(new model { date = Convert.ToDateTime("05/05/2012") });
            models.Add(new model { date = Convert.ToDateTime("08/09/2016") });
            models.Add(new model { date = DateTime.Now.Date });
    
            listview.ItemsSource = models;
        }
    }
    
    public class model
    {
        public string content { get; set; }
        public DateTime date { get; set; }
    }
    

    page.xaml

    <StackLayout>
        <ListView x:Name="listview">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout>
                            <DatePicker Date="{Binding date}" />
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>
    

    Here is a sample which demonstrates a Todo list application where the data is stored and accessed in a local SQLite database. Binding is involved in this demo.

  • Moose6Moose6 Member ✭✭
    Accepted Answer

    Hey Everyone,

    I found a solution works like a charm, see below!

            try
            {
                string entrydb = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), 
               "Entry.db");
                var db = new SQLiteConnection(entrydb);
    
    
                var getdata= db.Query<CreatedEntry>("SELECT Password FROM CreatedEntry WHERE (Title='" + 
                TitleViewList.SelectedItem + "')");
                foreach (var s in getdata)
                {
                    DisplayAlert("success", s.Password, "ok");
                }
    
            }
            catch
            {
    
            }
    
Sign In or Register to comment.