Tuesday, September 28, 2010

Kal chaudhvi ki raat thi


Kal chaudvin ki raat thi
Shab bhar raha charcha tera
Kal chaudvin ki raat thi
Kuch ne kaha yeh chaand hai
Kuch ne kaha chehra tera
Kal chaudvin ki raat thi

Hum bhi vahin maujood the
Humse bhi sab poochha kiye
Hum bhi vahin maujood the
Humse bhi sab poochha kiye
Hum has diye, hum chup rahe
Manzoor tha parda tera
Kal chaudvin ki raat thi

Is shaher mein kisse mile
Humse to chhooti mehfilein
Har shaks tera naam le
Har shaks deewana tera
Kal chaudvin ki raat thi

Koonche ko tere chhodkar
Jogi hi ban jaaye magar
Jungle tere, parbat tere
Basti teri, sahera tera
Kal chaudvin ki raat thi

Bedard sunni ho to chal
Kehta hai kya achchhi ghazal
Aashiq tera, ruswaan tera
Shaayar tera, inshaa tera
Kal chaudvin ki raat thi





Anoop Bhargav

रिश्तों को सीमाओं में नहीं बाँधा करते
उन्हें झूँठी परिभाषाओं में नहीं ढाला करते

उडनें दो इन्हें उन्मुक्त पँछियों की तरह
बहती हुई नदी की तरह
तलाश करनें दो इन्हें अपनी सीमाएं
खुद ही ढूँढ लेंगे उपमाएं

होनें दो वही जो क्षण कहे
सीमा वही हो जो मन कहे
* * *

Subhash Roy !!

मिटकर आओ
नहीं तुम प्रवेश नहीं
कर सकते यहाँ
दरवाजे बंद हैं तुम्हारे लिए

यह खाला का घर नहीं
कि जब चाहा चले आये

पहले साबित करो खुद को
जाओ चढ़ जाओ
सामने खड़ी चोटी पर
कहीं रुकना नहीं
किसी से रास्ता मत पूछना
पानी पीने के लिए
जलाशय पर ठहरना नहीं
सावधान रहना
आगे बढ़ते हुए
फलों से लदे पेड़ देख
चखने की आतुरता में
उलझना नहीं
भूख से आकुल न हो जाना
जब शिखर बिल्कुल पास हो
तब भी फिसल सकते हो
पांव जमाकर रखना
चोटी पर पहुँच जाओ तो
नीचे हजार फुट गहरी
खाई में छलांग लगा देना
और आ जाना
दरवाजा खुला मिलेगा

या फिर अपनी आँखें
चढ़ा दो मेरे चरणों में
तुम्हारे अंतरचक्षु
खोल दूंगा मैं
अपनी जिह्वा कतर दो
अजस्र स्वाद के
स्रोत से जोड़ दूंगा तुझे
कर्णद्वय अलग कर दो
अपने शरीर से
तुम्हारे भीतर बांसुरी
बज उठेगी तत्क्षण
खींच लो अपनी खाल
भर दूंगा तुम्हें
आनंद के स्पंदनस्पर्श से

परन्तु अंदर नहीं
आ सकोगे इतने भर से
जाओ, वेदी पर रखी
तलवार उठा लो
अपना सर काटकर
ले आओ अपनी हथेली
पर सम्हाले
दरवाजा खुला मिलेगा

यह प्रेम का घर है
यहाँ शीश उतारे बिना
कोई नहीं पाता प्रवेश
यहाँ इतनी जगह नहीं
कि दो समा जाएँ
आना ही है तो मिटकर आओ
दरवाजा खुला मिलेगा।
- सुभाष राय
* * *

getting stored procedure insert return the primary key in C#.Net

So, all I wanted to do was to insert a row in the database and get the primary key of thus inserted row.

C# decided not to help.

Here is how I did it:

first, wrote a stored procedure to add the row to the table



/****** Object:  StoredProcedure [dbo].[AddMember]    Script Date: 09/28/2010 20:28:25 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[AddMember]') AND type in (N'P', N'PC')) DROP PROCEDURE [dbo].[AddMember]
GO




/****** Object:  StoredProcedure [dbo].[AddMember]    Script Date: 09/28/2010 20:28:25 ******/
SET ANSI_NULLS ON
GO


