Tag Archives: VB.NET

Select and edit a record in GridView in vb.net

If you want to select a record in a Gridview in VB.Net and then edit that record in a separate page, then you can use the following code to give you an idea.

I assume that you alredy know how to add a gridview and populate it using vb.net

You would need to add a hyperlink field in the gridview columns as shown belo

The headertext will show what will appear in the header row
datatextfield will be name of your datafield which is the primary key for the data
datanavigateurlformatstring  – this field will construct the name of your page which the system will navigate to if someone clicks on the edit button
datanavigateurlfields  is again the primary key eg. My primary key was “NewsId”
DataTextFormatString  tells what will be displayed on the data gridl eg. I want to show a hyperlink called “Edit”

so whenever someone clicks on the edit button the system will take you to the “EditNews.aspx?q=1 where 1 will be the NewsID

<asp:hyperlinkfield headertext=”Edit” datatextfield=”NewsID” datanavigateurlformatstring=”EditNews.aspx?q={0}” datanavigateurlfields=”NewsID” DataTextFormatString=”Edit” Text=”Edit” />

Here is the complete code which shows where the hyperlink code will sit

<asp:GridView ID=”GridView1″ runat=”server” AllowPaging=”True”
AllowSorting=”True” AutoGenerateColumns=”False” DataKeyNames=”NewsID”
DataSourceID=”SqlDataNews” Width=”617px”>

<asp:BoundField DataField=”NewsID” HeaderText=”NewsID” InsertVisible=”False”
ReadOnly=”True” SortExpression=”NewsID” />

<asp:BoundField DataField=”NewsDate” HeaderText=”NewsDate”
SortExpression=”NewsDate” />
<asp:BoundField DataField=”NewsStartDate” HeaderText=”NewsStartDate”
SortExpression=”NewsStartDate” />
<asp:BoundField DataField=”NewsEndDate” HeaderText=”NewsEndDate”
SortExpression=”NewsEndDate” />
<asp:BoundField DataField=”NewsShortDescripton”
HeaderText=”NewsShortDescripton” SortExpression=”NewsShortDescripton” />

<asp:hyperlinkfield headertext=”Edit”

datanavigateurlfields=”NewsID” DataTextFormatString=”Edit” Text=”Edit” />



Send Email using Google SMTP in vb.net

Here is small function which will allow you to send the emails
programatically using any SMTP server or you can also use the Google
SMTP server if you have a GMAIL account.

Function SendMail(ByVal strFrom As String, ByVal strTo As String, ByVal
strSubject As String, ByVal strBody As String) As Boolean
Dim mailmsg As New System.Net.Mail.MailMessage()

mailmsg.From = New MailAddress(strFrom)


mailmsg.Subject = strSubject

mailmsg.IsBodyHtml = True

mailmsg.Body = strBody

mailmsg.Priority = System.Net.Mail.MailPriority.Normal

Dim client As New System.Net.Mail.SmtpClient()

client.Host = “smtp.gmail.com”
client.Port = “587”

client.Credentials = New System.Net.NetworkCredential(“youremailid@gmail.com“, “Yourpassword“)

‘ client.EnableSsl = True

Dim userstate As Object = mailmsg

Return True
End Function

Replace the email id and the password with your own  gmail id and your gmail password

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




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.



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.





Add the script at the exact position show.



Step 3

Run your application.

Your AJAX Calls should work fine.







How to use Select Case function to evaluate different conditions in VB.NET

If you have to select one of the options from various possibilities then your best option is to use the case

In VB.NET you would used something like below.

Select [ Case ] testexpression
[ Case expressionlist
[ statements ] ]
[ Case Else
[ elsestatements ] ]
End Select

Here is more practical example:

Dim intMyNumber as Integer = 20
Select Case intMyNumber
Case 1
Debug.WriteLine("The value of my number is 1")

Case 2,3,4
Debug.WriteLine("The value of my number is between 2 and 4.")

Case 5 to 9
Debug.WriteLine("The value of my number is between 5 and 9")

Case Else
Debug.WriteLine("The value of my number is not in 1 to 9")

End Select