3

Background: I have a winform application written in VB.NET that uses a WebService to send out different invitations to users based on the marketing company they select to take different interviews. The winform app is pulling string values from a variety of textboxes, listboxes, and dropdownlists to create some XML and push it to a web service called AcompServiceClient

Questions:

  • Is there a wizard or 3rd party application that will export winform data to webform asp.net or should I build an aspx page from scratch w/ the same namespaces for all the controls as the winform app?
  • Which files do I need to transport or setup to make this work besides the AcompServiceClient web service and the code-behind vb? (look at screenshot of the Project Files)
  • Do i have to copy over any parts of the app.config file and adapt it to the web.config file?

I was thinking:

  • I can start by copying the Acomp_Invitation_Form.vb to the AComp_Invitation_Web_App.aspx.vb code behind page.
  • Add existing webservice off the webserver
  • Manually re-add formatting, text boxes, list boxes, and drop down lists on the front end aspx page using the same names / id's

Here's a screenshot of the WinForm App:

ACOMP Screenshot

Here's a screenshot of the Project Files:

enter image description here

Here's my code on Acomp_Invitation_Form.vb:

Imports TestClient.aCompService
Imports System.Text
Public Class Form1

Private proxy As New AcompServiceClient
Private Sub stuff()

    Dim splitContractingBundle() As String
    splitContractingBundle = Split(cb2.SelectedItem, "|")
    Dim splitMarketingCompany() As String
    splitMarketingCompany = Split(cb3.SelectedItem, "|")
    Dim strDate As String = System.DateTime.Now.ToString
    Dim strOpData As String = String.Format("{0}~{1}~{2}~{3}~{4}~{5}~{6}~{7}~{8}~{9}~{10}",
                                            Trim(splitMarketingCompany(0)), txtFirstName.Text, "", txtLastName.Text,
                                            txtEmail.Text, txtEmail.Text, "1", strDate,
                                            "Pending", "1/1/1900", Trim(splitContractingBundle(0)))

    Dim int1 As Boolean = proxy.AddContractOpportunity(strOpData, "test", "test")
    txtEmail.Text = ""
    txtFirstName.Text = ""
    txtLastName.Text = ""
    lbCarriers.Items.Clear()
    cb2.Items.Clear()
    cb3.Items.Clear()
    cb2.SelectedItem = ""
    cb3.SelectedText = ""
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    'TODO Add code to validate that all selections that are reaquired are met.
    'ccemail and the additional message are not required
    Dim firstname As String = txtFirstName.Text
    Dim lastname As String = txtLastName.Text
    Dim ccEmail As String = txtccEmail.Text
    Dim sb As New StringBuilder
    sb.AppendLine("<?xml version=""1.0"" encoding=""utf-8""?>")
    sb.AppendLine("<root>")
    sb.AppendLine("<MarketingCompany>")
    sb.AppendLine("<MarketingCompanyName>")
    ''Get Marketing Company Short Name
    Dim splitMC As String() = Split(cb3.SelectedItem, "|")
    Dim MCShort As String = Trim(splitMC(0))
    sb.AppendLine(String.Format("<MCNAme>{0}</MCNAme>", MCShort))
    'sb.AppendLine(String.Format("<MCNAme>{0}</MCNAme>", My.Settings.MarketingCompanyShortName))
    sb.AppendLine(String.Format("<ccEmail>{0}</ccEmail>", txtccEmail.Text))
    sb.AppendLine(String.Format("<emailMessage>{0}</emailMessage>", txtMessage.Text))
    sb.AppendLine(String.Format("<MarketerName>{0}</MarketerName>", txtMarketerName.Text))
    sb.AppendLine("<agent>")
    sb.AppendLine(String.Format("<FirstName>{0}</FirstName>", txtFirstName.Text))
    sb.AppendLine(String.Format("<LastName>{0}</LastName>", txtLastName.Text))
    sb.AppendLine(String.Format("<Email>{0}</Email>", txtEmail.Text))
    sb.AppendLine("<CRMGuid>123456</CRMGuid>")
    Dim spltBundles() As String

    For Each item In cb2.SelectedItems
        If Trim(item) <> "" Then
            spltBundles = Split(item, "|")
            sb.AppendLine("<ContractingOpportunity>")
            sb.AppendLine(String.Format("<Carrier>{0}</Carrier>", Trim(spltBundles(0))))
            sb.AppendLine(String.Format("<ContractingOpportunityName>{0}</ContractingOpportunityName>", Trim(spltBundles(1))))
            sb.AppendLine("</ContractingOpportunity>")
        End If
    Next
    sb.AppendLine("</agent>")
    sb.AppendLine("</MarketingCompanyName>")
    sb.AppendLine(" </MarketingCompany>")
    sb.AppendLine(" </root>")
    Dim xmlStr = sb.ToString
    Dim int1 As Boolean = proxy.AddContractOpportunity(xmlStr.ToString, "test", "test")
    MsgBox("Made It")
