Category Archives: Visual Studio 2013

How to fill a DataGridView using Entity Model in VB.NET

Here is a quick way of filling up and displaying the records from a table in a DataGridView

This post assumes that you already have created a EntityModel in your project

My Entity Model is called SuperHREntities and I have a table called Employees in it

The trick is to you use the .ToArray() after your table name in the following example to display the records.  If you simply try to use db.Employees then the records will not be displayed.

 

Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim db As New SuperHREntities
DataGridView1.DataSource = db.Employees.ToArray()

End Sub

 

 Click on the Image to see it in full screen.

How to fill DataGridViewUsing Entity Model

 

 

 

 

 

Implement PagedList in VB.NET MVC3 or MVC4

How to use Paging using the PagedList

Once you have installed the PagedList package from NUGET

 

Controller

 

Add the following two imports at the top of the header

Imports PagedList
Imports PagedList.Mvc

 

 

Your Linq or SQL query should be sorted

eg.

Dim v = (From vid In db.qry_Videos Where vid.CatID = CatID Order By vid.OrderByID Select vid)

ViewBag.CurrentSort = v   ‘ This is most important you need to tell the view what your current sort order is

Return View(v)

 

 

View

In your view add the following imports at the top of the view

@imports PagedList
@imports PagedList.Mvc

 

Add the style sheet in your view as well. This will help you render your pager properly

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

 

Wherever you want your pager to appear use the following

index” is the name of your view, if your view is named different then change it to your view

page”  – this is the variable which stores the current page number

“ViewBag.search – this is the ViewBag which you used in your controller

OnlyShowFivePagesAtATime  – this tells the pager how many buttons will be shown at one time ( this option shows

@Html.PagedListPager(DirectCast(ViewBag.CurrentSort, IPagedList), Function(page) Url.Action(“index“, New With {.page = page, .criteria = ViewBag.search}), PagedListRenderOptions.OnlyShowFivePagesAtATime)

How to Decode and Encode MIME data

This function will decode the mime data contained in a string into a BYTE and then it can be saved onto your desktop using the FileStream.

————————————————————————-
Dim byteData As Byte()
byteData = FromBase64(strMimeData)
Dim oFileStream As System.IO.FileStream
oFileStream = New System.IO.FileStream(“c:\temp\” & Me.txtAttachFileName.Text, System.IO.FileMode.Create)
oFileStream.Write(byteData, 0, byteData.Length)
oFileStream.Close()

This function will encode the data contained in a BYTE into MIME data which will be returned as a string
———————————————————————–

Public Function EncodeToBase64(ByVal data() As Byte) As String
If data Is Nothing Then Throw New ArgumentNullException(“data”)
Return Convert.ToBase64String(data)
End Function

This function will decode the MIME data contained in a string  and the binary data will be returned as a BYTE
———————————————————————–

Public Function  DecodeFromBase64(ByVal base64 As String) As Byte()
If base64 Is Nothing Then Throw New ArgumentNullException(“base64″)
Return Convert.FromBase64String(base64)
End Function

Fix the SQL Single quote problems in SQL in vb.net

When the sql contains the single quotes then they have to be properly escaped otherwise it will give errors

just pass the field value which you want to check and the following function will return you the properly escaped string.

eg. o’brien will be returned as o”brien and this will not cause your sql to break.

Function FixSql(ByVal sStr As String) As String

FixSql = Replace(sStr, “‘”, “””)

End Function

How to get the file extension from the filepath

The following function returns the file extension only.

eg. using the following function

GetFileExtension(“c:\test\file.aspx”) will return “.aspx”

Here is the complete function, copy and paste it in your form or a module

Function GetFileExtension(ByVal sFilePath As String) As String
' Dim fullPath As String = "c:\MyDirectory\MYFile.txt"
Dim sExtension As String = IO.Path.GetExtension(sFilePath)
Return (sExtension)
End Function

Redirect to a new page in vb.net

Here is an example of a stand alone page which can help you to move your traffic to your new page.

Just copy the following lines into a note pad , remember to change the url to the actual page where you want to redirect.

Save the page with a .aspx extension.

eg. If you save this page as “OLD.ASPX” then every time someone types in this url eg. “http://vbnet.redirect.com.au/old.aspx”

it will redirect to the new page

<%@ Page Language="VB" Debug="true" %>

How to add Nuget Packages

How to Add Packages using NUGET in Visual Studio

Step 1

In Visual Studio Go to Tools > Library Package Manager > Manage Nuget Packages for Solution.

 

How to add Nuget Packages
How to add Nuget Packages

 

Step 2

You would get the following screen, you can check the packages which you have already available in your solution, you can search for new packages and install them to your solution.

Remember:  These packages need to be downloaded , each time you create a new Visual Studio solution

 

Packages in NUGET
Packages in NUGET

How to fix : Ajax.BeginForm doing full postback instead of partial.

I realised that in my MVC5 project  in Visual Studio I could not get the Ajax.BeginForm to work properly. It will always do a full postback.

 

I was trying to complete a Video Rating Page, where there are buttons to rate a video and once the user presses one of the rating button, it should thank the user and display the new rating value. I wanted this to shown on the same page without reloading the page, So I considered using AJAX.BeginForm which will simply work fine.

 

In order to have this AJAX functionality you will have to download the AJAX from NUGET.

 

Step 1

Install the following two highlighted packages from NUGET. Click here to check how to load Packages from NUGET in Visual Studio

 

Nuget

 

Step 2

Once you have installed the above two packages through NUGET then do the following.

Go to the _Layout file in your _shared  folder in the Views. ( Note that I am using VB.NET but the example will work for C# also.

 

_Layout in the Shared Folder
_Layout in the Shared Folder

 

Open the _Layout.vbhtml file and browse to the bottom.

 

You will see the following two lines.

@Scripts.Render(“~/bundles/jquery”)
@Scripts.Render(“~/bundles/bootstrap”)

 

Now add the following line after the above two lines.
<script src=”~/Scripts/jquery.unobtrusive-ajax.min.js”></script>

 

I made a mistake of adding this script at the top of my page and my AJAX calls refused to work.

Then I realised that the AJAX has to be loaded after loading the jquery. So I added it at the last.

 

 

 

 

AjaxCallsinMVC5
Add the script at the exact position show.

 

 

Step 3

Run your application.

Your AJAX Calls should work fine.

 

 

 

 

 

 

Could not load file or assembly ‘Antlr3.Runtime’ Error in Visual Studio 2013 Express

While working on the Visual Studio 2013 Express, I was happily burning the midnight oil to produce a new application, the application was coming on nicely.

But When I started work next day, suddenly I started getting the following error message.


Could not load file or assembly 'Antlr3.Runtime' or one of its dependencies. The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))

 

After searching for long and trying different optionx the following solution did work for me and I was back in action. Hope you can benefit it from as well.

Step 1
Go to the file explorer and search for %Temp% (See the screen below)

Antlr3RuntimeIssueScreen1

Step 2
You would see lots of file in Temp folder. Delete them all.

Antlr3RuntimeIssueScreen2

Step 3

Run Visual Studio 2013 Express again and you would be able to work again on your project.