Showing posts with label Silverlight. Show all posts
Showing posts with label Silverlight. Show all posts

Monday, May 13, 2013

Accessing Web.Config to pass values to silverlight Application

Like asp.net we are use to save our application information for example connection string and others. Application needs that configuration information to run a different section of it. Application developer needs to change that information often, but in Silverlight it not possible with traditional way because Silverlight is client side technology so web.config is not accessible directly like in asp.net.

For Silverlight we need to pass parameter from server side as access them on client side in App.xaml.

Steps:

1- Add a key and its value in web.config in appSettings section.

2- Add param tags in Default.aspx and Default.html



3- Catch the value in App.xaml in Application_Startup function.

4- Now you can set this value anywhere in application by using this code.

if (Application.Current.Host.InitParams.ContainsKey("BingMapKey"))
{
BingMap.CredentialsProvider = new ApplicationIdCredentialsProvider(Application.Current.Host.InitParams["BingMapKey"]);
}







Friday, August 10, 2012

How to get current URL in asp.net or Silverlight?


How to get current URL in asp.net or Silverlight?

Get Silverlight xap and hosting page URL
Very simple but I am often asked this during trainings:

Get the URL to the current xap file:
    App.Current.Host.Source.AbsoluteUri

Sample for Xap Name
    http://www.myhost.com/ClientBin/SilverlightApplication1.xap
Get the full URL of the page hosting the xap (with QueryStrings):
    HtmlPage.Document.DocumentUri.ToString()

Sample for hosing page URI
Uri SourceUri = new Uri(HtmlPage.Document.DocumentUri, Application.Current.Host.Source.ToString().Substring(0,Application.Current.Host.Source.ToString().IndexOf("ClientBin") -1));

It will return http://www.myhost.com/

Thanks!

Monday, October 31, 2011

Entity is currently being edited and has uncommitted changes - EndEdit before changes can be... (Exception)


I was facing the same problem as many people has faced this problem while I searched the internet for solution,  my scenario is that I am inserting data from a child form, then it updated in datagrid and as well inserted in database it is working fine. but after adding if user delete the new added entity it give me above exception and delete failed. But if you sort or search the grid after adding or refresh the page it works fine user can just can select the new added entity and delete perfectly.
Here is my code
this is code for adding the input from childform on closing event of child form It will add the data into database and update the datagrid.
private void NewCompany_Closed(object sender, EventArgs e)
        {
            var companyView = (AddCompanyView)sender;
            if (companyView.NewCompany.OrganizationNumber != null)
            {   
                companyDomainDataSource.DataView.Add(companyView.NewCompany);
                CompanyDataGrid.CommitEdit(DataGridEditingUnit.Row, true );
                CompanyDataGrid.CancelEdit();
              
                companyDomainDataSource.SubmitChanges();
            }
            companyView.NewCompanyForm.CancelEdit();
       
        }

This is my delete method.

private void DeleteCompany_Click(object sender, RoutedEventArgs e)
        {

            MessageBoxResult result = MessageBox.Show("Are you sure you want to delete the selected company?", "Message",
                                                      MessageBoxButton.OKCancel);

            if(result == MessageBoxResult.OK)
            {
                var selectedCompany = (Company)CompanyDataGrid.SelectedItem;

                var counts = selectedCompany.Fleet.Count;

                if (counts != 0)
                {

                    MessageBox.Show("You cannot delete selected company because there are fleets registered under this company!", "Message",
                                                             MessageBoxButton.OK);
                }
                else
                {
                    try
                    {
                        var dctx = (TNTDS)companyDomainDataSource.DomainContext;

                        if (result == MessageBoxResult.OK)
                        {
                            dctx.Companies.Remove(selectedCompany);
                            dctx.SubmitChanges();
                            MessageBox.Show("Deleted Successfully!", "Message", MessageBoxButton.OK);
                        }

                    }
                    catch (Exception ex)
                    {

                        MessageBox.Show(ex.ToString(), "Message", MessageBoxButton.OK);
                    }
                }
            }

        }


Here is solution I found. Its very simple just send changes from different contexts so it will avoid any confusion to Entity framework
For example for inserting data use 
companyDomainDataSource.DomainContext.SubmitChanges();
And for deleting
companyDomainDataSource.SubmitChanges();
It will work perfectly and datagrid will show changes according while inserting and deleting both.
Thanks!