Silverlight / WPF - how to make the silverlight page auto refresh after making the save action in MVVM

Asked By Ramachandran on 02-Jul-12 02:01 AM
I am using the Silver light MVVM  page i am calling the web service for loading the data from database and showing in listbox. i have a xaml page as a item. when i click the save from the individual item of the list box the data is saved in the database and again the complete page should be load with the latest saved data in the top of the list box. how to make this page refresh after clicking the save button of the item in the list box........
sravan challa replied to Ramachandran on 16-Jul-12 07:12 AM
Hi Ramchandran,
    
1)  When fetching from service you are getting a colllection from the database and binding that to your listbox

2) When saving the data to your database you are only saving the data. If you can call the method which returns a collection while saving the data that will update the listbox without refreshing the page. Please have a look at the sample code below.

-- Fetch Data
public ObservableCollection<Customer> GetData()
        {
            using (SqlConnection conn = new SqlConnection(connString))
            {
                using (SqlCommand cmd = new SqlCommand("select * from Persons", conn))
                {
                    conn.Open();
                    SqlDataReader dr = cmd.ExecuteReader();
                    cust = new ObservableCollection<Customer>();
                    while (dr.Read())
                    {
                        Customer c = new Customer(); 

                        cust.Add(c);
                    }
                }
            }
            return cust;
        }


-- Save Data
public ObservableCollection<Customer> Save(Customer input)
        {
            using (SqlConnection conn = new SqlConnection(connString))
            {
                string insert = "insert into Persons values (" + input.id + ",'" + input.lname + "','" + input.fname + "','" + input.city + "')";
                using (SqlCommand cmd = new SqlCommand(insert, conn))
                {
                    conn.Open();
                    cmd.ExecuteNonQuery();
                }
            }
            return GetData();
        }