Posts

Showing posts from December, 2011

Captcha Code

Hello readers. I have written a code to generate Captcha Code. I have created a DLL file for this, all u have  to do is - just add reference of the DLL and use to generate captcha code. 1. Add Reference of the DLL  that is added to bin folder of your application. 2. Now add aspx page and add an Image control. < asp : Image ID ="Image1" runat ="server" /> < asp : TextBox ID ="txtCaptcha" runat ="server"></ asp : TextBox >         < asp : Button ID ="btnValidate" runat ="server" Text ="Validate" OnClick ="btnValidate_Click" />         < br />         < br />         < asp : Label ID ="lblMessage" runat ="server"></ asp : Label ></ div >  3. Now write in the .cs page using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.

Preserve Primary Key Value - Always in sequence

Hello readers. One of the major problem with primary key when set to auto increment is, when a row is deleted from the table the value of the primary key of that row is lost. So here is a small Trigger script for the table that will keep the primary key always in sequence that means this trigger always maintain the order or primary key like 1,2,3,4... We need two triggers to make it possible. One check the value of primary key at time of Insert and other checks at Deletion. First create a table  CREATE TABLE AITEST ( TBLID  INT IDENTITY ( 1 , 1 ) PRIMARY KEY ,   TBLNAME  NVARCHAR ( 50 )) Trigger One CREATE TRIGGER [dbo] . [CHKSETIDENT] ON [dbo] . [AITEST] AFTER INSERT AS DECLARE @CURIDENT INT = ( SELECT IDENT_CURRENT ( 'AITEST' )) DECLARE @LASTROW INT = ( SELECT COUNT (*) FROM AITEST ) IF @CURIDENT < @LASTROW       BEGIN             DBCC CHECKIDENT ( 'AITEST' , RESEED , @LASTROW )             SELECT * FROM AITEST       END ELSE

Concatenate values of those column values to single having same ID

Suppose there is table name Items having id and item name. Now you want to show that the items name having same id are show in one row and with all the item name in single column of the row having same id. Below are the steps: First Create a table CREATE TABLE items ( id INT , itemname VARCHAR ( 30 )) GO Inserting values to items table INSERT INTO items ( id , itemname ) VALUES ( 1 , 'Pen' ),( 2 , 'Pencil' ),( 3 , 'Rubber' ),( 1 , 'Scale' ),( 1 , 'Copy' ), ( 3 , 'Box' ),( 2 , 'Compas' ),( 4 , 'Stapler' ),( 5 , 'Paper' ),( 6 , 'Ink' ) GO SELECT * FROM items GO /* Below is the query to get the desired out put */ DECLARE @ctr INT DECLARE @val INT DECLARE @itemName VARCHAR ( 30 ) CREATE TABLE #tempid ( row INT IDENTITY ( 1 , 1 ), id INT , itemnames VARCHAR ( 30 )) CREATE TABLE #tempitems ( id CHAR ( 4 ), itemname VARCHAR ( 1000 )) INSERT INTO #tempid ( id , itemnames )

Multiple Value Insert In A Single Query

Hello readers, Below is the query to inset multiple values in a Table name items in a single INSERT statement. First Create a table CREATE   TABLE  items ( id  INT ,  itemname  VARCHAR ( 30 )) GO Inserting values to items table INSERT   INTO  items ( id , itemname )   VALUES  ( 1 ,   'Pen' ),( 2 , 'Pencil' ),( 3 , 'Rubber' ),( 1 , 'Scale' ),( 1 , 'Copy' ), ( 3 , 'Box' ),( 2 , 'Compas' ),( 4 , 'Stapler' ),( 5 , 'Paper' ),( 6 , 'Ink' ) GO SELECT   *   FROM  items Query is simple. Not too complicated. Just add new values after first by placing comma , and type next values.