Results 1 to 2 of 2

Thread: how can i use controller to execute this code

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2020
    Location
    Kampala
    Posts
    24

    how can i use controller to execute this code

    I would want it in a way that after the Name has been enter then the account number is generated automatically iam using MVC visual 2019

    Name:  Screenshot 2022-03-04 at 11-08-50 Create - My ASP NET Application.png
Views: 179
Size:  13.2 KB
    Code:
     
        Protected Sub Names_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Names.TextChanged
            Dim maxcode As Double
            Dim sqlConnection1 As New SqlConnection(conString)
            Dim objCommand As SqlCommand = New SqlCommand()
            Dim objCommand2 As SqlCommand = New SqlCommand()
            Dim cmd As New SqlCommand
            cmd.CommandText = "delaccount"
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Connection = sqlConnection1
            ' cmd.Parameters.Add("@c", SqlDbType.NVarChar, 12).Value = TextBox1.Text
            sqlConnection1.Open()
            cmd.ExecuteReader()
            sqlConnection1.Close()
    
            Dim sqlConnection12 As New SqlConnection(conString)
            'Dim objCommand2 As SqlCommand = New SqlCommand()
            Dim objCommand22 As SqlCommand = New SqlCommand()
            Dim cmd2 As New SqlCommand
            cmd2.CommandText = "sendsingleaccount"
            cmd2.CommandType = CommandType.StoredProcedure
            cmd2.Connection = sqlConnection12
            cmd2.Parameters.Add("@c", SqlDbType.NVarChar, 12).Value = Names.Text
            sqlConnection12.Open()
            cmd2.ExecuteReader()
            sqlConnection12.Close()
    
    
            Using Con As New SqlConnection(conString)
                Con.Open()
                Using Com As New SqlCommand("Select * from Zero", Con)
                    Using RDR = Com.ExecuteReader()
                        If RDR.HasRows Then
                            Do While RDR.Read
                                maxcode = RDR.Item("zero").ToString
                            Loop
                        End If
                    End Using
                End Using
                Con.Close()
                ' Exit Sub
            End Using
    
            ' include code
    
            Using Con As New SqlConnection(conString)
                Con.Open()
                Using Com As New SqlCommand("Select * from max4", Con)
                    Using RDR = Com.ExecuteReader()
                        If RDR.HasRows Then
                            Do While RDR.Read
                                maxcode = RDR.Item("maxcode").ToString()
                            Loop
                        Else
                            maxcode = maxcode + 1
                        End If
                    End Using
                End Using
                Con.Close()
                'Exit Sub
            End Using
            If Account.Text = "" Then
                Select Case maxcode
                    Case 0 To 9
                        Account.Text = Microsoft.VisualBasic.Right(Str(Year(Now)), 2) + "-0000" + Trim(Str$(maxcode))
                        'Me!Account = "06-0000" + Trim(Str$(maxcode))
                    Case 10 To 99
                        Account.Text = Microsoft.VisualBasic.Right(Str(Year(Now)), 2) + "-000" + Trim(Str$(maxcode))
                        'Me!Account = "06-000" + Trim(Str$(maxcode))
                    Case 100 To 999
                        Account.Text = Microsoft.VisualBasic.Right(Str(Year(Now)), 2) + "-00" + Trim(Str$(maxcode))
                        'Me!Account = "06-00" + Trim(Str$(maxcode))
                    Case 1000 To 9999
                        Account.Text = Microsoft.VisualBasic.Right(Str(Year(Now)), 2) + "-0" + Trim(Str$(maxcode))
                        'Me!Account = "06-0" + Trim(Str$(maxcode))
                    Case Else
                        Account.Text = Microsoft.VisualBasic.Right(Str(Year(Now)), 2) + "-" + Trim(Str$(maxcode))
                        'Me!Account = "06-" + Trim(Str$(maxcode))
                End Select
                
            End If
        End Sub

  2. #2
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,516

    Re: how can i use controller to execute this code

    Hi Makumbi,

    I am afraid I can't answer your question. However I do have a few coding tips:
    1. Provide all relevant components. In an attachment if necessary.
    2. Fully quailified references (for example "System.EventArgs") are usually unnecessary when using Imports statements or project level imports.
    3. I prefer to initialize variables when they're declared. (for example: "Dim maxcode As Double = 0")
    4. Arithmetic operations such as "maxcode = maxcode + 1" can usually be shortened: "maxcode += 1".
    5. I prefer to use the Nothing keyword instead of a pair of quotes ("").
    6. I recommend avoiding the Microsoft.VisualBasic namespace. Everything you refer to should have an alternative in another namespace.
    7. Turn on Option Strict. Use "As" clausules to explicitly declare a variable's type and explicit data type conversions.
    8. And your entire Select Case block can be reduced to Account.Text = $"{DateTime.Now:yy}-{maxcode:D5}"

    Following these tips may give you a little extra work in the short run but reduce the chance of bugs in your program.
    Last edited by Peter Swinkels; Mar 4th, 2022 at 07:08 AM.

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