Programming

VB.Net : Read Settings in WinForm

To read settings in a VB.NET WinForm application, you can use the My.Settings object. My.Settings is a strongly typed class that provides access to the settings defined in the project’s Settings.settings file.

Here’s an example of how you can use My.Settings to read a setting:

Dim settingValue As String = My.Settings.SettingName

My.Settings.SettingName = “NewValue”
My.Settings.Save()

Note that you need to call the Save method after changing a setting value to persist the change.

VB.Net : Parse XML

Imports System.Xml

Private Sub ParseXML()
Dim xmlDoc As New XmlDocument()
xmlDoc.LoadXml(“NavaFP01000012022-12-170.00.00.0S C 10 %0.0C.Gst 2.5 %0.0S.Gst 2.5 %0.0NcSale“)

Dim xmlNode As XmlNode = xmlDoc.SelectSingleNode(“Records/Record”)
If xmlNode IsNot Nothing Then
Dim posName As String = xmlNode(“POSName”).InnerText
Dim billNo As String = xmlNode(“BillNo”).InnerText
Dim date As String = xmlNode(“Date”).InnerText
Dim time As String = xmlNode(“Time”).InnerText
Dim subTotal As String = xmlNode(“SubTotal”).InnerText
Dim discountAmount As String = xmlNode(“DiscountAmount”).InnerText
Dim grandTotalAmount As String = xmlNode(“GrandTotalAmount”).InnerText

‘ Access the Taxes node
Dim taxesNode As XmlNode = xmlNode(“Taxes”)
For Each taxNode As XmlNode In taxesNode.ChildNodes
Dim taxDesciption As String = taxNode(“TaxDesciption”).InnerText
Dim taxAmount As String = taxNode(“TaxAmount”).InnerText
Next

Dim settelmentMode As String = xmlNode(“SettelementMode”).InnerText
Dim transactionStatus As String = xmlNode(“TransactionStatus”).InnerText
End If
End Sub

Public Class Record
Public Property POSName As String
Public Property BillNo As String
Public Property Date As String
Public Property Time As String
Public Property SubTotal As Decimal
Public Property DiscountAmount As Decimal
Public Property GrandTotalAmount As Decimal
Public Property Taxes As List(Of Tax)
Public Property SettelementMode As String
Public Property TransactionStatus As String
End Class

Public Class Tax
Public Property TaxDesciption As String
Public Property TaxAmount As Decimal
End Class

Imports System.Xml

Private Function ParseXML(xmlString As String) As Record
Dim xmlDoc As New XmlDocument()
xmlDoc.LoadXml(xmlString)

Dim xmlNode As XmlNode = xmlDoc.SelectSingleNode(“Records/Record”)
If xmlNode IsNot Nothing Then
Dim record As New Record()
record.POSName = xmlNode(“POSName”).InnerText
record.BillNo = xmlNode(“BillNo”).InnerText
record.Date = xmlNode(“Date”).InnerText
record.Time = xmlNode(“Time”).InnerText
record.SubTotal = Decimal.Parse(xmlNode(“SubTotal”).InnerText)
record.DiscountAmount = Decimal.Parse(xmlNode(“DiscountAmount”).InnerText)
record.GrandTotalAmount = Decimal.Parse(xmlNode(“GrandTotalAmount”).InnerText)

‘ Access the Taxes node
Dim taxesNode As XmlNode = xmlNode(“Taxes”)
record.Taxes = New List(Of Tax)()
For Each taxNode As XmlNode In taxesNode.ChildNodes
Dim tax As New Tax()
tax.TaxDesciption = taxNode(“TaxDesciption”).InnerText
tax.TaxAmount = Decimal.Parse(taxNode(“TaxAmount”).InnerText)
record.Taxes.Add(tax)
Next

record.SettelmentMode = xmlNode(“SettelementMode”).InnerText
record.TransactionStatus = xmlNode(“TransactionStatus”).InnerText

Return record
End If

Return Nothing
End Function

You can use the System.Net.Http namespace in .NET to send a REST API GET request and retrieve the XML response. To parse the XML response into a class object, you can use the code I provided in my previous answer.

