none
Converting a vb.net app to asp.net RRS feed

  • Question

  • Good day to you all tech gurus

    I have some vb.net desktop apps that l want to convert to asp.net web based apps. I have been practicing asp.net in vb.net and now understand a few things about connecting to database, updating and querying the database in asp.net. Would you guys care to assist me so that l can work very fast on a project that is supposed to be completed like yesterday. l want to know how l can work with datasets in asp.net forms. Here is a small test project that l would love to be assisted and then l can take it up from there:

    SQL

    database name: asp_test

    table name: test

    first_name    nvarchar(50)  
    surname       nvarchar(50) 
    ID                 nvarchar(50)

    stored procedures: (1)sp_saveNames (2)sp_getLikeCustomerSurname (3)sp_deleteUser

    Class test under BusinessLogik project - with variables, properties and methods:

    Imports Microsoft.Practices.EnterpriseLibrary.Common
    Imports Microsoft.Practices.EnterpriseLibrary.Data
    Imports Microsoft.Practices.ObjectBuilder
    Imports System.Data.Common
    Imports System.Data
    Public Class Test
    #Region "Variables"
        Private vfirst_name As String
        Private vsurname As String
        Private vID As String
        Public log As Boolean
        Private dbs As Database

    #End Region

    #Region "Properties"
     
        Public Property name() As String
            Get
                Return vfirst_name
            End Get
            Set(ByVal value As String)
                vfirst_name = value
            End Set
        End Property
        Public Property surname() As String
            Get
                Return vsurname
            End Get
            Set(ByVal value As String)
                vsurname = value
            End Set
        End Property
        Public Property ID() As String
            Get
                Return vID
            End Get
            Set(ByVal value As String)
                vID = value
            End Set
        End Property
     

    #End Region

    #Region "Constructor"
        Public Sub New(ByVal conn As String)
            dbs = DatabaseFactory.CreateDatabase(conn)
        End Sub
    #End Region

    #Region "Methods"

        Public Function save() As Boolean
            Dim cmd As DbCommand = dbs.GetStoredProcCommand("sp_saveNames")
            dbs.AddInParameter(cmd, "@first_name", DbType.String, vfirst_name)
            dbs.AddInParameter(cmd, "@surname", DbType.String, vsurname)
            dbs.AddInParameter(cmd, "@ID", DbType.String, vID)

            Try
                Dim ds As DataSet = dbs.ExecuteDataSet(cmd)
                MsgBox("Customer saved successfully.", MsgBoxStyle.Information, "ASP_GUI")
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Information, "ASP_GUI")
            End Try
        End Function

        Public Function getLikeCustomerSurname(ByVal custsurname As String) As DataSet
            Dim cmd As DbCommand = dbs.GetStoredProcCommand("sp_getLikeCustomerSurname")
            dbs.AddInParameter(cmd, "@surname", DbType.String, custsurname)
            Try
                Dim ds As DataSet = dbs.ExecuteDataSet(cmd)
                If ds.Tables().Count > 0 AndAlso ds.Tables(0).Rows.Count > 0 Then
                Else
                    'MsgBox("There are no records to show.", MsgBoxStyle.Information, "ASP_GUI")
                End If
                Return ds
            Catch ex As Exception
                Dim ds As DataSet = Nothing
                MsgBox(ex.Message, MsgBoxStyle.Information, "ASP_GUI")
                Return ds
            End Try
        End Function

        Public Function deleteUser(ByVal lastName As String) As Boolean
            Dim cmd As DbCommand = dbs.GetStoredProcCommand("sp_deleteUser")
            dbs.AddInParameter(cmd, "@surname", DbType.String, lastName)
            Try
                Dim i As Integer = dbs.ExecuteNonQuery(cmd)
                If i <> 0 Then
                    Return True
                Else
                    Return False
                End If
            Catch ex As Exception
                Return False
            End Try
        End Function
    #End Region
    End Class

    Here is asp.net webform under project WebTest, and it calls methods from class test above

    Imports businessLogik
    Imports System.Data.SqlClient
    Imports Microsoft.Reporting.WebForms

    Partial Public Class WebForm1
        Inherits System.Web.UI.Page
     
        Protected Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
            Dim clsTest As New Test("asp_test")
            Try
                clsTest.name = txtFirstName.Text
                clsTest.surname = txtLastName.Text
                clsTest.ID = txtID.Text
                If clsTest.save Then
                Else
                End If
            Catch ex As Exception

            End Try
        End Sub

        Protected Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
          
        End Sub
     
        Protected Sub OnSelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GridView1.SelectedIndexChanged
            Dim row As GridViewRow = GridView1.SelectedRow
            txtFirstName.Text = row.Cells(0).Text
            txtLastName.Text = row.Cells(1).Text
            txtID.Text = TryCast(row.FindControl("lblIDNumber"), Label).Text
        End Sub

        Protected Sub GridDataSource_Selecting(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs) Handles GridDataSource.Selecting

        End Sub
    End Class

    Here is the asp.net script source code

    <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="webTest.WebForm1" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:Label ID="Label1" runat="server" Text="FIRST NAME" Width="157px"></asp:Label>
            <asp:TextBox ID="txtFirstName" runat="server" Width="305px"></asp:TextBox><br />
            <br />
            <asp:Label ID="Label2" runat="server" Text="LAST NAME" Width="157px"></asp:Label>
            <asp:TextBox ID="txtLastName" runat="server" Width="305px"></asp:TextBox>&nbsp;<br />
            <br />
            <asp:Label ID="Label4" runat="server" Text="NationalID" Width="157px"></asp:Label><asp:TextBox
                ID="txtID" runat="server" Width="305px"></asp:TextBox><br />
            <br />
            <asp:Button ID="btnSave" runat="server" Text="Save" Width="152px" /><br />
            <br />
            <br />
            <asp:Label ID="Label3" runat="server" Text="SEARCH CUSTOMER" Width="173px"></asp:Label>
            <asp:TextBox ID="txtSearch" runat="server" Width="296px"></asp:TextBox>
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
            <asp:Button ID="btnSearch" runat="server" Text="Search" Width="97px" /><br />
            <br />
          
                     <asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
                    runat="server" AutoGenerateColumns="false" DataSourceID="GridDataSource" AllowPaging="true"
                    OnSelectedIndexChanged="OnSelectedIndexChanged">
                    <Columns>
                        <asp:BoundField DataField="first_name" HeaderText="First Name" ItemStyle-Width="150" />
                        <asp:BoundField DataField="surname" HeaderText="Last Name" ItemStyle-Width="150" />
                        <asp:TemplateField HeaderText="ID">
                            <ItemTemplate>
                                <asp:Label ID="lblIDNumber" Text='<%# Eval("ID") %>' runat="server" />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:LinkButton Text="Select" ID="lnkSelect" runat="server" CommandName="Select" />
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
                <asp:SqlDataSource ID="GridDataSource" runat="server" ConnectionString="<% $ConnectionStrings:asp_test %>"
                    SelectCommand="SELECT first_name, surname, ID FROM test" FilterExpression="surname LIKE '%{0}%'">
                    <FilterParameters>
                        <asp:ControlParameter Name="surname" ControlID="txtSearch" PropertyName="Text" />
                    </FilterParameters>
                </asp:SqlDataSource>         
        </div>
        </form>
    </body>
    </html>

     My questions from the above code are:

    1)How can l call methods and set datasets in asp.net forms as l would do in vb.net

    eg in vb.net under key press event of a text box, filter and display to GridView1 as you type:

    Private Sub txtSearch_KeyPress
            Dim clsTest As New Test("asp_test")
            Dim ds As DataSet = Nothing

            Try
                ds = clsTest .getLikeCustomerSurname(txtSearch.Text)
                GridView1.ClearSelection()
                GridView1.DataSource = ds.Tables(0).DefaultView
                GridView1.Refresh()

            Catch ex As Exception
            End Try
    End

    2)How can l, on click event of a button, refresh say GridView2 so that it displays what l have saved to database based on a certain field, say l want it to retrieve all records with same last name on click event of save button.

    With this information, l will do my projects faster as l will use the same classes and methods defined in the BusinessLogik. Thanks in advance.

    Wednesday, July 26, 2017 9:19 AM

