To Read a Assembly file using Reflection in C#.NET

July 20, 2008

using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Web;
using System.IO;
using System.Net;

namespace ConsoleExample
{
class LoadAssembly
{
public void LoadAssembly_FromFile()
{
Assembly Ass = System.Reflection.Assembly.LoadFrom(“D:\\Parthiban\\ConsoleExample\\CrystalDecisions.Shared.dll”);
Type Asstype = Ass.GetType();

#region Get Constructor Info
ConstructorInfo[] I = Asstype.GetConstructors();
#endregion

#region Extractiong Properties Info
PropertyInfo[] PrInfo = Asstype.GetProperties();
#endregion

#region Extracting Method Info
MethodInfo[] MeInfo = Asstype.GetMethods();
Console.WriteLine(“Return Parameter \t” + MeInfo[0].ReturnType);
Console.WriteLine(“Name \t” + MeInfo[0].Name);
Console.WriteLine(“Module \t” + MeInfo[0].Module);
Console.WriteLine(“Method Body \t” + MeInfo[0].GetMethodBody());
Console.ReadLine();
#endregion

#region Extracting Event Info
EventInfo[] EveInfo = Asstype.GetEvents();
#endregion
Console.ReadLine();
}
}
}

Cookiless Session State in ASP.NET

July 20, 2008

When each user browse a Page the unique Session  Object is created….Session Object is idendified by unique session Id.
The Session ID is Sent back to the client in the form of cookies.

Some browser does not support cookies.. So that we can set cooliless=true.

For that… the Session ID Sent to the URL QueryString.

Passing Object Reference to a Function

July 20, 2008

While Passing reference to the function any changes made to  that variables inside the Function.That will affect the
Main variable value.

—————————————–
When we can Pass it object value as variables it just make a Copy of that variables…… and than it takes to perform
an operation.But while passing the reference Both variables are refer to the same memory location.

using System;
using System.Collections.Generic;
using System.Text;

namespace Java2s
{
enum constants
{
x=1,
a=2,
b=3
}

class Program
{

static void Main(string[] args)
{
Console.WriteLine(constants.a);
Console.WriteLine(constants.b);
Console.WriteLine(constants.x);
Console.WriteLine(int.MaxValue);
Console.ReadLine();
int a = 1;
int b = 2;
Console.WriteLine(“Before Reference a&b is   “+a +  “   ” +b);
Program Obj = new Program();
Console.WriteLine(Obj.Addition(ref a, ref  b));

Console.WriteLine(“After Reference a&b is   ” + a + “   ” + b);
Console.ReadLine();
}
public int Addition(ref int a,ref int b)
{
a = 2;
b = 3;
return (a + b);
}
}

}

Overloaded Constructor in C#.NET

July 20, 2008

A class can have any number of constructor.

OverloadConstructor is nothing but  the Constructor having different set of parameters.

1.Under this type of situation ,how to call one Constructor to another constructor.

For the below Example when creating Object to the Parameter 2 Constructor it automatically redire
-ct to the Parameter 3 Constructor.The Parameter 2 Constructor wont’t called

///////////////////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleExample
{
class OverloadedConstructor
{
int i;
string Name;
string Native;

public OverloadedConstructor()
{
Console.WriteLine(“No Parameter Constructor”);
Console.ReadLine();

}
public OverloadedConstructor(string Name, string Native):this(“Pattukkottai”)
{
Console.WriteLine(“2 Parameter Constructor”);
Console.WriteLine(Name);
Console.WriteLine(Native);
Console.ReadLine();
}
public OverloadedConstructor(string Native)
{
Console.WriteLine(“1 Parameter Constructor”);
Console.ReadLine();

}

}
}

To Call a Base Class Constructor From Derived Class Constructor Calling in C#.NET

July 20, 2008

class CheckStatic:NewKeyWord
{

public CheckStatic():base()
{
Console.WriteLine(“I am a Derived Class Constructor”);
}

public void CheckStatic1()
{
base.GetData1();
}
}
///////////////////////////////////////////

Use the (Base) Keyword to call a Base Class Constructor.

Basic 64-bit Encoding and Decoding in C#.NET

July 20, 2008

byte[] Keybyte = new byte[Key.Length];
System.Text.ASCIIEncoding Encoding = new System.Text.ASCIIEncoding();

return  Convert.ToBase64String(Encoding.GetBytes(Key) );

///// U can   also use  it to convert  string to Bytes.

Short Method:

string OrderCode = “Ord12011982″;