Imports System.Net.Http

Private Async Function GetXMLAsync() As Task(Of String)
Dim client As New HttpClient()
Dim response As HttpResponseMessage = Await client.GetAsync(“https://your-api-endpoint.com”)

If response.IsSuccessStatusCode Then
Dim xmlString As String = Await response.Content.ReadAsStringAsync()
Return xmlString
End If

Return Nothing
End Function

Private Async Sub LoadXMLAsync()
Dim xmlString As String = Await GetXMLAsync()
If Not String.IsNullOrEmpty(xmlString) Then
Dim record As Record = ParseXML(xmlString)
‘ Do something with the Record object
End If
End Sub

Listing All Records

Private Function ParseXML(xmlString As String) As List(Of Record)
Dim records As New List(Of Record)()

Dim xmlDoc As New XmlDocument()
xmlDoc.LoadXml(xmlString)

Dim recordNodes As XmlNodeList = xmlDoc.SelectNodes(“/Records/Record”)
For Each recordNode As XmlNode In recordNodes
Dim record As New Record()

record.POSName = recordNode.SelectSingleNode(“POSName”).InnerText
record.BillNo = recordNode.SelectSingleNode(“BillNo”).InnerText
record.Date = DateTime.Parse(recordNode.SelectSingleNode(“Date”).InnerText)
record.Time = DateTime.Parse(recordNode.SelectSingleNode(“Time”).InnerText)
record.SubTotal = Decimal.Parse(recordNode.SelectSingleNode(“SubTotal”).InnerText)
record.DiscountAmount = Decimal.Parse(recordNode.SelectSingleNode(“DiscountAmount”).InnerText)
record.GrandTotalAmount = Decimal.Parse(recordNode.SelectSingleNode(“GrandTotalAmount”).InnerText)

Dim taxNodes As XmlNodeList = recordNode.SelectNodes(“Taxes/Tax”)
For Each taxNode As XmlNode In taxNodes
Dim tax As New Tax()
tax.TaxDesciption = taxNode.SelectSingleNode(“TaxDesciption”).InnerText
tax.TaxAmount = Decimal.Parse(taxNode.SelectSingleNode(“TaxAmount”).InnerText)

record.Taxes.Add(tax)
Next

record.SettelementMode = recordNode.SelectSingleNode(“SettelementMode”).InnerText
record.TransactionStatus = recordNode.SelectSingleNode(“TransactionStatus”).InnerText

records.Add(record)
Next

Return records
End Function

Create Web Server in Android

import fi.iki.elonen.NanoHTTPD;

public class MyHTTPServer extends NanoHTTPD {
public MyHTTPServer() {
super(8080);
}
@Override
public Response serve(IHTTPSession session) {
String msg = “

Hello from Android TV HTTP Server!

“;
return newFixedLengthResponse(Response.Status.OK, “text/html”, msg);
}
public static void main(String[] args) {
MyHTTPServer server = new MyHTTPServer();
try {
server.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}

This code starts a HTTP server on port 8080 and returns a simple HTML message when a client makes a request to the server.
Here’s a simple example to demonstrate this:
On the mobile device, send an HTTP POST request to the Android TV’s IP address (e.g. http://192.168.0.100:8080/) with the text to be displayed as the body of the request.
On the Android TV, the web server can receive the request and extract the text from the body.
The extracted text can then be displayed on the TV screen using an Android view such as TextView.

import fi.iki.elonen.NanoHTTPD;

public class MyHTTPServer extends NanoHTTPD {
public MyHTTPServer() {
super(8080);
}
@Override
public Response serve(IHTTPSession session) {
Method method = session.getMethod();
if (Method.POST.equals(method)) {
try {
session.getInputStream();
Map files = new HashMap();
session.parseBody(files);
String text = files.get(“postData”);
// display the text on the TV screen
displayText(text);
String msg = “Text received: ” + text;
return newFixedLengthResponse(Response.Status.OK, “text/plain”, msg);
} catch (IOException | ResponseException e) {
e.printStackTrace();
}
}
return newFixedLengthResponse(Response.Status.BAD_REQUEST, “text/plain”, “Bad Request”);
}
private void displayText(String text) {
// code to display the text on the TV screen
}
public static void main(String[] args) {
MyHTTPServer server = new MyHTTPServer();
try {
server.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}

TSQL: Extract Next Random Question On A QUIZ

DECLARE @UserID INT = 1;
DECLARE @PrevQuestionID INT = NULL;

WITH PossibleQuestions AS
(
SELECT QuestionID, Question, Answer1, Answer2, Level
FROM Questions
WHERE PrevQuestionID = @PrevQuestionID
AND QuestionID NOT IN (SELECT QuestionID FROM UserAnswers WHERE UserID = @UserID)
),
Levels AS
(
SELECT MIN(Level) AS MinLevel
FROM PossibleQuestions
)
SELECT TOP 1 QuestionID, Question, Answer1, Answer2, Level
FROM PossibleQuestions
CROSS JOIN Levels
WHERE Level = MinLevel
ORDER BY NEWID();

How To Use Remote Procedure Call (RPC) in C#:

Server Codes

using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;

namespace RPCServer
{
public class MathService : MarshalByRefObject
{

public int Add(int a, int b)
{
Console.WriteLine(“Received Add request”);
return a + b;
}

public int Subtract(int a, int b)
{
Console.WriteLine(“Received Subtract request”);
return a – b;
}
}

class Program
{
static void Main(string[] args)
{
TcpChannel channel = new TcpChannel(8080);
ChannelServices.RegisterChannel(channel, false);
RemotingConfiguration.RegisterWellKnownServiceType(
typeof(MathService),
“MathService”,
WellKnownObjectMode.SingleCall
);
Console.WriteLine(“Press enter to exit”);
Console.ReadLine();
}
}
}

Client Code

using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;

namespace RPCCLient
{
class Program
{
static void Main(string[] args)
{
TcpChannel channel = new TcpChannel();
ChannelServices.RegisterChannel(channel, false);

MathService service = (MathService)Activator.GetObject(
typeof(MathService),
“tcp://localhost:8080/MathService”
);

Console.WriteLine(“5 + 3 = ” + service.Add(5, 3));
Console.WriteLine(“5 – 3 = ” + service.Subtract(5, 3));

Console.WriteLine(“Press enter to exit”);
Console.ReadLine();
}
}
}

This example creates a simple math service that can perform addition and subtraction operations, and makes it available over the network using the TCP protocol. The MathService class is decorated with the MarshalByRefObject attribute, which makes it possible to call its methods from a remote client.

The RPCServer project contains the code for the server, which registers the MathService class with the .NET remoting framework and makes it available over the network. The RPCCLient project contains the code for the client, which uses the .NET remoting framework to call methods on the remote MathService instance.

Minimal API , Reading User Claim.

app.MapGet(“/b”, async (context) =>
{
var s = “Fail”;
var identity = context.User?.Identity;
if (identity != null && identity.IsAuthenticated)
{
s = context.User?.Claims?.FirstOrDefault(c => c.Type.Contains(“LoginName”))?.Value ?? “”;
}
await context.Response.WriteAsync(s);
});

 

 

app.MapGet(“/b”, async (context) =>
{
var s = “Fail”;
var identity = context.User?.Identity;
if (identity != null && identity.IsAuthenticated)
{
s = “OK”;
}
await context.Response.WriteAsync(s);
});

 

 

app.MapGet(“/b”, async (context) =>
{
var s = “Fail”;
var identity = context.User?.Identity;
if (identity != null && identity.IsAuthenticated)
{
s = “OK”;
}
return new ContentResult { Content = s, ContentType = “text/plain”, StatusCode = 200 };
});

 

app.MapGet(“/b”, async (context) =>
{
var s = “Fail”;
var identity = context.User?.Identity;
if (identity != null && identity.IsAuthenticated)
{
s = “OK”;
}
return Task.FromResult(s);
});

Scroll to Top