All replies

  • With this information, l will do my projects faster as l will use the same classes and methods defined in the BusinessLogik. Thanks in advance

    What business logic? You talked about it, but it doesn't seem to be any there. What I do see is a lot of DB logic in the code behind file that is tightly coupled to the form instead of loose coupling of code and placing DB code where it should be in the DAL.

    Maybe you should consider these things in the future in Web development.

    https://en.wikipedia.org/wiki/Separation_of_concerns

    https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93presenter

    MVP for Web form UI

    http://www.codeproject.com/Articles/228214/Understanding-Basics-of-UI-Design-Pattern-MVC-MVP

    You really are not going to find too much in VB.

    https://msdn.microsoft.com/en-us/library/ff649571.aspx

    https://msdn.microsoft.com/en-us/library/bb384398.aspx

    This can be applied to a ASP.NET Web form solution too.

    https://www.codeproject.com/Articles/21115/Building-an-N-Tier-Application-in-VB-NET-in-Step

    https://dzone.com/articles/reasons-move-datatables

    http://lauteikkehn.blogspot.com/2012/03/datatable-vs-list.html

    It's too late now all of this for your current project.

    You can post to the ASP.NET forum for help, the Web form section.

    http://forums.asp.net/

    Wednesday, July 26, 2017 10:17 AM
  • When posting code use the code button as this makes reading code much better.

    One thing about moving from desktop to web in general, best to learn how to write web apps, in this case ASP.NET and leave behind desktop practices as there are many differences between the two.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, July 26, 2017 11:05 AM
    Moderator
  • Tangozop,

    Forget it, in the First versions of .Net it was intended that Forms where used in a kind of same way in Windows Forms and ASP.Net. 

    That is 15 years ago. It was to difficult and already with the next (not "Next") version of ASP.Net Microsoft was back on in fact Classic ASP with some new scripting methods. 

    Therefore simple start new, choose the tools you like and use your design and knowledge around your current windows forms application. 

    Be aware that Microsoft is currently lead by Marketing guys. Gates was still a developer. Names of products don't not always show what they are (Think on Windows 8). 

    Here a nice representation around that about current Web tools in Visual Studio. 

    http://www.codedigest.com/posts/18/difference-between-aspnet-and-aspnet-core

    However, Visual Studio .Net has one advantage, the community version is complete freeware and it is in my eyes (despite in my experience not really fast) one of the best version made. 


    Success
    Cor


    Wednesday, July 26, 2017 11:28 AM
  • I have some vb.net desktop apps that l want to convert to asp.net web based apps. I have been practicing asp.net in vb.net and now understand a few things about connecting to database, updating and querying the database in asp.net. Would you guys care to assist me so that l can work very fast on a project that is supposed to be completed like yesterday. l want to know how l can work with datasets in asp.net forms. Here is a small test project that l would love to be assisted and then l can take it up from there:

     My questions from the above code are:

    1)How can l call methods and set datasets in asp.net forms as l would do in vb.net

    2)How can l, on click event of a button, refresh say GridView2 so that it displays what l have saved to database based on a certain field, say l want it to retrieve all records with same last name on click event of save button.

    Hi tangozop,

    About your question, you want to do some thing in asp.net, but this forum is to discuss and ask questions about the Visual Basic programming language, IDE, libraries, samples, and tools. So I suggest you to ask your question at asp.net forum, you can get more helpful help.

    Thanks for your understanding.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, July 27, 2017 5:46 AM
    Moderator