Showing posts with label ASP.NET. Show all posts
Showing posts with label ASP.NET. Show all posts

Monday, December 27, 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();
}

What is 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;

What is 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;
}
}