Thursday, July 31, 2008

Writing XML dynamically

string xmlFile = Server.MapPath("UserControl.xml");
System.Xml.XmlTextWriter writer = new System.Xml.XmlTextWriter(xmlFile, null);
writer.WriteStartDocument();
writer.Formatting = Formatting.Indented;
writer.WriteStartElement("InputValue");


for (int i = 0; i < 3; i++)
{

writer.WriteAttributeString(i.ToString(),i.ToString());
//writer.WriteElementString(i.ToString(), i.ToString());
}
writer.WriteEndElement();
writer.Close();

Monday, July 28, 2008

Range value setting in C#

string Source = TextBox2.Text;
string oops1,oops2;
int a1, a2;
string[] First = Source.Split('-');
for (int i = 1; i < First.Length; i++)
{
oops1 = First[0];
oops2 = First[1];
a1 = Convert.ToInt32(oops1);
a2 = Convert.ToInt32(oops2);
for (int ii = a1; ii <= a2; ii++)
{
DropDownList1.Items.Add(ii.ToString());
}

}

Sunday, July 27, 2008

Extracting string through comma separator in C#

Separating the given string into commas and adding them to dropdownlist.


string Months = TextBox1.Text;
string[] ind = Months.Split(new char[]{','});
foreach(string m in ind)
{

DropDownList1.Items.Add(m.ToString());



}

How to Querying XML raw data

This code snippet for loading XML file in to Dataset and then querying particular record as we practising on SQL-Query.

DataSet ds = new DataSet();
XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(File.ReadAllText(@"E://Form.xml"));
ds.ReadXml(new XmlNodeReader(xDoc));
foreach (DataRow dr in ds.Tables[0].Rows)
{
if (dr["ID"].Equals(TextBox1.Text))
{
Response.Write(dr["txtControlName"].ToString() + "\t");
Response.Write(dr["lblControlName"].ToString());

}


}

Wednesday, July 23, 2008

DataSet as ArrayList in C#

protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=(local);Database=Test;user id=sa;pwd=####");
con.Open();
SqlDataAdapter sq = new SqlDataAdapter("SELECT *From Employee", con);
DataSet ds = new DataSet();
sq.Fill(ds, "Employee");
ArrayList arrList = new ArrayList();


foreach (DataRow dr in ds.Tables[0].Rows)
{

arrList.Add(dr["Name"]);
GridView1.DataSource = arrList;
GridView1.DataBind();

string[] strArray = arrList.ToArray(typeof(string)) as string[];


}

sq.Dispose();
con.Close();
Response.Write("Done!");

}
}

Tuesday, July 22, 2008

Dynamically showing the TextBox in C#

Its a manual TextBox showing accordingly the requirements.

string[] s1 ={ "TextBox4", "TextBox3","TextBox1" };

TextBox[] _TxtLoop = new TextBox[2];
for (int i = 0;i<_TxtLoop.Length; i++)
{
for(int ii=0;ii {

string t1 =s1[ii].ToString();
TextBox txt=(TextBox)FindControl(t1);
txt.Visible=true;

}


}

Monday, July 21, 2008

dynamically adding TextBox

SqlConnection con = new SqlConnection("Data Source=(local);Database=Test;user id=sa;pwd=***");
con.Open();
SqlDataAdapter sq = new SqlDataAdapter("SELECT *From Employee", con);
DataSet ds = new DataSet();
sq.Fill(ds, "Employee");

foreach (DataRow dr in ds.Tables[0].Rows)
{
string test = "Murugesan";

TextBox txt = (TextBox)this.FindControl(test);

txt.Visible = true;

}


sq.Dispose();
con.Close();


}

Tuesday, July 15, 2008

switch-vb.net

Sub Main()
Dim keyIn As Integer
WriteLine("Enter a number between 1 and 4")
keyIn = Val(ReadLine())

Select Case keyIn
Case 1
WriteLine("You entered 1")
Case 2
WriteLine("You entered 2")
Case 3
WriteLine("You entered 3")
Case 4
WriteLine("You entered 4")
End Select

End Sub

Wednesday, July 02, 2008

Reflection-C#

This part is copied from Gopalan Suresh Raj's article for my reference purpose.

C# Reflection and Dynamic Method Invocation
Gopalan Suresh Raj

The Reflection API allows a C# program to inspect and manipulate itself. It can be used to effectively find all the types in an assembly and/or dynamically invoke methods in an assembly. It can at times even be used to emit Intermediate Language code on the fly so that the generated code can be executed directly.

Reflection is also used to obtain information about a class and its members. Reflection can be used to manipulate other objects on the .NET platform.

The Reflection API uses the System.Reflection namespace, with the Type class to identify the Type of the Class being reflected, and fields of a struct or enum represented by the FieldInfo class, Members of the reflected class represented by the MemberInfo class, Methods of a reflected class represented by the MethodInfo class, and parameters to Methods represented by the ParameterInfo class.

The Activator class's CreateInstance() method is used to dynamically invoke instances on the fly. Dynamic Invocation is very useful in providing a very late-binding architecture, where one component's runtime can be integrated with other component runtimes.

Obtaining All Classes and Their Type Information from an Assembly
using System;
using System.Reflection;

/**
* Develop a class that can Reflect all the Types available in an Assembly
*/
class ReflectTypes {

public static void Main (string[] args) {

// List all the types in the assembly that is passed in as a parameter
Assembly assembly = Assembly.LoadFrom (args[0]);

// Get all Types available in the assembly in an array
Type[] typeArray = assembly.GetTypes ();

Console.WriteLine ("The Different Types of Classes Available in {0} are:", args[0]);
Console.WriteLine ("_____");
// Walk through each Type and list their Information
foreach (Type type in typeArray) {
// Print the Class Name
Console.WriteLine ("Type Name : {0}", type.FullName);
// Print the name space
Console.WriteLine ("Type Namespace : {0}", type.Namespace);
// Print the Base Class Name
Console.WriteLine ("Type Base Class : {0}", (type.BaseType != null)?type.BaseType.FullName:"No Base Class Found...");
Console.WriteLine ("_____");
}
}
}