Tuesday, October 26, 2010

Hari Om Pawaar

रात के अँधेरे में न ऐसा कोई काम करो के मुह को छुपाना पड़े दिन के उजाले में
और दिन के उजाले में ऐसा न कोई काम करो के नीद नहीं आये तुम्हे रात के अँधेरे में 

Wednesday, October 20, 2010

How to send an email via GMail using .NET

It’s really easy to use GMail as an SMTP client:
public class GoogleSmtpTest
{
    public void SendEmailViaGmail()
    {
        var message = new MailMessage(
            "xxx@gmail.com",
            "yyy@joebloggs.com", 
            "Hi via Google SMTP",
            "some body");

        var client = new SmtpClient("smtp.gmail.com")
        {
            EnableSsl = true,
            Port = 587,
            Credentials = new NetworkCredential("xxx@gmail.com", "xxx's gmail password")
        };

        client.Send(message);
    }
}
The only thing to note is that the SSL port given in the documentation (465) doesn’t seem to work, but the TLS one (587) works fine.

Monday, October 18, 2010

Hyperlink in DataList Asp.Net 2.0

Simple hyperlink in asp.net 2.0 Datalist
< asp:HyperLink runat="server" ID="Hyperlink2" Text='< %# Bind("Contact_URL") % >' NavigateUrl='< %# Bind("Contact_URL") % >' Target="_blank">

IE 9 beta is out for testing

New News ! Microsoft's Internet Explorer 9 is now out for developers to test. IE9 beta is available in 33 languages. By testing IE9 beta it seems like a lot of effort has gone into making IE 9 a better browser as it is much more faster than IE8. 


One of the coolest feature of Internet Explorer 9 beta is that it automatically warns you via a pop-up message window when your add-on features increase the load time and browsing time, and it give you the feature to disable them. 


IE9 beta supports HTML5 AND CSS3.


IE9 beta is definitely worth a try. Let's try it !

The Internet Explorer 9 beta version can be downloaded from here: http://ie.microsoft.com/testdrive/

A small note: As of most windows products, the install requires a restart... So, do it in your spare time when a restart wont mess everything else in your world !!!


Reset password for sa using windows authentication

So, happily installing the new database on my dev machine and realized had forgotten the "sa" password. It kept on giving me the infamous "Login failed for user sa, error 18456" error message.

So, I googled and found the following two methords

sp_password NULL,'new_password','sa'
go


Used this and it worked like a charm... Another method I found was

1. Open the "SQL Server Enterprise Manager". This is usually under "Start"-->"Programs"-->"Microsoft SQL Server".

2. Navigate to the "Logins" object under the "Security" folder on the SQL Server you wish to administer. Then, right click on the 'sa' account and select "Properties".

3. Now, enter a new password in the "Password" field under the "Authentication" options.

Wednesday, October 13, 2010

SQL SERVER – Retrieve Current Date Time in SQL Server CURRENT_TIMESTAMP, GETDATE(), {fn NOW()}

There are three ways to retrieve the current datetime in SQL SERVER.

CURRENT_TIMESTAMP, GETDATE(), {fn NOW()}
CURRENT_TIMESTAMP
CURRENT_TIMESTAMP is a nondeterministic function. Views and expressions that reference this column cannot be indexed. CURRENT_TIMESTAMP can be used to print the current date and time every time that the report is produced.
GETDATE()
GETDATE is a nondeterministic function. Views and expressions that reference this column cannot be indexed. GETDATE can be used to print the current date and time every time that the report is produced.
{fn Now()}
The {fn Now()} is an ODBC canonical function which can be used in T-SQL since the OLE DB provider for SQL Server supports them. {fn Now()} can be used to print the current date and time every time that the report is produced.
If you run following script in Query Analyzer. I will give you same results. If you see execution plan there is no performance difference. It is same for all the three select statement.
SELECT CURRENT_TIMESTAMPGOSELECT {fn NOW()}
GO
SELECT GETDATE()GO

Performance:
There is absolutely no difference in using any of them. As they are absolutely same.
My Preference:
I like GETDATE(). Why? Why bother when they are same!!!

Update Table values based on another table values

You may wish to update records in one table based on values in another table. Since you can't list more than one table in the UPDATE statement, you can use the EXISTS clause.
For example:
UPDATE suppliers
SET supplier_name =( SELECT customers.name
FROM customers
WHERE customers.customer_id = suppliers.supplier_id)
WHERE EXISTS
  ( SELECT customers.name
    FROM customers
    WHERE customers.customer_id = suppliers.supplier_id);
