Page 1 of 1

Update Data Problem with Sqlite!

Posted: Thu 25 Jul 2013 13:51
by Muneeb
Hi there and Salam!
I am using Visual Studio 2012 with devart dotconnect support as instructed in http://www.devart.com/dotconnect/sqlite ... _linq.html. The problem is i am not able to update the data!
Here is the module!

Code: Select all

private void button1_Click(object sender, EventArgs e)
        {
            if (textBox2.Text == "" || textBox3.Text == "" || textBox4.Text == "" || textBox5.Text == "" || textBox6.Text == "")
            {
                MessageBox.Show("Please Fill All The Fields!", "Typing Mistake", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else if (textBox2.Text != "" && textBox3.Text != "" && textBox4.Text != "" && textBox5.Text != "" && textBox6.Text != "")
            {
                if (pictureBox1.Image != null)
                {
                    string s = Directory.GetCurrentDirectory();
                    if (!Directory.Exists("Photos"))
                    {
                        Directory.CreateDirectory("Photos");
                    }
                    pictureBox1.Image.Save(s + "\\Photos\\" + textBox2.Text + ".jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);

                }
                MainDataContext context = new MainDataContext();

                Patient newPatient = new Patient();
                newPatient.Name = textBox2.Text;
                newPatient.Father = textBox3.Text;
                newPatient.Dasd = textBox4.Text;
                newPatient.Sex = textBox5.Text;
                newPatient.Mob = textBox6.Text;
                context.SubmitChanges();
                MessageBox.Show("Saved!");
                this.Dispose();
            }
        }
The problem i am facing is that, i am unable to update (edited) data. Followed this example on the Tutorial Page.

Code: Select all

product.ProductName = "Edited product";
product.Price = 15;
context.SubmitChanges();
Telling you before hand that it is not the matter of Picture as far as i know because i am not saving any picture, it is null all the time.

This code Gives me no error of any sort, displays the MessageBox that i created, but no data is updated.
I have no clue how to make it working!
Please Help me out!
Regards,
Mirza Muneeb

Re: Update Data Problem with Sqlite!

Posted: Fri 26 Jul 2013 09:17
by MariiaI
Your code sample doesn't update an existing data. It creates a new Patient object and prepared it with values for insertion. To make your DataContext (MainDataContext context = new MainDataContext();) object know that the newPatient object should be inserted to the database, it is necessary to call InsertOnSubmit() method before SubmitChanges():

Code: Select all

context.Patients.InsertOnSubmit(newPatient);
context.SubmitChanges();
To update an existing database data, you should retrieve the necessary data before making changes:

Code: Select all

MainDataContext context = new MainDataContext();
 Patient newPatient = (from c in context.Patients select c).First();
 /// make changes as in your code
 context.SubmitChanges();
As you can see in the LINQ to SQLite Tutorial, before updating data via this code:

Code: Select all

product.ProductName = "Edited product";
product.Price = 15;
context.SubmitChanges();
we have retrieved product (see above in the "Inserting New Data" section, the last lines in the C# example code):

Code: Select all

Product product = query.First();
For more information please refer here:
http://www.devart.com/linqconnect/docs/ ... gData.html
http://www.devart.com/linqconnect/docs/ ... gData.html
http://www.devart.com/linqconnect/docs/ ... ities.html