locked
Pass the value in silverlight to Asp.net RRS feed

  • Question

  • Hi All,

    Am in application we are using xml file as backend. so we have problem in insert and update the xml from silverlight applicatio. so we used the redirect to asp.net in clientbin folder and then we push the reocrd xml file

    mainPage.xaml

    var serviceUri = new Uri("Edit.aspx?Action=Update&Id=" + lblhiddenfield.Text , UriKind.Relative);

      var webClient = new WebClient();
                                webClient.OpenReadCompleted += openReadCompleted;
                                webClient.OpenReadAsync(serviceUri);

    Test.aspx

     Parameters par = new Parameters();
                    par.Action = Request.QueryString["Action"];
                    if (par.Action != "")
                    {
                        par.Id = Request.QueryString["Id"];
                        par.Counterparty = Request.QueryString["Counterparty"];
                        par.FileName = HttpUtility.UrlDecode(Request.QueryString["FileName"]);

    }

    then am save the XML file in Test.aspx

    Now in i have lager file data in XML so how can transer that much data in aspx with query string

    Is there any way pass the class,object or any thing to get the data in test.aspx file

    in simple word: How to pass the value between Silverlight applicaton to web application(client bin) with out query string

    ist very urgent am earing waitting for ur reply

    vignesh.k



    Friday, May 18, 2012 3:49 AM

Answers

  • Hi Vignesh,

    It is possible to edit a server file from silverlight,  i have done a sample project , find the code below i think it will help you.

    MainPage.xaml

     

    <UserControl x:Class="SilverlightApplication1.MainPage"xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d=http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc=http://schemas.openxmlformats.org/markup-compatibility/2006

    mc:Ignorable="d" 

    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White">

     <Grid.RowDefinitions>

    <RowDefinition />

    <RowDefinition Height="Auto"/>

    </Grid.RowDefinitions>

    <TextBox TextWrapping="Wrap" Name="XMLBlock"/>

    <Button Content="Write XML data" Grid.Row="1" Margin="3" Width="100" Click="Button_Click" />  


    </Grid>

    </UserControl>

    MainPage.xaml.cs

     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using System.IO;

    namespace SilverlightApplication1
    {
        public partial class MainPage : UserControl
        {
           WebClient client = new WebClient();
            Uri uritoXML = new Uri("FTPFileMonitor.xml", UriKind.Relative);
            Uri uritopage = new Uri("/Webform1.aspx", UriKind.Relative);
            public MainPage()
            {
                InitializeComponent();
            
                client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
                client.OpenWriteCompleted += new OpenWriteCompletedEventHandler(client_OpenWriteCompleted);
                client.WriteStreamClosed += new WriteStreamClosedEventHandler(client_WriteStreamClosed);
                client.OpenReadAsync(uritoXML);
               
            }

            void client_OpenWriteCompleted(object sender, OpenWriteCompletedEventArgs e)
            {

                using (System.IO.StreamWriter writer = new System.IO.StreamWriter(e.Result))
                {
                    writer.Write(XMLBlock.Text);
                    writer.Flush();           
                }
            }

            void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
            {
              
                using (StreamReader reader = new StreamReader(e.Result))
                {
                  
                    XMLBlock.Text = reader.ReadToEnd();

                }
               
               
            }

           

         

            private void Button_Click(object sender, RoutedEventArgs e)
            {
           
                client.OpenWriteAsync(uritopage);
              
            }

            void client_WriteStreamClosed(object sender, WriteStreamClosedEventArgs e)
            {
                if (e.Error != null)
                {
                    MessageBox.Show("error: "+e.Error.Message);
                }
                else
                {
                    MessageBox.Show("Written Successfully");
                }
            }
        }
    }

     

    In web project in Webform1.aspx.cs (we can use httphandler also)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    namespace SilverlightApplication1.Web
    {
        public partial class WebForm1 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                using (System.IO.StreamReader reader = new System.IO.StreamReader(Request.InputStream))
                {
                    System.IO.File.WriteAllText(this.Context.Server.MapPath("clientbin/FTPFileMonitor.xml"), reader.ReadToEnd());

                }
            }
        }
    }

     

    Regards,

    Sarathy K


     

     

     

    Sunday, May 20, 2012 2:01 PM