Whenever a supplier_id matched a customer_id value, the supplier_name would be overwritten to the customer name from the customers table.

Passing of death wand in Harry Potter book 7

So I listened to harry potter audio book and finally figured out the order of passing of elder's wand.. here it is

Tuesday, October 12, 2010

.NET Runtime Optimization Service (clr_optimization_v2.0.50727_32) - Failed to compile: D:\Apps\SQLServer\100\Tools\Binn\VSShell\Common7\Tools\VDT\DataProjects.dll . Error code = 0x8007000b

So, I installed SQL Server 2008 and changed my TCP/IP port

Why ? you'd ask

Ah am just too paranoid..thats why.

Anyways, I do that and try to connect and SQL Server Gods get angry on me

"Msg 18456, Level 14, State 1, Server , Line 1
Login failed for user ''"


".NET Runtime Optimization Service (clr_optimization_v2.0.50727_32) - Failed to compile: D:\Apps\SQLServer\100\Tools\Binn\VSShell\Common7\Tools\VDT\DataProjects.dll . Error code = 0x8007000b"

I restarted my server...and the issue seems to have fixed. 

Now I know what to do next time !!!

Get list names of tables in database sql server 2008

I was trying to clean up my database and first step stumped me. I couldnt figure out how to get the table names from Sql Server 2008 database


SELECT TABLE_NAME, *
FROM INFORMATION_SCHEMA.TABLES


This works perfectly


Gave me the following information

TABLE_NAMETABLE_CATALOGTABLE_SCHEMATABLE_NAMETABLE_TYPE
categoryDBNamedbocategoryBASE TABLE

Then, I tried the following


SELECT s.name + '.' + o.name as tableName
FROM   sys.schemas s
JOIN   sys.objects o ON s.schema_id = o.schema_id
WHERE  o.type = 'U'
ORDER  BY s.name, o.name

and it gave me the following

TABLE_NAME
dbo.category

Other possible types that can be used:

  • C: Check constraint
  • D: Default constraint
  • F: Foreign Key constraint
  • L: Log
  • P: Stored procedure
  • PK: Primary Key constraint
  • RF: Replication Filter stored procedure
  • S: System table
  • TR: Trigger
  • U: User table
  • UQ: Unique constraint
  • V: View
  • X: Extended stored procedure

Monday, October 11, 2010

Ever needed to copy an SQL Server database, but get only its data and leave out everything else?


Here's a T-SQL script that you can use to copy all the database's tables' data, and leave out everything else (indexes, partitions, programmability components, etc...)
The script will go through all tables in the 'dbo' schema of the source database (named 'sourceDatabase' in this example), create a copy of the table in the 'dbo' schema of the destination database (named 'targetDatabase') and insert all data from the source table in the destination table (by using a SELECT INTO).
Here's the code:
DECLARE @tableName varchar(300)
DECLARE @sqlStmt nvarchar(300)

DECLARE curTables CURSOR LOCAL FOR
SELECT table_name FROM sourceDatabase.INFORMATION_SCHEMA.tables


OPEN curTables

FETCH NEXT FROM curTables INTO @tableName
WHILE @@FETCH_STATUS = 0
BEGIN
SET @sqlStmt = 'SELECT * INTO targetDatabase.dbo.' + @tableName + ' FROM sourceDatabase.dbo.' + @tableName
EXEC sp_executesql @sqlStmt

FETCH NEXT FROM curTables INTO @tableName
END

CLOSE curTables
DEALLOCATE curTables

Thursday, October 7, 2010

Karan ka daan

बात उन दिनों की है जब महाराज युधिष्ठिर इंद्रप्रस्थ पर राज्य करते थे। राजा होने के नाते वे काफी दान-पुण्य भी करते थे। धीरे-धीरे उनकी प्रसिद्धि दानवीर के रूप में फैलने लगी और पांडवों को इसका अभिमान होने लगा। कहते हैं कि भगवान दर्पहारी होते हैं। अपने भक्तों का अभिमान, तो उन्हें बिल्कुल पसंद नहीं। एक बार श्रीकृष्ण इंद्रप्रस्थ पहुंचे। भीम व अजरुन ने उनके सामने युधिष्ठिर की प्रशंसा शुरू की। दोनों ने बताया कि वे कितने बड़े दानी हैं। 

