Monday, March 21, 2011

GetType Gotchas in VB .NET

Since VB .NET has a built in GetType method , some confusion is created when using GetType in VB .NET.

I will conclude following two points from my experience :

1. Use the VB .NET GetType  to get types  of System defined types.
2. Use System.Type.GetType("FullyqualifiedTypeName , AssemblyName") to get Type of user
defined types.

In the following, "MyServiceClass" is a class defined in an external class library ClassLibrary1. Further the class
implements IMyService interface, which is also defined in the same class library. Here the class and interface are being
refered outside of the class library, in a differenct project, with a reference to the ClassLibrary1.dll added.



Imports SystemImports System.TypeImports System.ServiceModelImports System.ServiceModel.DescriptionImports ClassLibrary1Public





sh.Description.Behaviors.Add(mbehave)
sh.AddServiceEndpoint(
sh.AddServiceEndpoint(
sh.AddServiceEndpoint(System.Type.GetType(
sh.AddServiceEndpoint(System.Type.GetType(
sh.Open()

End
Class Form1Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.LoadDim tcpa As New Uri("net.tcp://localhost:8000")Dim sh As New ServiceHost(System.Type.GetType("ClassLibrary1.MyServiceClass,ClassLibrary1"), New Uri("net.pipe://localhost"), tcpa)Dim tcpb As New NetTcpBindingDim mbehave As New ServiceMetadataBehaviorGetType(IMetadataExchange), MetadataExchangeBindings.CreateMexTcpBinding(), "mex")GetType(IMetadataExchange), MetadataExchangeBindings.CreateMexNamedPipeBinding, "mex")"ClassLibrary1.IMyService,ClassLibrary1"), tcpb, tcpa)"ClassLibrary1.IMyService,ClassLibrary1"), New NetNamedPipeBinding, New Uri("net.pipe://localhost"))End Sub Class

Sunday, March 13, 2011

Try Catch block variable access scope


Are the variables declared in  a try block accessible in
the catch block ?
NO. For a variable to be accessible in both try and catch block
it must be declared outside of both blocks.
e.g in the following, variable ut is not accessible in
catch block because it is declared in try block, hence the code will not compile
and will give "Name "ut" is not declared" error

        Try
     Dim ut As New Utility
            Dim dr As DataRow() = x.[Select]("DateTime < '" & DateTime.Now.AddMinutes(-30).ToString("yyyy-MM-dd HH:mm:ss.fff") & "'")
            If dr.Length > 0 Then
                For Each drr As DataRow In dr
                    drr.Delete()
                    ut.WriteLog("C:\test.txt", "Deleted Some Records")
                Next
            End If
        Catch ex As Exception
            ut.WriteLog("C:\test.txt", "Deleted Some Records" + " " + ex.Message)
        End Try

Wednesday, March 9, 2011

What is wrong with this Connection String ?

QUESTION : What's  wrong with this connection string  ?


string
userid=gps;password=1234#";




ANSWER :  You will get the following error :
 "Keyword not supported:'userid'"






The correct connstr should be:
string connstr = "Data Source=IBM\\SFT;Initial Catalog=GPS_Live;User ID=gps;
uid=gps;password=gPs123!@#";
connstr = "Data Source=IBM\\SFT;Initial Catalog=GPS_Live;

Caching and Manipulating a DataTable in memory (without storing in SQL)

