Monday, December 27, 2010

All about roolback in SQL

If accidentally you did some CRUD operation in the table then, here I am telling how to get back to the previous stage.

BEGIN TRANSACTION;
update tblName set FirstName='test' where id=1;
rollback ;


this will get you back to your previous stage(UNDO).
And if you want to get-back any particular column data then go for it.

BEGIN TRANSACTION transiction_name;
update tbl_name set FirstName='test' where id=1;
rollback transaction transiction_name;

But don't do commit; unless rollback will not work any more.

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