Daily Archives: November 29, 2020
VB.NET || How To Get The Computer & User Client IP Address Using VB.NET
The following is a module with functions which demonstrates how to get the computers and user client request IPv4 IP address using VB.NET.
The function demonstrated on this page returns the IPv4 address of the calling user. When under a web environment, it returns the clients System.Web.HttpContext.Current.Request IP address, otherwise it returns the IP address of the local machine (i.e: server) if there is no request.
The following function uses System.Web to determine the IP address of the client.
Note: To use the function in this module, make sure you have a reference to ‘System.Web‘ in your project.
One way to do this is, in your Solution Explorer (where all the files are shown with your project), right click the ‘References‘ folder, click ‘Add Reference‘, then type ‘System.Web‘ in the search box, and add the reference titled System.Web in the results Tab.
1. Get IP Address
The example below demonstrates the use of ‘Utils.GetIPv4Address‘ to get the IPv4 address of the calling user.
1 2 3 4 5 6 7 8 9 |
' Get IP Address ' Get IP address of the current user Dim ipAddress = Utils.GetIPv4Address Debug.Print($"IP Address: {ipAddress}") ' example output: ' IP Address: 192.168.0.5 |
2. Utils Namespace
The following is the Utils Namespace. Include this in your project to start using!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
' ============================================================================ ' Author: Kenneth Perkins ' Date: Nov 29, 2020 ' Taken From: http://programmingnotes.org/ ' File: Utils.vb ' Description: Handles general utility functions ' ============================================================================ Option Strict On Option Explicit On Namespace Global.Utils Public Module modUtils ''' <summary> ''' Returns the IPv4 address of the calling user. When under a web ''' environment, returns the <see cref="System.Web.HttpRequest"/> ''' IP address, otherwise returns the IP address of the local machine ''' </summary> ''' <returns>The IPv4 address of the calling user</returns> Public Function GetIPv4Address() As String Dim ipAddress = String.Empty ' Get client ip address If System.Web.HttpContext.Current IsNot Nothing _ AndAlso System.Web.HttpContext.Current.Request IsNot Nothing Then ' Get client ip address using ServerVariables Dim request = System.Web.HttpContext.Current.Request ipAddress = request.ServerVariables("HTTP_X_FORWARDED_FOR") If Not String.IsNullOrEmpty(ipAddress) Then Dim addresses = ipAddress.Split(",".ToCharArray, StringSplitOptions.RemoveEmptyEntries) If addresses.Length > 0 Then ipAddress = addresses(0) End If End If If String.IsNullOrEmpty(ipAddress) Then ipAddress = request.ServerVariables("REMOTE_ADDR") End If ' Get client ip address using UserHostAddress If String.IsNullOrEmpty(ipAddress) Then Dim clientIPA = GetNetworkAddress(request.UserHostAddress) If clientIPA IsNot Nothing Then ipAddress = clientIPA.ToString End If End If End If ' Get local machine ip address If String.IsNullOrEmpty(ipAddress) Then Dim loacalIPA = GetNetworkAddress(System.Net.Dns.GetHostName) If loacalIPA IsNot Nothing Then ipAddress = loacalIPA.ToString End If End If Return ipAddress End Function Private Function GetNetworkAddress(addresses As String) As System.Net.IPAddress Return System.Net.Dns.GetHostAddresses(addresses) _ .FirstOrDefault(Function(x) x.AddressFamily = System.Net.Sockets.AddressFamily.InterNetwork) End Function End Module End Namespace ' http://programmingnotes.org/ |
3. More Examples
Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
' ============================================================================ ' Author: Kenneth Perkins ' Date: Nov 29, 2020 ' Taken From: http://programmingnotes.org/ ' File: Program.vb ' Description: The following demonstrates the use of the Utils Namespace ' ============================================================================ Option Strict On Option Explicit On Imports System Public Module Program Sub Main(args As String()) Try ' Get IP address of the current user Dim ipAddress = Utils.GetIPv4Address Display($"IP Address: {ipAddress}") Catch ex As Exception Display(ex.ToString) Finally Console.ReadLine() End Try End Sub Public Sub Display(message As String) Console.WriteLine(message) Debug.Print(message) End Sub End Module ' http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.
VB.NET || How To Copy DataTable DataRow From One DataRow To Another Using VB.NET
The following is a module with functions which demonstrates how to copy a DataTable DataRow from one DataRow to another using VB.NET.
The function demonstrated on this page is an extension method, which copies all matching columns from the source DataRow to the destination DataRow. If no matching column exists between the two rows, the data at that column is skipped. This allows to safely copy a single row from one DataTable to other.
1. Copy DataRow
The example below demonstrates the use of ‘Utils.Collections.CopyTo‘ to copy all matching columns from a source row to a destination row.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
' Copy DataRow Imports Utils.Collections ' Declare the source datatable Dim table1 = New DataTable table1.Columns.Add("Name", GetType(String)) table1.Rows.Add("Kenneth") table1.Rows.Add("Jennifer") table1.Rows.Add("Lynn") table1.Rows.Add("Sole") table1.Rows.Add(System.DBNull.Value) ' Declare the destination datatable Dim table2 = New DataTable table2.Columns.Add("Id", GetType(Integer)) table2.Columns.Add("Name", GetType(String)) For index = 0 To table1.Rows.Count - 1 ' Get the source row Dim rowSource = table1.Rows(index) ' Get the destination row Dim rowDestination = table2.NewRow ' Do something with the destination row rowDestination("Id") = index + 1 ' Copy contents from source to destination rowSource.CopyTo(rowDestination) table2.Rows.Add(rowDestination) Next ' Display information from destination table For Each row As DataRow In table2.Rows Debug.Print($"Id: {row("Id")}, Name: {row("Name")}") Next ' expected output: ' Id: 1, Name: Kenneth ' Id: 2, Name: Jennifer ' Id: 3, Name: Lynn ' Id: 4, Name: Sole ' Id: 5, Name: |
2. Utils Namespace
The following is the Utils Namespace. Include this in your project to start using!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
' ============================================================================ ' Author: Kenneth Perkins ' Date: Nov 29, 2020 ' Taken From: http://programmingnotes.org/ ' File: Utils.vb ' Description: Handles general utility functions ' ============================================================================ Option Strict On Option Explicit On Namespace Global.Utils Namespace Collections Public Module modCollections ''' <summary> ''' Copies all matching columns from the source row to the destination row ''' </summary> ''' <param name="source">The DataRow source</param> ''' <param name="destination">The DataRow destination</param> <Runtime.CompilerServices.Extension()> Public Sub CopyTo(source As DataRow, destination As DataRow) For Each col As DataColumn In source.Table.Columns If destination.Table.Columns.Contains(col.ColumnName) Then destination(col.ColumnName) = source(col.ColumnName) End If Next End Sub End Module End Namespace End Namespace ' http://programmingnotes.org/ |
3. More Examples
Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
' ============================================================================ ' Author: Kenneth Perkins ' Date: Nov 29, 2020 ' Taken From: http://programmingnotes.org/ ' File: Program.vb ' Description: The following demonstrates the use of the Utils Namespace ' ============================================================================ Option Strict On Option Explicit On Imports System Imports Utils.Collections Public Module Program Sub Main(args As String()) Try ' Declare the source datatable Dim table1 = New DataTable table1.Columns.Add("Name", GetType(String)) table1.Rows.Add("Kenneth") table1.Rows.Add("Jennifer") table1.Rows.Add("Lynn") table1.Rows.Add("Sole") table1.Rows.Add(System.DBNull.Value) ' Declare the destination datatable Dim table2 = New DataTable table2.Columns.Add("Id", GetType(Integer)) table2.Columns.Add("Name", GetType(String)) For index = 0 To table1.Rows.Count - 1 ' Get the source row Dim rowSource = table1.Rows(index) ' Get the destination row Dim rowDestination = table2.NewRow ' Do something with the destination row rowDestination("Id") = index + 1 ' Copy contents from source to destination rowSource.CopyTo(rowDestination) table2.Rows.Add(rowDestination) Next ' Display information from destination table For Each row As DataRow In table2.Rows Display($"Id: {row("Id")}, Name: {row("Name")}") Next Catch ex As Exception Display(ex.ToString) Finally Console.ReadLine() End Try End Sub Public Sub Display(message As String) Console.WriteLine(message) Debug.Print(message) End Sub End Module ' http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.