Friday, November 26, 2010

How to Read and Get Data From XML Document in C#

By this Method we can easily get Data from XML Database. For this first of all create a "ContactsList.xml" XML file and under root section add your recoreds.

public void GetData()
{
try
{
string path = "ContactsList.xml";
FileStream fs = new FileStream path,FileMode.Open,FileAccess.Read, FileShare.ReadWrite);
DataSet ds = new DataSet();
ds.ReadXml(fs);
if (ds.Tables.Count != 0)
{
DataTable dt = ds.Tables[0];

int a = dt.Rows.Count;

for (int i = 0; i < a; i++)
{
string[] str1 = new string[4];
str1[0] = dt.Rows[i].ItemArray[0].ToString();


str1[1] = dt.Rows[i].ItemArray[1].ToString();
str1[2] = dt.Rows[i].ItemArray[2].ToString();
int count = Convert.ToInt32(dt.Rows[i].ItemArray[7].ToString());
if (count.Equals(1) || count.Equals(0))
{
str1[3] = dt.Rows[i].ItemArray[3].ToString();
ListViewItem item1 = new ListViewItem(str1);
ldtvw_Contact.Items.Add(item1);
}
else
{
string interests = dt.Rows[i].ItemArray[3].ToString() + "," + dt.Rows[i].ItemArray[4].ToString() + "," + dt.Rows[i].ItemArray[5].ToString() + "," + dt.Rows[i].ItemArray[6].ToString();
str1[3] = interests;
ListViewItem item1 = new ListViewItem(str1);
ldtvw_Contact.Items.Add(item1);
}

}

fs.Close();
}

Thats All....

Thursday, November 25, 2010

How to Generate Random String in C#, ASP.NET

To Generate a Random String is very Tricky and useful stuff to Know as we can use this functionality at the time of Generating Random Password automatically for each User Registering on the website.

I am sure this will help you a lot, here I am writing the Method to Generate the Random String.

private string RandomString(int size, bool lowerCase)
    {
        StringBuilder builder = new StringBuilder();
        Random random = new Random();
        char ch;
        for (int i = 0; i < size; i++)
        {
            ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
            builder.Append(ch);
        }
        if (lowerCase)
            return builder.ToString().ToLower();
        return builder.ToString();
    }
Now the above code will generate the Random String. If you want to have a Strong Password then password must contains Number. So lets go to generate Random Numbers:
public string GetRandomNumber()
    {
        StringBuilder strbuild = new StringBuilder();
        strbuild.Append(RandomNumber(1000, 9999));
        return strbuild.ToString();
    }

private int RandomNumber(int min, int max)
    {
        Random random = new Random();
        return random.Next(min, max);
    }

Now you are ready to generate a random password. for that you have to call these methods as given below:
public string GetPassword()
    {
        StringBuilder builder = new StringBuilder();
        builder.Append(RandomString(4, true));
        builder.Append(RandomNumber(1000, 9999));
        builder.Append(RandomString(2, false));
        return builder.ToString();
    }

That's All, Please give your comments if the post is helpful.

Sunday, November 21, 2010

Freeze GridView Header While Scrolling

This is very common problem in Web development that we need to freeze the GridView header at the time of scrolling, Here is one simple tips:

Step 1 : Create a CSS class as following
<br />.HeaderFreez <br />{ <br />position:relative ; <br />top:expression(this.offsetParent.scrollTop); <br />z-index: 10; <br />} <br />

Step 2 Set Gridview’s HeaderStyle CssClass as follows
CssClass="HeaderFreez"
That’s all guys ...

How to Save Valuable Code section in your Visual Toolbox

Very useful for re-Usability of the code...


Step 1: Very Simple Just Copy the Code section and Drop on the Toolbox.
thats it...

If you wanna give your unique Identification to the code than Give a Name to that by Right clicking and Renaming that.

Connection Pooling

Creating and establishing a connection with a database server is a high resource consuming process and hence by result more time taking.
If any application needs to fire any query against any database server, we need to first establish a connection with the server and then execute the query against that database server.
So why we will again and again open and close the connection of Database, for that only connection pooling came in feature.
When the first time ADO.Net is stabilizing connection with the database and connection open with the respective credentials the ADO. Net  is  saving the references for the that connection and while closing the connection ADO.Net is not closing total connection and when another request done for the Database action ADO.Net using that references for the Database process so no Hassel for opening and closing connection again and again, and process is more faster .
Sample Connection String with Connection Pooling…..

initial catalog=Northwind; Data Source=localhost; Connection Timeout=30;
User Id=USER; Password=PASS; Min Pool Size=20; Max Pool Size=200;
 Incr Pool Size=10; Decr Pool Size=5;

Delegates:

A delegate is a type-safe object that can point to another method (or possibly multiple methods) in the application, which can be invoked at later time.

What Is A Web Service?

A Web Service is a program that can be invoked in a distributed web envi ronment. Web Service technologies like SOAP, WSDL, and UDDI are standards to describe, publish, discover, and use Web Services; they define a development model  for integrating applications over the web.

Steps to Create a Web Service

1.  Create (or use an existing) virtual directory for any folder on your computer.
2.  Create an empty file called “test.asmx” in the folder.
3.  Edit the file and add the  following text:
<%@ WebService Language="C#" Class="MyMath" %>
using System.Web.Services;
public class MyMath
{
    [WebMethod]
    public int Add(int num1, int num2)
    {
         return num1+num2;
    }
}