1
Answer

Database not updating by update query SQLITE

Aditya Patil

Aditya Patil

10y
755
1
using my below right code my database not getting updated WHy please tell me problem and solution

Code:

private void Update_Click(object sender, EventArgs e)
        {
            if (flag1 == true)
            {
                try
                {
                    // here we updating Notes i.e. Text Filed using Date filed. before that we have to check row present or not
                    var testdata = dbConn.Query<Task>("select * from task where Id='" + tempid + "'").FirstOrDefault();
                    // Check result is empty or not
                    if (testdata == null)
                        MessageBox.Show("ID Not Present in DataBase");
                    else
                    {
                        var tp = dbConn.Query<Task>("update task set Date= '" + dpkDate.Value.Value.ToShortDateString()
                                + "', Time='" + tpkDate.Value.Value.ToShortTimeString() //SQLite Exception coming at this stage as per SQLITE Exception message
                                + "', Text='" + TextField.Text
                                + "', Priority='" + ((ListBoxItem)priority.SelectedItem).Content.ToString()
                                + "' where Id = '" + id + "'").FirstOrDefault();
                        // Update Database
                        dbConn.Update(tp);
                        Update_List();
                        MessageBox.Show("Database Updated");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }

        }

above code written on Update Button click event

Code:

private void Update_List()
        {
            // Retrieve the task list from the database.
            List<Task> retrievedTasks = dbConn.Table<Task>().ToList<Task>();
            // Clear the list box that will show all the tasks.
            TaskListBox.Items.Clear();
            foreach (var t in retrievedTasks)
            {
                TaskListBox.Items.Add(t);
            }
        }

This method is called from Update.
Below is table Task
Code:

  // Task class representing the Task table. Each attribute in the class become one attribute in the database.

        public sealed class Task
        {
            /// <summary>
            /// You can create an integer primary key and let the SQLite control it.
            /// </summary>
            [PrimaryKey, AutoIncrement]
            public int Id { get; set; }

            public String Date { get; set; }

            public String Time { get; set; }

            public string Text { get; set; }

            public String Priority { get; set; }

            public override string ToString()
            {
                return "------------------------------------\n" + Id + "\n" + Date + "\n" + Time + "\n" + Text + "\n" + Priority + "\n------------------------------------ ";
            }
        }
    }

Answers (1)