Default.aspx.cs
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;usingSystem.IO;using System.Data;using System.Data.SqlClient;public
{


{

{
th =
th.AddRowWithVerification(
th.AddRowWithVerification(
th.AddRowWithVerification(
Session[
}

{
th =
}
GridView1.DataSource = th.GetTable();
GridView1.DataBind();
partial class _Default : System.Web.UI.Page TableHandler th;protected void Page_Load(object sender, EventArgs e)if (Session["THTable"] == null)new TableHandler();"aa11", "bb", "ccb", DateTime.Now);"aa2", "bb", "cc", DateTime.Now);"aa22", "bbb", "ccb", DateTime.Now);"THTable"] = th.GetTable();if (Session["THTable"] != null && th == null)new TableHandler();// Start date
DateTime startDate = new DateTime(2005, 2, 1, 3, 4, 12, 56);// End date
DateTime endDate = new DateTime(2005, 12, 12, 4, 30, 45, 12);// Time span
TimeSpan diffDate = endDate.Subtract(startDate);// Spit it out Label1.Text =

Label1.Text += (diffDate.Days.ToString() +
Label1.Text += (diffDate.Hours.ToString() +
Label1.Text += (diffDate.Minutes.ToString() +
Label1.Text += (diffDate.Seconds.ToString() +
Label1.Text += (diffDate.Milliseconds.ToString() +
Label1.Text +=
Label1.Text += diffDate.TotalMilliseconds +
Label1.Text +=
Label1.Text += (
}

{
th.AddRowWithVerification(TextBox1.Text,TextBox2.Text,TextBox3.Text,
GridView1.DataBind();
}
}

"Start Date:" + startDate.ToString("yyyy-MM-dd HH:mm:ss.fff") + "<br>" + "End Date:" + endDate.ToString("yyyy-MM-dd HH:mm:ss.fff") +"<br>" + "Time Difference: ";" Days,");" Hours,");" Minutes,");" Seconds,");" Milliseconds " + "<br>");"Total TimeDifference: " + diffDate.TotalMinutes + "Minutes = " + diffDate.TotalHours + "Hours = ";"Millisecs = " + diffDate.TotalDays + "Days" + "<br>";DateTime.Now.AddMinutes(-30).ToString("yyyy-MM-dd HH:mm:ss.fff") + " " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "<br>";DateTime.Now.AddMinutes(-30) < DateTime.Now) ? (DateTime.Now.AddMinutes(-30).ToString("yyyy-MM-dd HH:mm:ss.fff")) : DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");protected void Button1_Click(object sender, EventArgs e)DateTime.Now);//pagepublic class TableHandler{


{

{
x =
x.Columns.Add(
x.Columns.Add(
x.Columns.Add(
x.Columns.Add(

dr[
dr[
dr[
dr[
x.Rows.Add(dr);
System.Web.
}
DataTable x;public TableHandler()if (System.Web.HttpContext.Current.Session["THTable"] == null)new DataTable();new DataColumn("IPAddress", System.Type.GetType("System.String")));new DataColumn("PortNo", System.Type.GetType("System.String")));new DataColumn("DeviceIMEINo", System.Type.GetType("System.String")));new DataColumn("DateTime", System.Type.GetType("System.DateTime")));DataRow dr = x.NewRow();"IPAddress"] = "sfds";"PortNo"] = "sdfds";"DeviceIMEINo"] = "dsfds";"DateTime"] = DateTime.Now;HttpContext.Current.Session["THTable"] = x;elsex = (
}

{
(
DeleteOldRecords();
DataTable)System.Web.HttpContext.Current.Session["THTable"];public void AddRowWithVerification(string IPAddress, string PortNo, string DeviceIMEINo, DateTime dtinn)new Utility()).WriteLog("C:\\test\\test.txt" ,"AddRowWithVerification Called");DataRow[] dr = x.Select("DeviceIMEINo = '" + DeviceIMEINo + "'");if (dr.Count() > 0) //row already exists , update it{

drr[
drr[
drr[
}
DataRow drr = dr[0];"IPAddress"] = IPAddress;"PortNo"] = PortNo;"DateTime"] = dtinn;else //add new row{

drr[
drr[
drr[
drr[
x.Rows.Add(drr);
}
System.Web.
}
DataRow drr = x.NewRow();"IPAddress"] = IPAddress;"PortNo"] = PortNo;"DeviceIMEINo"] = DeviceIMEINo;"DateTime"] = dtinn;HttpContext.Current.Session["THTable"] = x;//AddRowWithVerification
{

dr[
dr[
dr[
x.Rows.Add(dr);
System.Web.
}
public void AddRow(string IPAddress, string PortNo, string DeviceIMEINo)DataRow dr = x.NewRow();"IPAddress"] = IPAddress;"PortNo"] = PortNo;"DeviceIMEINo"] = DeviceIMEINo;HttpContext.Current.Session["THTable"] = x;//AddRow
{

}
public DataTable GetTable()return (DataTable)System.Web.HttpContext.Current.Session["THTable"];//GetTable
{


{

{
drr.Delete();
(
}
}
}
void DeleteOldRecords()DataRow[] dr = x.Select("DateTime < '" + DateTime.Now.AddMinutes(-1).ToString("yyyy-MM-dd HH:mm:ss.fff") + "'");if (dr.Count() > 0)foreach (DataRow drr in dr)new Utility()).WriteLog("C:\\test\\test.txt", "Deleted Some Records");//DeleteOldRecords
{


DeviceIMEIMo = dr[0][

}
public string GetDeviceIMEINo(string IPAddress)string DeviceIMEIMo = "";DataRow[] dr = x.Select("IPAddress = " + IPAddress.Trim());"DeviceIMEINo"].ToString().Trim();return DeviceIMEIMo;//GetDeviceIMEINo}



