Results 1 to 13 of 13

Thread: silverlight and database

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2009
    Posts
    448

    silverlight and database

    Basically I am hosting my website on a linux server. My website is running in php using a mysql database. I have been playing around with silverlight because I have good knowledge in c# and would like to create some fancy silverlight applications. However the data that the silverlight application will use is going to come from the mysql database. From my understanding silverlight runs on the client machine so you cant directly access mysql. So I read up on it some more and people were suggesting a wcf service. However, from what I was reading it only runs on a windows machine with iis. So what are the options for me. Isnt there an easy way to talk to a mysql database. I dont really know anything about web services but I have good knowledge in php, mysql, and c#. Any help at all is appreciated.
    If I helped you please rate me.

  2. #2
    Frenzied Member ntg's Avatar
    Join Date
    Sep 2004
    Posts
    1,449

    Re: silverlight and database

    Not an expert in SL myself but I run across such a problem a few days ago. Basically, you have to create an indirect mechanism of getting the data from the SL application. One is WCF. Another is to create a web service. Yet another would be to use an HTTP Post which would trigger the needed query to MySql and retrieve the response with the data - see the HttpWebRequest class.
    "Feel the force...read the source..."
    Utilities: POPFileDebugViewProcess ExplorerWiresharkKeePassUltraVNCPic2Ascii
    .Net tools & open source: DotNetNukelog4NetCLRProfiler
    My open source projects: Thales SimulatorEFT CalculatorSystem Info ReporterVSS2SVNIBAN Functions
    Customer quote: "If the server has a RAID array, why should we bother with backups?"
    Programmer quote: "I never comment my code. Something that is hard to write should be impossible to comprehend."
    Ignorant quote: "I have no respect for universities, as they teach not practicle stuff, and charge money for"

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2009
    Posts
    448

    Re: silverlight and database

    I have tried to use httpwebrequest with silverlight but when I put in this line:

    Code:
    byte[] buffer = Encoding.ASCII.GetBytes("username=test");
    it is giving me this error:

    System.Text.Encoding does not contain a definition for "ASCII".
    However when I am typing it out it shows the method fine. I have the System.Text namespace in my project. Any ideas?

    Also it is giving me errors for System.Net.HttpWebRequest on contentlength, getrequeststream, and getresponse for not containing a definition. I used the same code I had in a windows form project that worked fine.
    If I helped you please rate me.

  4. #4
    Frenzied Member ntg's Avatar
    Join Date
    Sep 2004
    Posts
    1,449

    Re: silverlight and database

    Encoding.ASCII does not exist in the Silverlight runtime - at least my intellisense isn't showing it. If you copied this from a WinForms project, that must be the cause of your problem.

    I quickly wrote this to confirm how it can be done.
    VB Code:
    1. Private Sub cmdCallService_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
    2.         Try
    3.             Dim req As HttpWebRequest = CType(WebRequest.Create("http://www.contra.gr/rss/home"), HttpWebRequest)
    4.             req.BeginGetResponse(New AsyncCallback(AddressOf responseHandler), req)
    5.         Catch ex As Exception
    6.             txtServiceResult.Text = ex.Message
    7.         End Try
    8.     End Sub
    9.  
    10.     Private Sub responseHandler(ByVal asyncResult As IAsyncResult)
    11.         Try
    12.             Dim req As HttpWebRequest = CType(asyncResult.AsyncState, HttpWebRequest)
    13.             Dim rsp As HttpWebResponse = CType(req.EndGetResponse(asyncResult), HttpWebResponse)
    14.             Dim b() As Byte
    15.             ReDim b(Convert.ToInt32(rsp.GetResponseStream.Length - 1))
    16.             rsp.GetResponseStream.Read(b, 0, b.GetLength(0))
    17.             rsp.Close()
    18.         Catch ex As Exception
    19.             txtServiceResult.Text = ex.Message
    20.         End Try
    21.     End Sub
    Included System.Net and added an 'Imports System.Net'. It works great. Can you post more info on the errors you're getting along with some code?
    "Feel the force...read the source..."
    Utilities: POPFileDebugViewProcess ExplorerWiresharkKeePassUltraVNCPic2Ascii
    .Net tools & open source: DotNetNukelog4NetCLRProfiler
    My open source projects: Thales SimulatorEFT CalculatorSystem Info ReporterVSS2SVNIBAN Functions
    Customer quote: "If the server has a RAID array, why should we bother with backups?"
    Programmer quote: "I never comment my code. Something that is hard to write should be impossible to comprehend."
    Ignorant quote: "I have no respect for universities, as they teach not practicle stuff, and charge money for"

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2009
    Posts
    448

    Re: silverlight and database

    I just tried the code that you gave me and I got this:

    An unhandled exception('Unhandled error in Silverlight Application Invalid cross-thread access. at MS.Internal.XcpImports.CheckThread() at System.Windows.DependencyObject SetValueInternal(DependencyProperty dp, Object value, Boolean allowReadOnlySet, Boolean isSetByStyle, Boolean
    It looks like it is cutting of the rest of the error. Any ideas? I added a reference to System.Net and did an Imports System.Net as well. No build errors just comes up when I run it.
    If I helped you please rate me.

  6. #6
    Frenzied Member ntg's Avatar
    Join Date
    Sep 2004
    Posts
    1,449

    Re: silverlight and database

    No idea where this comes from.

    The XAML that comes with the code:
    Code:
    <UserControl x:Class="SLTest.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:DesignWidth="640" d:DesignHeight="480">
      <Grid x:Name="LayoutRoot">
      	<Button x:Name="cmdCallService" HorizontalAlignment="Left" Margin="126,137,0,0" VerticalAlignment="Top" Width="75" Content="Button" Click="cmdCallService_Click"/>
      	<TextBox x:Name="txtServiceResult" Margin="126,193,87,89" Text="TextBox" TextWrapping="Wrap"/>
    
      </Grid>
    </UserControl>
    And BTW, using VS 2008 9.0.30729.1 SP and Silverlight 3 here.

    Can you debug and determine what line throws the error?
    "Feel the force...read the source..."
    Utilities: POPFileDebugViewProcess ExplorerWiresharkKeePassUltraVNCPic2Ascii
    .Net tools & open source: DotNetNukelog4NetCLRProfiler
    My open source projects: Thales SimulatorEFT CalculatorSystem Info ReporterVSS2SVNIBAN Functions
    Customer quote: "If the server has a RAID array, why should we bother with backups?"
    Programmer quote: "I never comment my code. Something that is hard to write should be impossible to comprehend."
    Ignorant quote: "I have no respect for universities, as they teach not practicle stuff, and charge money for"

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2009
    Posts
    448

    Re: silverlight and database

    Ok so now I rebooted my computer, turned of wamp(whoops) and tried connecting running it and it shows up in my browser(firefox). Then when I click the button it says transferring data from www.contra.gr (in the status bar of firefox) but nothing ever shows up in the textbox. Any ideas?
    If I helped you please rate me.

  8. #8
    Frenzied Member ntg's Avatar
    Join Date
    Sep 2004
    Posts
    1,449

    Re: silverlight and database

    I've an idea if you're using the code I've posted...I didn't bother to copy the contents of the response to the textbox. I just inspected the byte array b with the debugger.
    "Feel the force...read the source..."
    Utilities: POPFileDebugViewProcess ExplorerWiresharkKeePassUltraVNCPic2Ascii
    .Net tools & open source: DotNetNukelog4NetCLRProfiler
    My open source projects: Thales SimulatorEFT CalculatorSystem Info ReporterVSS2SVNIBAN Functions
    Customer quote: "If the server has a RAID array, why should we bother with backups?"
    Programmer quote: "I never comment my code. Something that is hard to write should be impossible to comprehend."
    Ignorant quote: "I have no respect for universities, as they teach not practicle stuff, and charge money for"

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2009
    Posts
    448

    Re: silverlight and database

    How do you do that? I havent really messed much with debugging. Also how would I write the response to the textbox.
    If I helped you please rate me.

  10. #10
    Frenzied Member ntg's Avatar
    Join Date
    Sep 2004
    Posts
    1,449

    Re: silverlight and database

    Are you using Visual Studio or are you compiling from the command line? If you're using studio, check the Debug menu.

    Anyway, here's the code that copies the results to the textbox (XAML remains the same).

    VB Code:
    1. Imports System.Net
    2.  
    3. Partial Public Class MainPage
    4.     Inherits UserControl
    5.  
    6.     Private Delegate Sub CheckBoxUpdateDelegate(ByVal s As String)
    7.  
    8.     Public Sub New()
    9.         InitializeComponent()
    10.     End Sub
    11.  
    12.     Private Sub cmdCallService_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
    13.         Try
    14.             Dim req As HttpWebRequest = CType(WebRequest.Create("http://www.contra.gr/rss/home"), HttpWebRequest)
    15.             req.BeginGetResponse(New AsyncCallback(AddressOf responseHandler), req)
    16.         Catch ex As Exception
    17.             txtServiceResult.Text = ex.Message
    18.         End Try
    19.     End Sub
    20.  
    21.     Private Sub responseHandler(ByVal asyncResult As IAsyncResult)
    22.         Try
    23.             Dim req As HttpWebRequest = CType(asyncResult.AsyncState, HttpWebRequest)
    24.             Dim rsp As HttpWebResponse = CType(req.EndGetResponse(asyncResult), HttpWebResponse)
    25.             Dim b() As Byte
    26.             ReDim b(Convert.ToInt32(rsp.GetResponseStream.Length - 1))
    27.             rsp.GetResponseStream.Read(b, 0, b.GetLength(0))
    28.             rsp.Close()
    29.             Dim s As String = System.Text.Encoding.GetEncoding("utf-8").GetChars(b)
    30.             Deployment.Current.Dispatcher.BeginInvoke(System.Delegate.CreateDelegate(GetType(CheckBoxUpdateDelegate), Me, "updateTextBox"), New Object() {s})
    31.         Catch ex As Exception
    32.             Deployment.Current.Dispatcher.BeginInvoke(System.Delegate.CreateDelegate(GetType(CheckBoxUpdateDelegate), Me, "updateTextBox"), New Object() {ex.Message})
    33.         End Try
    34.     End Sub
    35.  
    36.     Private Sub updateTextBox(ByVal s As String)
    37.         Try
    38.             txtServiceResult.Text = s
    39.         Catch ex As Exception
    40.             txtServiceResult.Text = ex.Message
    41.         End Try
    42.     End Sub
    43.  
    44. End Class

    Note that I've added the delegate, which helps me to post a string on the UI while not on the UI thread - that's why you were getting the 'Invalid cross-thread access' exception before. That means that I can't just go access some part of the user interface (like txtServiceResult.Text) without properly marshalling my call.

    Anyway, it must be working now. Once you hit the command button, the text box fills with the response.
    "Feel the force...read the source..."
    Utilities: POPFileDebugViewProcess ExplorerWiresharkKeePassUltraVNCPic2Ascii
    .Net tools & open source: DotNetNukelog4NetCLRProfiler
    My open source projects: Thales SimulatorEFT CalculatorSystem Info ReporterVSS2SVNIBAN Functions
    Customer quote: "If the server has a RAID array, why should we bother with backups?"
    Programmer quote: "I never comment my code. Something that is hard to write should be impossible to comprehend."
    Ignorant quote: "I have no respect for universities, as they teach not practicle stuff, and charge money for"

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2009
    Posts
    448

    Re: silverlight and database

    Cool that works good on that website. However, I tried it on another website and nothing showed up in the textbox. Does that only read xml documents? How does this method work? Is it posting data to the server and getting a response from the server?
    If I helped you please rate me.

  12. #12
    Frenzied Member ntg's Avatar
    Join Date
    Sep 2004
    Posts
    1,449

    Re: silverlight and database

    This uses an HTTP GET (essentially what your browser does what you point it to a URL), but I think you can change that using the Method property of HttpWebRequest.

    I noticed that it doesn't work on other sites as well. However, it's not confined to xml, because this site works. I've put a WireShark trace - it seems that on the sites it fails (an exception is thrown at line 24), Silvelight tries to retrieve clientaccesspolicy.xml and then crossdomain.xml, both without success (404). Geez, I've no idea why this happens.
    "Feel the force...read the source..."
    Utilities: POPFileDebugViewProcess ExplorerWiresharkKeePassUltraVNCPic2Ascii
    .Net tools & open source: DotNetNukelog4NetCLRProfiler
    My open source projects: Thales SimulatorEFT CalculatorSystem Info ReporterVSS2SVNIBAN Functions
    Customer quote: "If the server has a RAID array, why should we bother with backups?"
    Programmer quote: "I never comment my code. Something that is hard to write should be impossible to comprehend."
    Ignorant quote: "I have no respect for universities, as they teach not practicle stuff, and charge money for"

  13. #13
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: silverlight and database

    Quote Originally Posted by ntg View Post
    Silvelight tries to retrieve clientaccesspolicy.xml and then crossdomain.xml, both without success (404). Geez, I've no idea why this happens.
    Through probably too late for your question I'll post for others to see.

    When making a request to a cross-domain site Silverlight will attempt to download the Silverlight policy file clientaccesspolicy.xml (from the root of the target domain). If there is no Silverlight policy file then it will look for a Flash policy file crossdoamin.xml (from the root of the target domain again).

    If neither of these file are found at the root then Silverlight will throw a SecurityException of access denied.

    For a more in-depth explanation: Network Security Access Restrictions in Silverlight

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width