SET QUOTED_IDENTIFIER ON
GO


-- ########################################################################
-- # STORED PROCEDURES                                     
-- ########################################################################


CREATE PROCEDURE [dbo].[AddMember]
           @SALUTATION varchar(10),
           @FIRST_NAME varchar(20),
           @MIDDLE_NAME varchar(20),
           @LAST_NAME varchar(25),
  @Person_ID int OUTPUT
AS
BEGIN


SET NOCOUNT ON


INSERT INTO [IAIPMD].[dbo].[Members]
           ([SALUTATION]
           ,[FIRST_NAME]
           ,[MIDDLE_NAME]
           ,[LAST_NAME])
     VALUES( @SALUTATION
           ,@FIRST_NAME
           ,@MIDDLE_NAME
           ,@LAST_NAME)
-- Only grab the scope identity if the INSERT worked
IF @@ROWCOUNT = 1
SET @Person_ID  =CAST (@@Identity as int)

SET NOCOUNT OFF
END


Then came the fun part, executing the procedure from C#. This is my solution


    try
            {
                SqlConnection conn = new SqlConnection(string);
                SqlCommand cmd = new SqlCommand("AddMember", conn);
                cmd.CommandType = CommandType.StoredProcedure;


                cmd.Parameters.AddWithValue("@SALUTATION", SALUTATION.Items[SALUTATION.SelectedIndex].Value);
                cmd.Parameters.AddWithValue("@FIRST_NAME", FIRST_NAME.Text);
                cmd.Parameters.AddWithValue("@MIDDLE_NAME", MIDDLE_NAME.Text);
                cmd.Parameters.AddWithValue("@LAST_NAME", LAST_NAME.Text);
//this is the fun and tricky part... pay attention here 
                SqlParameter paramter1 = cmd.Parameters.Add("@Person_ID", SqlDbType.Int);
                paramter1.Direction = ParameterDirection.Output;
                
                //The following value is now 6, the number of records inside the table
                conn.Open();


                cmd.ExecuteNonQuery();
                personID = (int)paramter1.Value; 
                Response.Write(personID);
                conn.Close();
                cmd.Dispose();
                conn.Dispose();
                lblShowResults.Text = "

Successful Addition

The member  has been added successfully
";

               


            }
            catch (Exception ex)
            {


                lblShowResults.Text = "

Error

Member could not be added
";



            }
 


Hope I remember this the next time I struggle with weird stored procedures and almost am ready to kick the computer into submission.... Hey, it worked this time !!!!

Null in inserts causes heartaches