//TableHandlerpublic class Utility{

{


sr.WriteLine(LogString);
sr.Flush();
sr.Close();
}
}
public void WriteLog(string FileName, string LogString)FileStream ff = new FileStream(FileName, FileMode.Append);StreamWriter sr = new StreamWriter(ff);
Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!
<
<
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">html xmlns="http://www.w3.org/1999/xhtml">head runat="server">
</
<
<title></title>head>body><form id="form1" runat="server"><div>
<asp:GridView ID="GridView1" runat="server"></asp:GridView><asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</div>
</
</
</form>body>html>





using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using
System.IO;using System.Data;using System.Data.SqlClient;public
{


{

{
th =
th.AddRowWithVerification(
th.AddRowWithVerification(
th.AddRowWithVerification(
Session[
}

{
th =
}
GridView1.DataSource = th.GetTable();
GridView1.DataBind();

partial class _Default : System.Web.UI.Page TableHandler th;protected void Page_Load(object sender, EventArgs e)if (Session["THTable"] == null)new TableHandler();"aa11", "bb", "ccb", DateTime.Now);"aa2", "bb", "cc", DateTime.Now);"aa22", "bbb", "ccb", DateTime.Now);"THTable"] = th.GetTable();if (Session["THTable"] != null && th == null)new TableHandler();// Start date
DateTime startDate = new DateTime(2005, 2, 1, 3, 4, 12, 56);// End date
DateTime endDate = new DateTime(2005, 12, 12, 4, 30, 45, 12);// Time span
TimeSpan diffDate = endDate.Subtract(startDate);// Spit it out Label1.Text =

Label1.Text += (diffDate.Days.ToString() +
Label1.Text += (diffDate.Hours.ToString() +
Label1.Text += (diffDate.Minutes.ToString() +
Label1.Text += (diffDate.Seconds.ToString() +
Label1.Text += (diffDate.Milliseconds.ToString() +
Label1.Text +=
Label1.Text += diffDate.TotalMilliseconds +
Label1.Text +=
Label1.Text += (
}

{
th.AddRowWithVerification(TextBox1.Text,TextBox2.Text,TextBox3.Text,
GridView1.DataBind();
}
}


"Start Date:" + startDate.ToString("yyyy-MM-dd HH:mm:ss.fff") + "<br>" + "End Date:" + endDate.ToString("yyyy-MM-dd HH:mm:ss.fff") +"<br>" + "Time Difference: ";" Days,");" Hours,");" Minutes,");" Seconds,");" Milliseconds " + "<br>");"Total TimeDifference: " + diffDate.TotalMinutes + "Minutes = " + diffDate.TotalHours + "Hours = ";"Millisecs = " + diffDate.TotalDays + "Days" + "<br>";DateTime.Now.AddMinutes(-30).ToString("yyyy-MM-dd HH:mm:ss.fff") + " " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "<br>";DateTime.Now.AddMinutes(-30) < DateTime.Now) ? (DateTime.Now.AddMinutes(-30).ToString("yyyy-MM-dd HH:mm:ss.fff")) : DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");protected void Button1_Click(object sender, EventArgs e)DateTime.Now);//pagepublic class TableHandler{


{

{
x =
x.Columns.Add(
x.Columns.Add(
x.Columns.Add(
x.Columns.Add(

dr[
dr[
dr[
dr[
x.Rows.Add(dr);
System.Web.
}
DataTable x;public TableHandler()if (System.Web.HttpContext.Current.Session["THTable"] == null)new DataTable();new DataColumn("IPAddress", System.Type.GetType("System.String")));new DataColumn("PortNo", System.Type.GetType("System.String")));new DataColumn("DeviceIMEINo", System.Type.GetType("System.String")));new DataColumn("DateTime", System.Type.GetType("System.DateTime")));DataRow dr = x.NewRow();"IPAddress"] = "sfds";"PortNo"] = "sdfds";"DeviceIMEINo"] = "dsfds";"DateTime"] = DateTime.Now;HttpContext.Current.Session["THTable"] = x;elsex = (
}

{
(
DeleteOldRecords();

DataTable)System.Web.HttpContext.Current.Session["THTable"];public void AddRowWithVerification(string IPAddress, string PortNo, string DeviceIMEINo, DateTime dtinn)new Utility()).WriteLog("C:\\test\\test.txt" ,"AddRowWithVerification Called");DataRow[] dr = x.Select("DeviceIMEINo = '" + DeviceIMEINo + "'");if (dr.Count() > 0) //row already exists , update it{

drr[
drr[
drr[
}
DataRow drr = dr[0];"IPAddress"] = IPAddress;"PortNo"] = PortNo;"DateTime"] = dtinn;else //add new row{

drr[
drr[
drr[
drr[
x.Rows.Add(drr);
}
System.Web.
}
DataRow drr = x.NewRow();"IPAddress"] = IPAddress;"PortNo"] = PortNo;"DeviceIMEINo"] = DeviceIMEINo;"DateTime"] = dtinn;HttpContext.Current.Session["THTable"] = x;//AddRowWithVerification
{

dr[
dr[
dr[
x.Rows.Add(dr);
System.Web.
}
public void AddRow(string IPAddress, string PortNo, string DeviceIMEINo)DataRow dr = x.NewRow();"IPAddress"] = IPAddress;"PortNo"] = PortNo;"DeviceIMEINo"] = DeviceIMEINo;HttpContext.Current.Session["THTable"] = x;//AddRow
{

}
public DataTable GetTable()return (DataTable)System.Web.HttpContext.Current.Session["THTable"];//GetTable
{


{

{
drr.Delete();
(
}
}
}
void DeleteOldRecords()DataRow[] dr = x.Select("DateTime < '" + DateTime.Now.AddMinutes(-1).ToString("yyyy-MM-dd HH:mm:ss.fff") + "'");if (dr.Count() > 0)foreach (DataRow drr in dr)new Utility()).WriteLog("C:\\test\\test.txt", "Deleted Some Records");//DeleteOldRecords
{


DeviceIMEIMo = dr[0][

}
public string GetDeviceIMEINo(string IPAddress)string DeviceIMEIMo = "";DataRow[] dr = x.Select("IPAddress = " + IPAddress.Trim());"DeviceIMEINo"].ToString().Trim();return DeviceIMEIMo;//GetDeviceIMEINo}




//TableHandlerpublic class Utility{

{


sr.WriteLine(LogString);
sr.Flush();
sr.Close();
}
}
public void WriteLog(string FileName, string LogString)FileStream ff = new FileStream(FileName, FileMode.Append);StreamWriter sr = new StreamWriter(ff);


Comparing features of LangGraph vs CrewAI

LangGraph Orchestration vs CrewAI Orchestration The following table compares LangGraph and CrewAI orchestration across their menta...