Tag Archives: VB.NET

VB.NET – You must call the “WebSecurity.InitializeDatabaseConnection” method before you call any other method of the “WebSecurity” class.

Problem description:

If you are getting the error while running your MVC4 application in VB.NET

You must call the “WebSecurity.InitializeDatabaseConnection” method before you call any other method of the “WebSecurity” class.

Solution

Add the following line in the GLOBAL.ASX

Capture

 

Ensure that you are using the correct connection string and other information as per your own application as  shown in the highlighted line above.

VB.NET – ASP NET MVC Area routing/multiple routes issue in VB.NET

Scenario: If you have two controllers with the same name in different areas, then you would end up with errors as shown below, while there are numerous examples available on the web but it took me a while to figure out the correct solution for VB.

Hope it help you and saves you time.

Problem Description

I have a HOME controller in my MVC4 application and then I created a new AREA called SUPERADMIN. I then created a HOME controller in that area as well.I received the following error.

Error with duplicate controller names

 

 

 

Solution:

 

 

1. Fix the Route.Config in your MVC4 project under APP_Start folder. See the yellow coloured text,

 

vbnull is for constraints( so we are not adding special constraints

the next one “RealEstateVB” is the namespace , RealEstateVB is the name of my application and is taken as the default namespace.

Adding Namespace in ROUTE

2. Now Go to your AREA. I had an area called SUPERADMIN.

See the highlighted text. You have to define the namespace. eg. I have used “RealEstateVB.Areas.Superadmin for my SuperAdmin area.

Also make a note how I have added superadmin in front of the /{controller}/{action}/{id}

Adding namespace in Area

 

 

This should get your project going without any errors. For each area you have you will need to fix the xxxAreaRegistration.vb file as described in the above step(2)

Role based Authorization in MVC4 using VB.NET

Here is a simple way to do the role based Authorization in your MVC4 Controllers using VB.NET

IF you have a role called Client then simply add the following text on top of your each ActionResult( which displays the view)

<Authorize(Roles:=”Client”)>

 

See the following example. The Index View has been restricted and can only be viewed by someone in the Client role.

 <Authorize(Roles:="Client")>
        Function Index() As ActionResult
            Return View(db.Purchasers.ToList())
        End Function

Password protect complete MVC4 site in VB.NET

If you want to password protect some pages on your MVC4 site using the Forms Authentication then it is best to to protect the whole site and then selectively allow Anonymous Access to the pages which are to be shown to every visitor.
authentication in MVC4

Open the FilterConfig.VB file and add the line

filters.Add(New AuthorizeAttribute)   in it

 

Once you run your application you would see that all the pages of your website are now protected, the users have to register before they can access.

 

Imports System.Web
 Imports System.Web.Mvc
Public Class FilterConfig
 Public Shared Sub RegisterGlobalFilters(ByVal filters As GlobalFilterCollection)
 filters.Add(New HandleErrorAttribute())
 filters.Add(New AuthorizeAttribute)
 End Sub
 End Class

 

If you need to allow some pages for anonymous view then add the following decoration.

<AllowAnonymouse()> on each page which you want to open up for anonymous access.

 

    <AllowAnonymous()>
    Function Index() As ActionResult
        ViewData("Message") = "Modify this template to jump-start your ASP.NET MVC application."
        Dim rep As New RepositoryRealEstate

        ViewBag.AgentCount = rep.GetAgentsCount
        ViewBag.ProperyCount = rep.GetPropertyCount
        ViewBag.VendorCount = rep.getvendorcount
        Return View()
    End Function

How to create a GUID in VB.NET

Here is a small function to return the GUID.

GUID stands for Globally Unique Identifiers. GUIDs are 128-bit globally unique identifiers and they are generated automatically based on close to two zillion frequently varying factors.
It has extremely low possibility that a generated GUID would equal to another GUID.

Function GenerateGUID() as string
return System.Guid.NewGuid.ToString()
End function

How to use Application Settings in Windows Forms using VB.NET

Learn how to use Applications Settings in Visual Studio 2013 Winform project in VB.NET

 

Here is a simple way of storing applications settings.

 

Step 1

In your Visual Studio Project open the settings.

Winform Settings
Winform Settings

Step 2

When you click on the Settings as shown in Step 1, you would get the following screen.

You can create your settings as shown below by providing your customised values in the Value column as shown below

Best-Answer.net

 

 

Step 3

 

Here is how you will use those values in your project .

Best-Answer.net Learn how to use settings in winform

How to create a Datatable programatically in VB.NET

Dim dt as new DataTable   
        ' Add the columns which you need in your datatable
        dt.Columns.Add("SNo", GetType(Integer))  '1
        dt.Columns.Add("Details", GetType(String))  '2
        dt.Columns.Add("Qty", GetType(String))  '3
        dt.Columns.Add("UnitRate", GetType(Double)) '4
        dt.Columns.Add("ApplyTax", GetType(Boolean))  '5
        dt.Columns.Add("ExTaxTotal", GetType(Double))  '6
        dt.Columns.Add("Tax", GetType(Double))  '7
        dt.Columns.Add("InclTaxTotal", GetType(Double)) '8
       
        ' Create a new row for your table
        Dim drow = dt.NewRow()

        ' Now add the items in your newly created row
        drow.Item(0) = 1
        drow.Item(1) = "My Details go here"
      
        drow.Item(2) = 1
        drow.Item(3) = 234
        drow.Item(4) = 1
        drow.Item(5) = drow.Item(2) * drow.Item(3)   ' You can also calculate the values
        drow.Item(6) = drow.Item(2) * 0.1
        drow.Item(7) = drow.Item(5) * drow.Item(6)


        ' Now add this row to the table
        dt.Rows.Add(drow)

        ' If you want you can display this  data in a datagrid  
        Me.DataGridView1.DataSource = dt

How to get the list of installed printers in vb.net

To get the list of installed printers on your computer use the following code which will populate a Combo box with the list of installed printers.

 

Private Sub PopulateInstalledPrintersCombo()
        ' Add list of installed printers found to the combo box. 
        ' The pkInstalledPrinters string will be used to provide the display string. 
        Dim i As Integer
        Dim pkInstalledPrinters As String

        For i = 0 To Printing.PrinterSettings.InstalledPrinters.Count - 1
            pkInstalledPrinters = Printing.PrinterSettings.InstalledPrinters.Item(i)
            comboInstalledPrinters.Items.Add(pkInstalledPrinters)

        Next
    End Sub

Learn how to printer address labels on Dymo Label Printer in VB.NET