Tuesday, January 20, 2009

SQL Server 2005 Locking Hints

ROWLOCK
Use row-level locks when reading or modifying data.

PAGLOCK
Use page-level locks when reading or modifying data.

TABLOCK
Use a table lock when reading or modifying data.

DBLOCK
Use a database lock when reading or modifying data.

UPDLOCK
UPDLOCK reads data without blocking other readers, and update it later with the assurance that the data has not changed since last read.

XLOCK

Use exclusive locks instead of shared locks while reading a table, and use hold locks until the end of the statement or transaction.

HOLDLOCK
Use a hold lock to hold a lock until completion of the transaction, instead of releasing the lock as soon as the required table, row, or data page is no longer required.

NOLOCK
This does not lock any object. This is the default for SELECT operations. It does not apply to INSERT, UPDATE, and DELETE statements.

 

Thursday, November 6, 2008

ASP.NET Client Side State Management

 

http://dotnet.dzone.com/news/aspnet-server-side-state-manag

How to launch Window application from C# console application

This article is targeted for mid-level programmers who have already worked on C# language. This article uses batch files for demonstration and the reader should have preliminary knowledge of batch files.

Sample application describes how to call any Windows application or batch file using a C# console application. This application launches a new process and executes Windows applications in that process.

API Used

  • System.Console.WriteLine()
  • System.Diagnostics.Process
  • Process.Start()

 

Step by Step process to create a sample application

1.       Create a Console project called as InvokeBatchFile.

2.      Include the following namespce

                     Using System.Diagnostics;

3.      Add following code for the Main function

        Process p = null;

        try

        {

            string targetDir;

            targetDir = string.Format(@"C:\ CallBatchFile ");

            p = new Process();

            p.StartInfo.WorkingDirectory = targetDir;

            p.StartInfo.FileName = "MyBatch.bat";

p.StartInfo.Arguments = string.Format("C-Sharp Console application");

            p.StartInfo.CreateNoWindow = false;

            p.Start();

            p.WaitForExit();

         }

         catch (Exception ex)

         {

                Console.WriteLine("Exception Occurred :{0},{1}",

                          ex.Message, ex.StackTrace.ToString());

         }

 

 

               


C# Class Generator

 

Friday, August 29, 2008

Undocumented SQL Server 2000 functions

Encryption in SQL server [pwdencrypt,pwdcompare]

There are some hidden functions in SQL server through which we can encrypt any string and store the same in the table. This will be very helpful in encrypting the user password and other sensitive user data. Encryption supported by SQL server is one way hash. One way hash is nothing but the string encrypted cannot be decrypted. The only way is to compare values with encrypted string.

DECLARE @ClearPIN varchar (255)
DECLARE @EncryptedPIN varbinary(255)
SELECT @ClearPIN = 'test'
SELECT @EncryptedPIN = CONVERT (varbinary (255), pwdencrypt (@ClearPIN))
SELECT pwdcompare(@ClearPIN, @EncryptedPIN, 0)

In the above example @EncryptedPIN will store the cipher Text. The data in this string is not the encrypted string instead it will return the hash code of the supplied plain string.

Monday, June 2, 2008

Now let's get into details. First of all say in our aspx page we need to save the image to the server at the runtime. For this we need to use the stream class to read the file as a byte and then save it to the required path at a particular event. The event can vary from situation to situation. e.g in my project I did this in the page_load event. Some would definitely like to avail this just before producing the report.

Code for storing your Image in to the server

Listing 1

System.Drawing.Image drawingImage;

drawingImage = System.Drawing.Image.FromStream(new System.IO.MemoryStream (byteArr));

string strPath;

strPath = System.Web.HttpContext.Current.Request.MapPath("path");

drawingImage.Save(strPath, System.Drawing.Imaging.ImageFormat.Bmp);

After saving the image now its turn for designing the report. For this we need a dataset(xsd) which will store the Image.

For this add a dataset to your project. Create a datatable inside your dataset and add a column to the datatable for storing the image.One thing to note here is the datatype for the column. It should be System.Byte[].

Figure 1

After Creating the DataSet we can start on designing the report. Open the Crystal report, add the DataTable to the database fields of your report and then drag and drop the image field to the report.

Now for populating the datatable with the data at the runtime we will have a function inside a class that takes one paramater i.e the Image that needs to be displayed and using the data row we will add it to the datatable and finally return the datatable.

Now for displaying it actually in the report, set the datasource of the crystal report with the data table. The below code demonstrates this.

Code for setting the datasource of the Crystal Report

Listing 2

C#

ReportDocument rptTest1 = new ReportDocument();

rptTest1.Load(System.Web.HttpContext.Current.Request.MapPath("App_Reports/rptpic1.rpt"));

rptTest1.Database.Tables["Images"].SetDataSource(rptTest.ImageTable(System.Web.HttpContext.

Current.Request.MapPath("App_Data/test1.bmp")));

Code for filling the data table with the Image

Listing 3

C#

public class rptTest

{

public static DataTable ImageTable(string ImageFile)

{

DataTable data = newDataTable();

DataRow row;

data.TableName = "Images";

data.Columns.Add("img", System.Type.GetType("System.Byte[]"));

FileStream fs = new FileStream(ImageFile, FileMode.Open);

BinaryReader br = new BinaryReader(fs);

row = data.NewRow();

row[0] = br.ReadBytes(br.BaseStream.Length);

data.Rows.Add(row);

br = null;

fs.Close();

fs = null;

return data;

}

}

When the program runs, the image gets added up to the data table and then is pulled up by the crystal report without any database overhead.


Wednesday, February 20, 2008

Remove Spaces From String

using System.Text.RegularExpressions;

private string RemoveSpaces(string str)
{
string result = "";
Regex regulEx = new Regex(@"[\s]+");
result = regulEx.Replace(str," ");
return result;
}