All replies

  • Hi ,

    If  you want to do that without querystring, then you can use a cookie .  Set the cookie from silverlight, and access it from asp.net.

    Here are two links for that :

    http://social.msdn.microsoft.com/Forums/en-US/silverlightnet/thread/cea8151b-1bb1-4c5c-913a-50c0faa4e032#f3b8d453-2531-444b-9d62-e5980fc31f59

    http://www.totaldotnet.com/Code/ShowCode37_SetCookieSilverlight.aspx

     Hope it Helps Laughing

    Friday, May 18, 2012 4:05 AM
  • Thanks for response Phani

    But  we have largre data its more than  75 values

    I ll attached my sample XML file

     <file id="1" counterpartyName="a" fileName="M%YYYY%%MM%%DD%.txt" modifiedDate="12/05/2012 10:28 AM" active="True">
        <configuration>
          <tags>a</tags>
          <fileLocation>E:\Projects\Senrigan\FTPMonitor\TestFiles</fileLocation>
          <fileArchivedLocation>a</fileArchivedLocation>
          <notification>
            <both>True</both>
            <emailAddressForSuccess>sundararajan@originwave.com</emailAddressForSuccess>
            <emailAddressForFailure>sundararajan@originwave.com</emailAddressForFailure>
          </notification>
          <schedule>
            <frequencyType>Monthly</frequencyType>
            <recurrsEvery>1</recurrsEvery>
            <weekDays></weekDays>
            <dateForMonthlyType>1</dateForMonthlyType>
            <skipweekends>True</skipweekends>
            <ExpectedDateInFilename>0</ExpectedDateInFilename>
            <softNoticeAt></softNoticeAt>
            <arrivesAt>11:30 PM</arrivesAt>
            <hardNoticeAt></hardNoticeAt>
            <startDate>01/05/2012</startDate>
            <endDate>31/12/2014</endDate>
          </schedule>
        </configuration>
        <description>a</description>
        <isArrived>True</isArrived>
        <isNotified>False</isNotified>
        <PreStatus>Success</PreStatus>
        <PostStatus></PostStatus>
        <lastArrivedDateTime>10/05/2012 11:21 AM</lastArrivedDateTime>
        <nextarrivalfilename>M20120801.txt</nextarrivalfilename>
        <nextArrivalDateTime>01/09/2012 11:30 PM</nextArrivalDateTime>
        <IsSatSunDayRun></IsSatSunDayRun>
    	<fileItem>
    		<fileItemFile fileName="M20120801.txt" isArrived="True" isNotified="False"  arrivedDateTIme="10/05/2012 11:21 AM"
    		PreStatus="" PostStatus=""/>
    	</fileItem>
        <history>
        </history>
      </file>

    Friday, May 18, 2012 4:14 AM
  • Hi ,

      Well, yes , the cookie size limit is 4kb , so if may be a problem if your file size is large.     Maybe if you could split your content , it would help , but is not that efficient solution.

      What issues did you face when trying to access that xml file directly from silverlight ?  Since your xml will be in your website, it has a url, right.  You can use that to access the data using Xmlreader and XmlWriter .  For large amount of data, i think this is the right way.

    Hope it Helps Laughing

       

    Friday, May 18, 2012 4:34 AM
  • Phani

    already we are using to dowload the data from xml by below code

    WebClient client = new WebClient();
                Uri uritoXML = new Uri("FTPFileMonitor.xml", UriKind.Relative);
    
                client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(rest_DownloadStringCompleted);
                client.DownloadStringAsync(uritoXML);

    But actual problem is we can't edit the XML from silverlight applciation

     our XML file in Project.web->Clientbin->test.xml so we can acess the file but we cant't edit that

    some times we need edit the  file to change the data for that we can acess

    i hope u understand the problem

    is there any way to handle this

    Friday, May 18, 2012 11:17 AM
  • I'm not an expert on this part of silverlight, but I would like to suggest a workaround for this problem.

    Here is the solution but it might be a bit complex :

    1. you can use the asp.net file upload mechanism to transfer file without size limitations to your asp.net page:

    http://msdn.microsoft.com/en-us/library/aa479405.aspx

    http://asp.net-tutorials.com/controls/file-upload-control/

    http://www.aspheute.com/english/20000802.asp

    2. use javascript to trigger this functionality using async call or initiating the asp.net functionality on the page:

    http://stackoverflow.com/questions/6187130/fire-asp-net-link-button-or-button-click-event-using-jquery

    http://stackoverflow.com/questions/7646162/how-to-fire-button-click-event-from-javascript-in-asp-net

    http://igstan.ro/posts/2009-01-11-ajax-file-upload-with-pure-javascript.html

    3. You can call this javascript function from Silverlight:

    http://blogs.silverlight.net/blogs/msnow/archive/2008/07/08/tip-of-the-day-15-communicating-between-javascript-amp-silverlight.aspx

    http://www.dotnetspider.com/tutorials/Silverlight-Tutorial-321.aspx

    These are the top links I found on Google, which are related to the basic steps I mentioned above. I think you might need to drill down more yourself to get it working completely.

    Saturday, May 19, 2012 2:28 PM
  • Hi Vignesh,

    It is possible to edit a server file from silverlight,  i have done a sample project , find the code below i think it will help you.

    MainPage.xaml

     

    <UserControl x:Class="SilverlightApplication1.MainPage"xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d=http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc=http://schemas.openxmlformats.org/markup-compatibility/2006

    mc:Ignorable="d" 

    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White">

     <Grid.RowDefinitions>

    <RowDefinition />

    <RowDefinition Height="Auto"/>

    </Grid.RowDefinitions>

    <TextBox TextWrapping="Wrap" Name="XMLBlock"/>

    <Button Content="Write XML data" Grid.Row="1" Margin="3" Width="100" Click="Button_Click" />  


    </Grid>

    </UserControl>

    MainPage.xaml.cs

     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using System.IO;

    namespace SilverlightApplication1
    {
        public partial class MainPage : UserControl
        {
           WebClient client = new WebClient();
            Uri uritoXML = new Uri("FTPFileMonitor.xml", UriKind.Relative);
            Uri uritopage = new Uri("/Webform1.aspx", UriKind.Relative);
            public MainPage()
            {
                InitializeComponent();
            
                client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
                client.OpenWriteCompleted += new OpenWriteCompletedEventHandler(client_OpenWriteCompleted);
                client.WriteStreamClosed += new WriteStreamClosedEventHandler(client_WriteStreamClosed);
                client.OpenReadAsync(uritoXML);
               
            }

            void client_OpenWriteCompleted(object sender, OpenWriteCompletedEventArgs e)
            {

                using (System.IO.StreamWriter writer = new System.IO.StreamWriter(e.Result))
                {
                    writer.Write(XMLBlock.Text);
                    writer.Flush();           
                }
            }

            void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
            {
              
                using (StreamReader reader = new StreamReader(e.Result))
                {
                  
                    XMLBlock.Text = reader.ReadToEnd();

                }
               
               
            }

           

         

            private void Button_Click(object sender, RoutedEventArgs e)
            {
           
                client.OpenWriteAsync(uritopage);
              
            }

            void client_WriteStreamClosed(object sender, WriteStreamClosedEventArgs e)
            {
                if (e.Error != null)
                {
                    MessageBox.Show("error: "+e.Error.Message);
                }
                else
                {
                    MessageBox.Show("Written Successfully");
                }
            }
        }
    }

     

    In web project in Webform1.aspx.cs (we can use httphandler also)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    namespace SilverlightApplication1.Web
    {
        public partial class WebForm1 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                using (System.IO.StreamReader reader = new System.IO.StreamReader(Request.InputStream))
                {
                    System.IO.File.WriteAllText(this.Context.Server.MapPath("clientbin/FTPFileMonitor.xml"), reader.ReadToEnd());

                }
            }
        }
    }

     

    Regards,

    Sarathy K


     

     

     

    Sunday, May 20, 2012 2:01 PM