End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    GetCarriers()
    GetMarketingCompanies()
End Sub

Private Sub GetCarriers()
    Try
        Dim ac1 As Array
        ac1 = proxy.GetCarrierNames("test", "test")

        For Each item In ac1
            lbCarriers.Items.Add(String.Format("{0} | {1} | {2}", item.CarrierID, item.CarrierNameLong, item.CarrierNameShort))

        Next
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

End Sub
Private Sub GetMarketingCompanies()
    Try
        Dim ac1 As Array
        ac1 = proxy.GetMarketingCompanyNames("test", "test")

        For Each item In ac1
            cb3.Items.Add(String.Format("{0}   |   {1}", item.MarketingCompanyShort, item.MarketingCompanyName))
        Next
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

Private Sub lbCarriers_LostFocus(sender As Object, e As System.EventArgs) Handles lbCarriers.LostFocus
    Dim splt() As String
    Dim ac1 As Array
    cb2.Items.Clear()

    For Each item In lbCarriers.SelectedItems
        splt = Split(item, "|")
        ac1 = proxy.GetContractingBundles("test", "test", Trim(splt(0)))
        For Each Pitem In ac1
            cb2.Items.Add(Trim(splt(2)) & " | " & Pitem.FormBundleName)
        Next
    Next
End Sub
End Class
Brian McCarthy
  • 4,658
  • 16
  • 49
  • 66
  • 1
    Well, easy is a relative term I guess. You could export your form as an activeX control with a setting in your project details and IE (with proper assembly trust) will load it right up. Thats pretty easy. – asawyer Apr 08 '11 at 15:27
  • @asawyer, thanks for your response. where can i find the option to export as an activeX control? All I see is the Export Template Wizard on VS2010 – Brian McCarthy Apr 08 '11 at 16:13
  • You have to check the "Make COM visible" checkbox, and there may be another small step or two, then reference it in html with a object tag. Honesty though, it's not really advisable and I didn't mean it as a real solution. Kinda neat though. – asawyer Apr 08 '11 at 16:51
  • @asawyer, what is the "Make COM visible" checkbox under? – Brian McCarthy Apr 08 '11 at 18:10
  • I haven't done this technique in years, and it was a simple demo then. There is more involved then just ticking the com box, it only works in IE, you have to open big security holes into your trusted zone, and if you ever want to distribute to clients you are forcing them to install your activex component. Everything you need to know is online if you search around a little. I will not do it for you. – asawyer Apr 08 '11 at 18:41

3 Answers3

4

Be very careful of the easy way. While ASP.NET Web Forms might look similar to Windows Forms (controls hooked up to events), the underlying mechanism is very very different. If you have not done so already I recommend you read up on how HTTP works and the life cycle of an ASP.NET page.

System Down
  • 6,192
  • 1
  • 30
  • 34
  • 1
    This has strong merit too... While true that within the scope of the question asked it's pretty easy to port from win forms to web forms, if this is just part of a larger solution you're almost certainly looking at an architecture overhaul. There are all kinds of issues related to state management, caching strategies, identity management, etc that require a very different approach when you move to web forms. – Pete M Apr 08 '11 at 15:50
  • +1 The easy way will only be easy if you have a good understanding of (1) ASP.Net (2) WinForms (3) the original WinForms application itself. Of course a good understanding of all those things will be very useful in any future development you do on that application, so it is worth acquiring. – MarkJ Apr 08 '11 at 16:19
  • -1 OoooOOOOoooOOOooo be careful of the spoooOOOOooooky web forms! lol and read HTML and ASP.NET books... We all know coding is something gained through practical experience..... Please give me something specific to work with like a tutorial link on exporting/converting. I rephrased my answer and included the files and more specific questions. Rephrase your answer and I'll undo the -1 haha – Brian McCarthy Apr 08 '11 at 16:40
  • What he means is if you don't understand how the server/client interaction works within an asp.net application and the page lifecycle you can be in for a world of difficult to understand hurt, and was just advising caution when you move forward. – asawyer Apr 08 '11 at 16:49
  • 1
    @Brian - While I do appreciate the comedy you have completely missed my point. I'm not saying read up on ASP.NET and HTML (that's a given!), what I'm saying is learn how WebForms work behind the scenes (*HTTP* and the ASP.NET life cycle are chief among them). There are *no* "step-by-step" guides to transforming WinForms to WebForms. The two technologies are too different from each other for a straightforward conversion. Pete M just mentioned a few of the ways that they are fundamentally different. You might get away without knowing the inner workings for small apps, but not for anything larger – System Down Apr 08 '11 at 17:06
  • @Pete M: Will he did give me a good laugh by threatening me with a *-1*. A good laugh deserves a good answer I suppose :) – System Down Apr 08 '11 at 17:47
  • Naw, it wasn't a threat. I was trying to be nice and funny about it + the vote is locked in until the answer is edited. – Brian McCarthy Apr 08 '11 at 18:03
