Results 1 to 5 of 5

Thread: [RESOLVED] winforms to silverlight

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2008
    Location
    Burlington, ON, Canada
    Posts
    343

    Resolved [RESOLVED] winforms to silverlight

    Hi all,

    I just did a lengthy thread on the approach that would be necessary to take a .net 4 winforms app and start the migration over to web - the thread is here:

    http://www.vbforums.com/showthread.php?t=616192

    long story short, i have an existing database with several tables in it, but the key table for discussion was the login table - it contained (among others), two fields - username and password.

    my initial thread was asking if it were possible to point to that database and simply authenticate against those two fields and allow entry without jumping through all of the asp.net service providers hoops until i could properly learn them.

    My same question now stands, but for Silverlight 4.

    In short, i've created a new silverlight application in VS2010, i've dragged two labels - user name and password on to it, i've dragged a textbox for username, i've dragged a password box for password, and i've dragged a button with the text login on it to the screen.

    I've modified the web.config to include the connection string to the database.....

    and that's where i'm stumped - the guys over in the asp forums over the course of the thread above provided a 100% functioning solution (which i have to give full credit to brin351 and gep13 for providing by the way, it was bang on) - but i really really like the look and multibrowser context that silverlight 4 can function in and that's the route i would like to pursue if it's possible

    I've started watching the silverlight videos on RIA services, but they're all about populating datagrids and listboxes for the most part. And yes, i will get there eventually, but for the moment, i'm just after a simple user and pass screen with a login button that if authenticates, redirects to the main dashboard of the silverlight app - if not, it says sorry and kicks ya out.

    Any further insight would be appreciated, and for the record, I'm a winforms guy that is just starting in to the web side of things, so be gentle :-)

    thanks all.

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2008
    Location
    Burlington, ON, Canada
    Posts
    343

    Re: winforms to silverlight

    okay, so i've been researching quite a bit and to replicate my issue (which is alot closer to being solved now by the way), this assumes you have a SQL Server 2008 Database called 'CompanyApp' with a table in it called 'login' and that table has 2 fields called username and password respectively. It also assumes you have the latest Silverlight 4 tools installed.

    Where i'm stuck now is what code and/or calls need to be added to the button click event to check the credentials, and then if successful, redirect to a page that states 'congrats, you're logged in' (it'll be the main page of the app, but you get the point, that's for another few hundred posts :-))

    so - here's the steps so far:

    1. Start Visual Studio 2010
    2. Click File, New Project
    3. Select Silverlight Application & Click OK
    4. Place a check in "Enable WCF RIA Services" & Click OK
    5. Drag 2 labels, 1 textbox, 1 passwordbox and one button on to the design surface
    6. Change the content of Label1 to "User Name:"
    7. Change the content of Label 2 to "Password:"
    8. Change the content of button1 to "Login"
    8. Align the textbox and password box beside their respective labels and center the button underneath them
    9. Right-Click SilverlightApplication1.web, then Add, then New Item
    10. On the left under 'Installed Templates', Select 'Data', then on the right, select "ADO.NET Entity Data Model"
    11. Make sure "Generate from Database" is highlighted & Click Next
    12. In this case, click "Yes, Include the sensitive data in the connection string" & Click Next
    13. Drop down 'Tables' and place a check in 'login' & Click Finish
    14. Press CTRL-Shift-S to save the project, then under solution explorer, right click "Solution 'SilverlightApplcation1' (2 Projects)" and select "Build Solution"
    15. Right click "SilverlightApplication.web" and select Add, then New Item
    16. On the left under installed templates, choose "Web", then on the right, choose "Domain Service Class"
    17. Place a check in "Expose OData endpoint", a check in "login", and a check under "Enable Editing" & Click OK
    18. It may take a few seconds, but under the data sources tab, you should see 'login' appear under 'DomainService1' - i had to actually F5 and run the app to get it to refresh - at this point, you can drag the table to the design surface and get datagrids, details, etc if you like....HOWEVER, if you are running the 64 bit version of Windows 7, i learned that the RIA services had some trouble installing in both the "Program Files(x86) folder, and the the plain ole "Program Files" folder - if the DomainService1 and login don't show up, you need to copy c:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\Extensions\Microsoft\RiaTools to c:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\Extensions\Microsoft\RiaTools - you may have to restart visual studio as well.
    19. In solution explorer, highlight mainpage.xaml to get to the designer and double click the login button

    20.........

    here's where i'm stuck - i'm not sure what code needs to go in to check the values of textbox1 (the username) and passwordbox1 (the password) against the respective fields in the database - and furthermore, if successfully authenticated, to redirect to the next screen that says "congrats you're loggin in"

    i'll keep researching on this one but any help would be appreciated folks - thanks a million

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2008
    Location
    Burlington, ON, Canada
    Posts
    343

    Re: winforms to silverlight

    almost have this one figured out folks - just posted a new post in this forum on the codebehind with the domainservice for the solution and am also scouring our friends at google and elsewhere.....if i figure it out or get some help to do so i'll post the results....

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2008
    Location
    Burlington, ON, Canada
    Posts
    343

    Re: winforms to silverlight

    getting closer still - with the help of the database guys, i modified the function in the domainservice to the following:

    Code:
    Public Function countloginsmatching(ByVal username As String, ByVal password As String) As Integer
    
            Return Me.ObjectContext.logins.Where(Function(t) t.username = username AndAlso t.password = password).Count
    
    End Function
    and modified the buttonclick code on mainpage.xaml.vb to the following:

    Code:
     Dim username As String = tbUserName.Text
            Dim password As String = pbPassword.Password
    
            Dim context = New LoginContext()
    
            context.Load(context.GetLoginsByNameQuery(username, password))
    
            Label3.Content = context.countloginsmatching(username, password).Value
    
            DataGrid1.ItemsSource = context.logins
    the datagrid and label are just debugging tools to make sure data is actually getting passed between the server side with the domain service and the client side on mainpage.xaml

    however........even though i can clearly see the datagrid populate with the user when a match is found, the count always returns 0 and i have no idea why - if anybody has any insight i'm all ears.

    thanks in advance.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2008
    Location
    Burlington, ON, Canada
    Posts
    343

    Re: winforms to silverlight

    okay, finally got it, this was a huge pain and here's the post that started it all because i actually thought my linq to sql was off when it turned out to be entirely different.

    definitely check the last entry of this post here:

    http://www.vbforums.com/showthread.php?t=620770

    as for the code required, included below is the client side codebehind:

    Code:
    Imports System.ServiceModel.DomainServices.Client
    Imports EstimatorSilverlight.Web
    
    Partial Public Class MainPage
        Inherits UserControl
        Dim context = New LoginContext()
        Dim invokeop As InvokeOperation(Of Integer)
    
        Public Sub New()
            InitializeComponent()
        End Sub
    
        Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnLogin.Click
            Dim username As String = tbUserName.Text
            Dim password As String = pbPassword.Password
    
            invokeop = context.countloginsmatching(username, password)
            AddHandler invokeop.Completed, AddressOf invokeOperation_Completed
    
        End Sub
    
        Private Sub invokeOperation_Completed(ByVal Sender As Object, ByVal E As EventArgs)
            Label3.Content = invokeop.Value
        End Sub
    
        Private Sub LoginDomainDataSource_LoadedData(ByVal sender As System.Object, ByVal e As System.Windows.Controls.LoadedDataEventArgs) Handles LoginDomainDataSource.LoadedData
    
            If e.HasError Then
                System.Windows.MessageBox.Show(e.Error.ToString, "Load Error", System.Windows.MessageBoxButton.OK)
                e.MarkErrorAsHandled()
            End If
        End Sub
    End Class
    and then finally the domainservice code as well - the key is the final function and the invoke tag:

    Code:
    Option Compare Binary
    Option Infer On
    Option Strict On
    Option Explicit On
    
    Imports EstimatorSilverlight.Web
    Imports System
    Imports System.Collections.Generic
    Imports System.ComponentModel
    Imports System.ComponentModel.DataAnnotations
    Imports System.Data
    Imports System.Linq
    Imports System.ServiceModel.DomainServices.EntityFramework
    Imports System.ServiceModel.DomainServices.Hosting
    Imports System.ServiceModel.DomainServices.Server
    
    'Implements application logic using the LoginEntities context.
    ' TODO: Add your application logic to these methods or in additional methods.
    ' TODO: Wire up authentication (Windows/ASP.NET Forms) and uncomment the following to disable anonymous access
    ' Also consider adding roles to restrict access as appropriate.
    '<RequiresAuthentication> _
    
    <EnableClientAccess()>  _
    Public Class LoginService
        Inherits LinqToEntitiesDomainService(Of LoginEntities)
        
        'TODO:
        ' Consider constraining the results of your query method.  If you need additional input you can
        ' add parameters to this method or create additional query methods with different names.
        'To support paging you will need to add ordering to the 'logins' query.
    
        <Query(IsDefault:=True)> _
        Public Function GetLogins() As IQueryable(Of login)
            Return From login In Me.ObjectContext.logins Order By login.username
        End Function
        
        Public Sub InsertLogin(ByVal login As login)
            If ((login.EntityState = EntityState.Detached)  _
                        = false) Then
                Me.ObjectContext.ObjectStateManager.ChangeObjectState(login, EntityState.Added)
            Else
                Me.ObjectContext.logins.AddObject(login)
            End If
        End Sub
        
        Public Sub UpdateLogin(ByVal currentlogin As login)
            Me.ObjectContext.logins.AttachAsModified(currentlogin, Me.ChangeSet.GetOriginal(currentlogin))
        End Sub
        
        Public Sub DeleteLogin(ByVal login As login)
            If (login.EntityState = EntityState.Detached) Then
                Me.ObjectContext.logins.Attach(login)
            End If
            Me.ObjectContext.logins.DeleteObject(login)
        End Sub
    
        <Invoke()> _
        Public Function countloginsmatching(ByVal username As String, ByVal password As String) As Integer
            Dim credentials As List(Of login)
            credentials = Me.ObjectContext.logins.Where(Function(t) t.username = username AndAlso t.password = password).ToList()
            Return credentials.Count
        End Function
    
    End Class
    once again, a special thanks to techgnome for all the insight.

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