#region Encode
byte[] Enc = ASCIIEncoding.UTF7.GetBytes(OrderCode);
string EncString = Convert.ToBase64String(Enc);
#endregion

#region Decode
byte[] Dec = Convert.FromBase64String(EncString);
string b = ASCIIEncoding.UTF7.GetString(Dec);
#endregion

Tou can al use UTF8,UTF32

To Change the gridview lines color in ASP.NET

July 20, 2008

<asp:DataGrid ID=”DataGrid1″ runat=”server” AutoGenerateColumns=”False”
BorderColor=”Black” BorderStyle=”Solid” BorderWidth=”1px” Width=”300px” PageSize=”7″>
<Columns>
<asp:TemplateColumn ItemStyle-BorderColor=”blue” ItemStyle-BorderStyle=”solid” ItemStyle-BorderWidth=”2px”>
<ItemTemplate>
<center>
<asp:Label ID=”Label1″ runat=”server” Text=’Hello’></asp:Label>
</center>
</ItemTemplate>
<HeaderStyle HorizontalAlign=”Center” />
</asp:TemplateColumn>
<AlternatingItemStyle BackColor=”Gainsboro” BorderColor=”White” />
<HeaderStyle BackColor=”Gray” />
<EditItemStyle BorderColor=”White” />
<ItemStyle BorderColor=”White” />
</asp:DataGrid>

Region Based Default Button in ASP.NET

July 20, 2008

EmailTextBox.Attributes.Add(“onKeyPress”, “javascript:if (event.keyCode == 13) __doPostBack(‘” + LoginButton.UniqueID + “‘,”)”);

Another one Simple Method is use the Panel inside it put the Default Button Focus To It
<asp:Panel ID=”Hi” runat=”server” DefaultButton=”Button1″>

You can also  kept the focus coding at the Form tag itself
<form id=”form1″ runat=”server”  defaultbutton=”Button3″ >

Transaction in C#.NET

July 20, 2008

SqlConnection conn = new SqlConnection(connString);
SqlTransaction trans = conn.BeginTransaction();
try
{
SqlCommand cmd = new SqlCommand(“MyWriteProc”,conn, trans);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(….

// additional transactioned writes to database
trans.Commit();
}
catch
{
trans.Rollback();
}
When you use ADO.NET manual transactions, you can set the desired isolation
level on the BeginTransacion method as shown in the following code fragment.

SqlConnection conn = new SqlConnection(connString);
SqlTransaction trans = conn.BeginTransaction(IsolationLevel.ReadCommitted);

To Send an XML stream to another Page in ASP.NET

July 20, 2008

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.Net;

public partial class SendStream : System.Web.UI.Page
{

HttpWebRequest MyRequest;
#region Page_Load
protected void Page_Load(object sender, EventArgs e)
{

}
#endregion

#region Send_Stream
protected void Button1_Click(object sender, EventArgs e)
{
try
{
using(SqlConnection SQLCon = new SqlConnection(“server=.;database=TEST;user id=sa;password=sa”))
{

SQLCon.Open();
string Str = “SELECT * FROM CATEGORY AS CATEGORY”;
SqlCommand SQLCmd = new SqlCommand(Str, SQLCon);
DataSet Ds = new DataSet();
SqlDataAdapter SQLAdapter = new SqlDataAdapter(SQLCmd);
SQLAdapter.Fill(Ds, “Category”);
ASCIIEncoding Encoding = new ASCIIEncoding();
byte[] Data = Encoding.GetBytes(Ds.GetXml());
MyRequest = (HttpWebRequest)WebRequest.Create(“http://localhost:3665/WEbsite1/ReceiveStream.aspx”);
MyRequest.Method = “Post”;
MyRequest.ContentLength = Data.Length;
MyRequest.ContentType = “application/xml”;
MyRequest.KeepAlive = false;
//Send the Stream
using(Stream RequestStream=MyRequest.GetRequestStream())
{
RequestStream.Write(Data, 0, Data.Length);
}
//Read the Response.
string Response_Xml = string.Empty;
using(HttpWebResponse MyResponse=(HttpWebResponse)MyRequest.GetResponse())
{
using (Stream ResponseStream =MyResponse.GetResponseStream())
{
using(StreamReader ResponseStreamReader=new StreamReader(ResponseStream))
{
Response_Xml = ResponseStreamReader.ReadToEnd();
}
}

}

}

}
catch (Exception Ex)
{
throw new Exception(Ex.Message);
}

}
#endregion
}