2

Yes, the way you want to do it is the way I have done this many times.

Just copy the methods from your code behind and paste them into the code behind of your asp.net page. Some of your methods are not compatible because they are not supported in asp.net but you will find that our real quick when you build the project.

Create your web page with the controls having exactly the same name as the ones in the winform. When you build, all you have to do is fix your errors and you are on your way.

It looks like you are hooked up to some service so of course you will need to reference that.

RJ.
  • 3,173
  • 4
  • 35
  • 44
1

Yeah, that's the general idea. I'd pay special attention to any concerns related to using AcompServiceClient in a stateless web environment. It's hard to say whether you have to rethink how you're using that or not without knowing anything about what it is, how it works or how it's consumed.

It doesn't look like you're doing anything else that relies on running in a stateful environment. You're just pulling string values from a variety of textboxes to create some XML and push it to a service. All of that should port over smoothly. You might want to look at adding some client side validation rules, but other than that it looks straight forward.

You'll want to change how you're populating your DropDownList. Those work a little different between win and web forms. It wants to be bound to a datasource in webforms.

Pete M
  • 2,008
  • 11
  • 17
  • @Pete, thanks for your response. What other info would you need? Should I add the app.config or the webservicedata.xml file codes to give you a better idead of what I'm working with? – Brian McCarthy Apr 08 '11 at 15:32
  • It comes down to "what is it?". Is it a web service? COM API? Other? Moving from winforms to webforms is to move from stateful to stateless. Depending on what that proxy is and how it's used, this may be a problem. Say it has some kind of initializing logic that's supposed to hang around for a duration longer than a single postback. You'll have extra persistence work to do that you didn't have to think about in a stateful winforms environment. – Pete M Apr 08 '11 at 15:36
  • Another concern would be the fact that you're now physically executing on a server as opposed to the individual client. Maybe this proxy makes some assumptions about where it's operating from. That could be a gotcha. – Pete M Apr 08 '11 at 15:37
  • I see you added a bit of info pointing to it being a webservice. If the proxy you're working with is just a generated service proxy from studio, then you're probably good to go. "Probably" being the keyword... – Pete M Apr 08 '11 at 15:45
  • @Pete, it's not letting me add a webform asp.net page into this project so it looks like I have to create a whole new project. Is there no wizard or application to do this? – Brian McCarthy Apr 08 '11 at 16:00
  • That's because under the hood ASP.NET is nothing like winforms and can never co-exist. You should have "ASP.NET Web Application" as an available project type under File -> New -> Project. If you're starting from scratch to the point of never creating an ASP.NET project before, I STRONGLY recommend walking through some tutorials before you get started. I've always been a fan of the videos on http://www.asp.net/get-started as a quick start. – Pete M Apr 08 '11 at 16:06
  • Should I copy the app.config file to the web.config file and the webservicedata.xml, XMLFile1.xml, XmlToSchema1.xsd files as well? – Brian McCarthy Apr 08 '11 at 16:10
  • I have no idea, sorry. That kind of implementation detail is well outside the scope of the question as well. The resource listed above is a good next step for help with understanding basic configuration. – Pete M Apr 08 '11 at 16:17
  • You can't just copy the app.config to the web.config, the structure of the two configuration files is different. What might usually go inside the app.config usually goes inside the portion of the web.config, unless it's a connection string which has its own section. – System Down Apr 08 '11 at 17:26
  • Ok, thanks. Then I won't mess w the .config files. I added the existing webservice off the webserver so it uses the same webservice. – Brian McCarthy Apr 08 '11 at 18:06