Null or nonexistent data gets complicated when the application expects a data value and gets nothing or unexpected values; that's when you must perform coding that properly handles these situations to avoid application failure. This requires a close examination of the data wherever and whenever it's needed.
Null and void 
A null value is a value that doesn't refer to any object. It's the default value of reference-type variables (e.g., class, interface, delegate).
These items are called reference types because they're objects that store references to the actual data. String, object, and struct are reference type examples. The null keyword is a literal that represents a null reference with the C# .NET environment. You may use the null keyword to check or assign the value of an object. For example, the following C# code determines if a string value is null:
string sTest = "Test";
if (sTest == null) {
Console.WriteLine("sTest is Null")
}
This code works without any problems with C#, but there's no VB.NET equivalent of the null keyword. Instead, VB.NET uses the Nothing keyword. The following code demonstrates its use:
Dim sTest As String
If (sTest Is Nothing) Then
Console.WriteLine("sTest is Null")
End If
Another area of confusion stems from the fact that VB.NET doesn't treat null and Nothing as equals; consequently, a VB.NET programmer may have to check for both values.
The variation in support may cause confusion, but a developer rarely develops in both languages simultaneously. On the other hand, Microsoft provides a uniform method for working with null values: The base System.Convert namespace includes the DBNull object.
DBNull 
The use of the DBNull class indicates the absence of a known value. While the Microsoft documentation says it's typically in a database application, you may also use it with any type of data.
In database applications, a null object is a valid value for a field. This class differentiates between a null value (a null object) and an uninitialised value (the DBNull.Value instance). For example, a table can have records with uninitialised fields. By default, these uninitialised fields have the DBNull value.
You can utilise the DBNull object by way of its Value property. This represents a null value, which is never equal to anything. The following C# snippet revises the previous example to utilise the DBNull class:
if (sTest.Equals(System.DBNull.Value)) {
Console.WriteLine("sTest is null.");
Notice how this code utilises the string class's equality method. The VB.NET sample may utilise the same code, like this:
If (sTest.Equals(System.DBNull.Value) Then
Console.WriteLine("sTest is Null")
End  If
You may also use the DBNull.Value constant on the right-hand of an assignment operation, thus storing a null value within a reference type. In addition, the .NET Framework provides the IsDBNull function (in the System.Convert namespace), which is a shorthand approach for determining if a reference type contains a null value. The following VB.NET uses IsDBNull to check an object's value:
Dim oTest As Object = DBNull.Value
If ((IsDBNull(oTest))) Then
Console.WriteLine("oTest is Null")
End If
Even if you check for null values at every conceivable location within your code, they still may creep into the application. Fortunately, the try/catch exception block provides another line of defense; it allows you to catch any unhandled exceptions that null values cause. The System namespace provides the NullReferenceException for these situations. See how the following C# code uses NullReferenceException.
try {
if (sTest.Equals(System.DBNull.Value)) {
Console.WriteLine("sTest is null.");
}
} catch (System.NullReferenceException e) {
Console.WriteLine("Error: " + e.ToString());
}
Handle your data with care 
Applications don't always receive the type of data they expect; for instance, an app may be looking for integer values and receive string or null values. Therefore, since data is the backbone of an application, you must be extremely careful when working with it.

Thursday, September 23, 2010

SCOPE_IDENTITY() return the id from the database on insert

As a .NET programmer most of my time is spent coding in C# and I try to avoid writing SQL where possible. Why is it so complicated to write a loop without turning the database upside down? I'm not saying it's not possible it's just the language can me annoying at times.

Anyways, Recently I had to write an insert stored procedure and needed to return the ID of the row I was inserting. While writing my usual bad SQL I came across a fascinating function I’ve never used before, SCOPE_IDENTITY(). 

Here's short example of how you can use the SCOPE_IDENTITY() function while doing an SQL INSERT to return the identity value of the row just inserted.

In this example I’m inserting a new customer record using a stored procedure and have declared a output parameter which I'd like to return to my application.

CREATE PROCEDURE [dbo].[Customer_Insert]
@Name VARCHAR(255),
@Email VARCHAR(255),
@Phone VARCHAR(255),
@CustomerID INT OUTPUT
AS
BEGIN
INSERT INTO dbo.Customer ([Name], Email, Phone)
VALUES (@Name,@Email,@Phone)
 
SET @CustomerID = SELECT CustomerID 
FROM dbo.Customer 
WHERE [Name] = @Name 
AND Email = @Email 
AND Phone = @Phone
END


What I’ve done wrong here is after inserting run a select to try and SET the @CustomerID. This is a bit risky as it could return more than 1 record, or it return an of completely different record. 

SQL's SCOPE_IDENTITY() function can return the last identity value inserted into an identity column in the same scope e.g. a stored procedure, trigger, function, or batch. Alternativly you can also use the @@IDENTITY function to return the last identity value inserted into an identity column regardless of the scope.

Now lets improve my stored procedure using SCOPE_IDENTITY().

CREATE PROCEDURE [dbo].[Customer_Insert]
@Name VARCHAR(255),
@Email VARCHAR(255),
@Phone VARCHAR(255),
@CustomerID INT OUTPUT
AS
BEGIN
INSERT INTO dbo.Customer ([Name], Email, Phone)
VALUES (@Name,@Email,@Phone)
 
SET @CustomerID = CAST(SCOPE_IDENTITY() AS INT)
END

I've casted the value returned by SCOPE_IDENTITY() to make sure the confirms to the return parametor @CustomerID INT OUTPUT. 

Tuesday, September 14, 2010

SQL COUNT( NULLIF( .. ) ) blew my mind


Using the SQL GROUP BY clause is really awesome. It makes gathering data "about" the data very easy. Sometimes, though, when you group data, not only do you want to count the number of records in a given group, you want to count the number of records in a group with a given property.
This is something that is not as easy.

I wanted to count the number of certain types of people and simple

Select count(personID) from people where condition='True' 

kept on returning "NULL" for certain rows.

And thats when I met NULLIF...

NULLIF() is pretty bad ass. If you have not used it, it takes two arguments and works such that if the two arguments are equal in value, it returns NULL, otherwise, it just returns the first argument it was passed. So, for instance, NULLIF( 1, 1 ) would return NULL, but NULLIF( 1, 0 ) would return 1.
Now, how does NULLIF() help us? It helps us because the SQL COUNT() aggregate does NOT count NULL values. This is HUGE.

My SQL query now simply becomes

Select COUNT(NULLIF([PERSONID], 0)) from people where condition='True' 

How freakin' cool is that?!? When I randomly tried this one day and it worked, it blew my mind. Now, I work in MS SQL Server. I don't know if the NULLIF() function is standard. If is not, then, that's probably why your database was free :) (I'm just messing with you...)

Note: Please do not use "group by" with NULLIF. It keeps returning NULL regardless in that case.

Silly Radiobuttonlist of asp.net

I added a asp:radiobuttonlist to a page and took me a while to get it to look just right...

ASP.NET provides you with a lot of options for tweaking and setting the properties for radiobuttonlist

Here is some nice info I found:

The Layout properties of RadioButtonList control include the following 3 properties:
RepeatColumns: It accepts the integer value that renders the layout of the radiobuttonlist control with specified number of columns. 0 is the default value of RepeatColumns property that renders the list items as a single vertical list of radio button items.
RepeatDirection: It allows you to set the rendering direction of list items of the radiobuttonlist control. RepeatDirection property accepts one of the following predefined values:
1. Horizontal: this value renders the list items in left to right manner. Each item renders at the right side of the previous until it reaches the specified number of RepeatColumns property value. Then it renders the next item in new line or row of the table based on the RepeatLayout property value.
2. Vertical: this value renders the list items in top to bottom manner. Each items renders below the previous item in a new row cell each time. It applies some logic to render the vertical layout of the radiobuttonlist items.
RepeatLayout: It enables you to specify the layout type of the radiobuttonlist control. You can specify the RepeatLayout property using any one of the following predefined keywords:
1. Table: It renders the list items using HTML table element. It generates the table cells and rows to display the radiobuttonlist items using table layout.
2. Flow: It renders the list items using HTML span element as a container and it uses the HTML
tag to enter the line break between the radio button list items to generate new rows.
"Table" is the default value of RepeatLayout property.
Following is a list of CSS based properties of the ASP.Net RadioButtonList control that can be used to customize its look and feel:
BackColor: allows you to set the background color.
BorderColor: allows you to set the border color.
BorderStyle: allows you to set the predefined style of the border around the radiobuttonlist. You can specify the border styles such as solid, dotted, dashed etc.
BorderWidth: allows you to set the width of the border using CSS units such as px, pt, cm etc.
CellPadding: It sets the cell padding for the table cells of the radiobuttonlist control when rendered with RepeatLayout property as "Table".
CellSpacing: It sets the cell spacing for the table cells of the radiobuttonlist control when rendered with RepeatLayout property as "Table".
CssClass: allows you to specify the name of the CSS class to customize the look and feel of radiobuttonlist control.
Font Style Properties: the font style properties of radiobuttonlist include font-size, family, bold, italics etc.
ForeColor: Sets the color of the text content of the listitems
TextAlign: It enables you to set the horizontal alignment of the text aligned to left or right direction.

Thursday, September 2, 2010

ye kaun aa gai dilruba mehki mehki

ye kaun aa gai dilruba mehki mehki
fizaa mehki mehki hawa mehki mehki

vo aankhon mein kaajal vo baalon mein gajraa
hatheli pe uske hinaa mehki mehki

Khuda jaane kis-kis ki ye jaan legi
vo qaatil adaa vo sabaa mehki mehki

savere savere mere ghar pe aai
ae 'Hasrat' vo baad-e-sabaa mehki mehk