तब कृष्ण ने उन्हें बीच में ही टोक दिया और कहा, ‘लेकिन हमने कर्ण जैसा दानवीर और नहीं सुना।’ पांडवों को यह बात पसंद नहीं आई। भीम ने पूछ ही लिया, ‘भला वो कैसे?’ कृष्ण ने कहा कि ‘समय आने पर बतलाऊंगा।’ बात आई-गई हो गई। कुछ ही दिनों में सावन शुरू हो गए व वर्षा की झड़ी लग गई। उस समय एक याचक युधिष्ठिर के पास आया और बोला, ‘महाराज! मैं आपके राज्य में रहने वाला एक ब्राह्मण हूं। 

आज मेरा व्रत है और हवन किए बिना मैं कुछ भी नहीं खाता-पीता। कई दिनों से मेरे पास यज्ञ के लिए चंदन की लकड़ी नहीं है। यदि आपके पास हो तो, कृपा कर मुझे दे दें। अन्यथा मैं हवन पूरा नहीं कर पाऊंगा और भूखा-प्यासा मर जाऊंगा।’ युधिष्ठिर ने तुरंत कोषागार के कर्मचारी को बुलवाया और कोष से चंदन की लकड़ी देने का आदेश दिया।

संयोग से कोषागार में सूखी लकड़ी नहीं थी। तब महाराज ने भीम व अजरुन को चंदन की लकड़ी का प्रबंध करने का आदेश दिया। लेकिन काफी दौड़-धूप के बाद भी सूखी लकड़ी की व्यवस्था नहीं हो पाई। तब ब्राह्मण को हताश होते देख कृष्ण ने कहा, ‘मेरे अनुमान से एक स्थान पर आपको लकड़ी मिल सकती है, आइए मेरे साथ।’ ब्राह्मण की आखों में चमक आ गई। 

भगवान ने अजरुन व भीम को भी इशारा किया, वेष बदलकर वे भी ब्राह्मण के संग हो लिए। कृष्ण सबको लेकर कर्ण के महल में गए। सभी ब्राह्मणों के वेष में थे, अत: कर्ण ने उन्हें पहचाना नहीं। याचक ब्राह्मण ने जाकर लकड़ी की अपनी वही मांग दोहराई। कर्ण ने भी अपने भंडार के मुखिया को बुलवाकर सूखी लकड़ी देने के लिए कहा, वहां भी वही उत्तर प्राप्त हुआ। 

ब्राह्मण निराश हो गया। अजरुन-भीम प्रश्न-सूचक निगाहों से भगवान को ताकने लगे। लेकिन वे अपनी चिर-परिचित मुस्कान लिए बैठे रहे। तभी कर्ण ने कहा, ‘हे देवता! आप निराश न हों, एक उपाय है मेरे पास।’ देखते ही देखते कर्ण ने अपने महल के खिड़की-दरवाज़ों में लगी चंदन की लकड़ी काट-काट कर ढेर लगा दी, फिर ब्राह्मण से कहा, ‘आपको जितनी लकड़ी चाहिए, कृपया ले जाइए।’ कर्ण ने लकड़ी पहुंचाने के लिए ब्राह्मण के साथ अपना सेवक भी भेज दिया। ब्राह्मण लकड़ी लेकर कर्ण को आशीर्वाद देता हुआ लौट गया। पांडव व श्रीकृष्ण भी लौट आए। 

वापस आकर भगवान ने कहा, ‘साधारण अवस्था में दान देना कोई विशेषता नहीं है, असाधारण परिस्थिति में किसी के लिए अपने सर्वस्व को त्याग देने का ही नाम दान है। अन्यथा चंदन की लकड़ी के खिड़की-द्वार तो आपके महल में भी थे।’ इस कहानी का तात्पर्य यह है कि हमें ऐसे कार्य करने चाहिए कि हम उस स्थिति तक पहुंच जाएं जहां पर स्वाभाविक रूप से जीव भगवान की सेवा करता है। 

हमें भगवान को देखने की चेष्टा नहीं करनी चाहिए, बल्कि अपने को ऐसे कार्यो में संलग्न करना चाहिए कि भगवान स्वयं हमें देखें। केवल एक गुण या एक कार्य में अगर हम पूरी निष्ठा से अपने को लगा दें, तो कोई कारण नहीं कि भगवान हम पर प्रसन्न न हों। कर्ण ने कोई विशेष कार्य नहीं किया, किंतु उसने अपना यह नियम भंग नहीं होने दिया कि उसके द्वार से कोई निराश नहीं लौटेगा।