PDA

Click to See Complete Forum and Search --> : silverlight and database


ngreenwood6
Aug 11th, 2009, 11:53 PM
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.

ntg
Aug 12th, 2009, 08:48 AM
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.

ngreenwood6
Aug 12th, 2009, 03:47 PM
I have tried to use httpwebrequest with silverlight but when I put in this line:


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.

ntg
Aug 12th, 2009, 04:14 PM
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.
Private Sub cmdCallService_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
Try
Dim req As HttpWebRequest = CType(WebRequest.Create("http://www.contra.gr/rss/home"), HttpWebRequest)
req.BeginGetResponse(New AsyncCallback(AddressOf responseHandler), req)
Catch ex As Exception
txtServiceResult.Text = ex.Message
End Try
End Sub

Private Sub responseHandler(ByVal asyncResult As IAsyncResult)
Try
Dim req As HttpWebRequest = CType(asyncResult.AsyncState, HttpWebRequest)
Dim rsp As HttpWebResponse = CType(req.EndGetResponse(asyncResult), HttpWebResponse)
Dim b() As Byte
ReDim b(Convert.ToInt32(rsp.GetResponseStream.Length - 1))
rsp.GetResponseStream.Read(b, 0, b.GetLength(0))
rsp.Close()
Catch ex As Exception
txtServiceResult.Text = ex.Message
End Try
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?

ngreenwood6
Aug 12th, 2009, 04:30 PM
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.

ntg
Aug 12th, 2009, 04:37 PM
No idea where this comes from.

The XAML that comes with the 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?

ngreenwood6
Aug 12th, 2009, 05:01 PM
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?

ntg
Aug 12th, 2009, 05:07 PM
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.:rolleyes: I just inspected the byte array b with the debugger.

ngreenwood6
Aug 12th, 2009, 05:08 PM
How do you do that? I havent really messed much with debugging. Also how would I write the response to the textbox.

ntg
Aug 12th, 2009, 05:34 PM
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).

Imports System.Net

Partial Public Class MainPage
Inherits UserControl

Private Delegate Sub CheckBoxUpdateDelegate(ByVal s As String)

Public Sub New()
InitializeComponent()
End Sub

Private Sub cmdCallService_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
Try
Dim req As HttpWebRequest = CType(WebRequest.Create("http://www.contra.gr/rss/home"), HttpWebRequest)
req.BeginGetResponse(New AsyncCallback(AddressOf responseHandler), req)
Catch ex As Exception
txtServiceResult.Text = ex.Message
End Try
End Sub

Private Sub responseHandler(ByVal asyncResult As IAsyncResult)
Try
Dim req As HttpWebRequest = CType(asyncResult.AsyncState, HttpWebRequest)
Dim rsp As HttpWebResponse = CType(req.EndGetResponse(asyncResult), HttpWebResponse)
Dim b() As Byte
ReDim b(Convert.ToInt32(rsp.GetResponseStream.Length - 1))
rsp.GetResponseStream.Read(b, 0, b.GetLength(0))
rsp.Close()
Dim s As String = System.Text.Encoding.GetEncoding("utf-8").GetChars(b)
Deployment.Current.Dispatcher.BeginInvoke(System.Delegate.CreateDelegate(GetType(CheckBoxUpdateDeleg ate), Me, "updateTextBox"), New Object() {s})
Catch ex As Exception
Deployment.Current.Dispatcher.BeginInvoke(System.Delegate.CreateDelegate(GetType(CheckBoxUpdateDeleg ate), Me, "updateTextBox"), New Object() {ex.Message})
End Try
End Sub

Private Sub updateTextBox(ByVal s As String)
Try
txtServiceResult.Text = s
Catch ex As Exception
txtServiceResult.Text = ex.Message
End Try
End Sub

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.

ngreenwood6
Aug 12th, 2009, 05:41 PM
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?

ntg
Aug 12th, 2009, 06:16 PM
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 (http://silverlight.net/forums/t/98582.aspx) 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). :sick: Geez, I've no idea why this happens.

MattP
Oct 8th, 2009, 06:32 PM
Silvelight tries to retrieve clientaccesspolicy.xml and then crossdomain.xml, both without success (404). :sick: 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 (http://msdn.microsoft.com/en-us/library/cc645032%28VS.95%29.aspx)