Category Archives: Visual Studio 2013

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

Print address label on Dymo using VB.NET – Address label printing on Dymo using VB.NET

If you have the need to print the address labels then this script will get you started, this is a very simple script which will get

you started. You can visit Dymo website site and checkup the SDK for complex scenarios.

 

Assumption

I assume that you have already attached a Dymo Label printer to your computer.

I am using Dymo LabelWriter 400

 

Step 1

You would need to add references to your your project

Add reference to your project

 

 

Step 2

On top of your form add the following imports.

Imports DYMO.Common
Imports DYMOPrintingSupportLib
Imports DYMO.DLS.Runtime

 

Step 3

Add a button on your form  and call it btnPrintLabel

Step 4

Add the following lines of code to the click event of your button

 Private Sub btnPrintLabel_Click(sender As Object, e As EventArgs) Handles btnPrintLabel.Click
 Dim Label As DYMO.Label.Framework.ILabel
 Label = DYMO.Label.Framework.Framework.Open("C:\Users\User\Documents\DYMO Label\Labels\Holroyd Council.label")
 Label.SetAddressText(0, "First address Line" & vbCrLf & "Second Line" & vbCrLf & "Third Line")

 Label.Print("DYMO LabelWriter 400")  ' This is the name of my printer, replace it with your printer name
 End Sub

How to find out the names of installed printers in VB.NET

Using Grid.MVC in MVC4 using VB.NET

There are numerous example for C3 but I had to spend quite some time to adapt it to my project using VB.NET

 

Hope it can be of help to you.

I assume that you have

already added the GridMVC package from NUGET.

Here are step by step guide

Controller

No change needed , see a sample below,

Function Index() As ActionResult
Return View(db.Categories.ToList())
End Function

View

Add the following import at the top of your view

@imports  GridMVC.html

 

You would need to reference the following also

 

Add the stylesheets (check the location after you install the GridMVC package)

<link href=”~/Content/Gridmvc.css” rel=”stylesheet” />
    <link href=”~/Content/gridmvc.datepicker.min.css” rel=”stylesheet” />
For your paging support

Install the PagedList package also

Once you have installed this package then add the following reference to your view, this will help you format your paging buttons

properly.

 <link href=”~/Content/PagedList.css” rel=”stylesheet” />

Add the following code for your Grid to appear – In the following examples my Model has three fields – CatId, CatName

and ParentCatId. Make changes to suit your needs.

@Html.Grid(Model).Columns(Function(columns)
                                   
                              columns.Add(Function(o) o.CatID).Titled(“Cat #“).SetWidth(40)
                                 
                              columns.Add(Function(o) o.CatName).Titled(“Category“).SetWidth(100)
                                  
                              columns.Add(Function(o) o.ParentCatID).Titled(“Parent Cat #“).SetWidth(40)
                          
                          End Function
).WithPaging(10).Sortable(True)