Pankajmittal’s Blog

May 7, 2012

write a file on disk.

Filed under: Uncategorized — pankajmittal @ 10:20 am
Tags:


public static void WriteLogToTextFile(string Message)
{
string strLogFileName = "SMSLog.txt";
if (!File.Exists(AppDomain.CurrentDomain.BaseDirectory + strLogFileName))
{
File.Create(AppDomain.CurrentDomain.BaseDirectory + strLogFileName);
}

using (StreamWriter sWriter = File.AppendText(AppDomain.CurrentDomain.BaseDirectory + strLogFileName))
{
sWriter.Write("\r\n{0}: ", DateTime.Now);
sWriter.WriteLine("\r\n{0}", Message);
sWriter.WriteLine("--------------------------------------------------------------");
sWriter.Flush();
}
}

July 22, 2011

Search and replace data in sql

Filed under: Uncategorized — pankajmittal @ 11:03 am

--EXEC SearchAndReplace 'hin','Pankaj'


Create PROC SearchAndReplace 
    ( 
      @SearchStr nvarchar(100), 
      @ReplaceStr nvarchar(100) 
    ) 
AS 
    BEGIN 
/*
    — Copyright © 2010 Pankaj Mittal. All rights reserved. 
    — Purpose: To search all columns of all tables for a given search string and replace it with another string 
    
    — Site: http://pankajmittal.wordpress.com/ 
    — Tested on: SQL Server 7.0 and SQL Server 2000/2005 
    — Date modified: 29 July 2010 
*/
        SET NOCOUNT ON 

        DECLARE @TableName nvarchar(256), 
            @ColumnName nvarchar(128), 
            @SearchStr2 nvarchar(110), 
            @SQL nvarchar(4000), 
            @RCTR int 
        SET @TableName = '' 
        SET @SearchStr2 = '%' + @SearchStr +  '%'
        SET @RCTR = 0 

        WHILE @TableName IS NOT NULL 
            BEGIN 
                SET @ColumnName = '' 
                              SET @TableName = ( SELECT   MIN(QUOTENAME(TABLE_SCHEMA) + '.' 
                                                + QUOTENAME(TABLE_NAME)) 
                                   FROM     INFORMATION_SCHEMA.TABLES 
                                   WHERE    TABLE_TYPE = 'BASE TABLE' 
                                            AND QUOTENAME(TABLE_SCHEMA) + '.' 
                                            + QUOTENAME(TABLE_NAME) > @TableName 
                                            AND OBJECTPROPERTY(OBJECT_ID(QUOTENAME(TABLE_SCHEMA) 
+ '.' + QUOTENAME(TABLE_NAME)), 'IsMSShipped') = 0 
                                 ) 

SELECT @TableName,@ColumnName
                WHILE ( @TableName IS NOT NULL ) 
                    AND ( @ColumnName IS NOT NULL ) 
                    BEGIN 
                   
                        SET @ColumnName = ( SELECT  MIN(QUOTENAME(COLUMN_NAME)) 
                                            FROM    INFORMATION_SCHEMA.COLUMNS 
                                            WHERE   TABLE_SCHEMA = PARSENAME(@TableName, 2) 
                                                    AND TABLE_NAME = PARSENAME(@TableName, 1) 
                                                    AND DATA_TYPE IN (  'char ',  'varchar ',  'nchar ',  'nvarchar ' ) 
                                                    AND QUOTENAME(COLUMN_NAME) > @ColumnName 
                                          ) 
                        IF @ColumnName IS NOT NULL 
                            BEGIN 
                             
                                SET @SQL =  'UPDATE  ' + @TableName +  ' SET  ' 
                                    + @ColumnName +  ' =  REPLACE( ' 
                                    + @ColumnName +  ', ''' 
                                    --+ QUOTENAME(@SearchStr, '') +  ''',  ''' 
                                    --+ QUOTENAME(@ReplaceStr, '') 
                                     + @SearchStr +  ''',  ''' 
                                    + @ReplaceStr 
                                    +  ''') WHERE  ' + @ColumnName +  ' LIKE  ''' 
                                    + @SearchStr2 +''''
                                    PRINT @SQL
                                EXEC ( @SQL 
                                    ) 
                                SET @RCTR = @RCTR + @@ROWCOUNT 
                            END 
                    END    
            END 

        SELECT   'Replaced  ' + CAST(@RCTR AS varchar) +  ' occurence(s) ' AS  'Outcome ' 
    END 


GO

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

June 30, 2011

Calculate sunday between two dates with MS SQL

If you want to take into account holidays or other non-working days, then best approach would be to create an auxiliary calendar table. Then you could run queries against this table, not just to calculate the number of work days, but you could do much more.
For example, you could easily count number of Mondays between two dates, get the last day in each month of this year, count number of Fridays between two dates, etc.

Calendar table is a simple table with pre-populated dates. You need to create and populate this table only once, then use it whenever needed. Not all columns are required. Include only those which you will need.

CREATE TABLE Calendar 
 ( 
         Date smalldatetime NOT null PRIMARY KEY clustered,   
       IsWeekday bit,     
   IsHoliday bit,     
   TheYear smallint,   
    TheMonth tinyint,      
 TheDay tinyint, 
      TheQuarter tinyint,   
    TheWeek tinyint,     
  TheWeekday tinyint
) 
GO

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

Fill the calendar table with dates from a range which will suffice you.

 

Fill other columns in the calendar table. This query does not mark holidays because they are different in each country. You will have to set them yourself.

UPDATE Calendar SET
        IsWeekday = CASE WHEN DATENAME(dw, Date) IN ('Saturday', 'Sunday') THEN 0 ELSE 1 END,
        IsHoliday = 0,
        TheYear = YEAR(Date),
        TheMonth = MONTH(Date),
        TheDay = DAY(Date),
        TheQuarter = CASE
                WHEN MONTH(Date) <= 3 THEN 1
                WHEN MONTH(Date) <= 6 THEN 2
                WHEN MONTH(Date) <= 9 THEN 3
                ELSE 4 END,
        TheWeek = DATEPART(wk, Date),
        TheWeekday = DATEPART(dw, Date)

The calendar table is ready. You can now calculate the number of business days between two dates with this query:

DECLARE @startDate smalldatetime, @endDate smalldatetime
SELECT @startDate = '2010-05-10', @endDate = '2010-12-25';
 
SELECT COUNT(*) 
FROM Calendar
WHERE Date >= @startDate AND Date <= @endDate
        AND IsWeekday = 1 AND IsHoliday = 0

Or you can even easily list all those dates:

SELECT Date
FROM Calendar
WHERE Date >= @startDate AND Date <= @endDate
        AND IsWeekday = 1 AND IsHoliday = 0

April 15, 2011

Now you can publish your post through Microsoft word 2007

Filed under: Uncategorized — pankajmittal @ 11:14 am

Open new word document.

Choose new blogpost template.

Choose your provider (whom’s blogpost you used.).

Enter your blog url , userid and password

That’s it now enter the post and publish it. J enjoy

December 6, 2010

pankaj mittal wants to share their location with you on Google Latitude

Filed under: Uncategorized — pankajmittal @ 1:24 pm

pankaj mittal (pankajmittal3@gmail.com) wants to share their location with you on Google Latitude. You too can see where your friends are and share your location using Latitude from your phone, computer, or both.

Aww snap. You don’t have a Google Account.

To use Google Latitude, you’ll need to sign in with a Google Account. If you don’t have one, use the following steps:

  1. Create a new Gmail account at https://mail.google.com/mail/signup or create a Google Account for your existing email address at https://www.google.com/accounts/NewAccount
  2. Start using Latitude! Go to https://www.google.com/latitude from your phone or computer.
  3. Sign in with your Google Account and add pankaj mittal (pankajmittal3@gmail.com) as a friend.

(c) 2010 Google Inc., 1600 Amphitheatre Parkway, Mountain View, CA 94043, USA. Terms of Service | Privacy Policy

September 22, 2010

Error: Web site worker process has been terminated by IIS

Filed under: Uncategorized — pankajmittal @ 1:03 pm
To configure IIS 7 to allow the worker process to continue
  1. Open the Administrative Tools window.

    1. Click Start, and then choose Control Panel.

    2. In Control Panel, choose Switch to Classic View, if necessary, and then double-click Administrative Tools.

  2. In the Administrative Tools window, double-click Internet Information Services (IIS) Manager.

    IIS Manager opens.

  3. In the Connections pane, expand the <computer name> node if necessary.

  4. Under the <computer name> node, click Application Pools.

  5. In the Application Pools list, right-click the name of the pool your application runs in, and then click Advanced Settings.

  6. In the Advanced Settings dialog box, locate the Process Model section, and perform one of the following actions:

    • Set Ping Enabled to False.

    • Set Ping Maximum Response Time to a value that is larger than 90 seconds.

    Setting Ping Enabled to False stops IIS from checking whether the worker process is still running and keeps the worker process alive until you stop your debugged process. Setting Ping Maximum Response Time to a large value allows IIS to continue monitoring the worker process.

  7. Click OK to close the Advanced Settings dialog box.

  8. Close IIS Manager and the Administrative Tools window.

September 17, 2010

get error number in ms sql Try catch block

Filed under: Uncategorized — pankajmittal @ 7:13 am

ALTER PROC abc
as
    begin
        begin TRY
            DECLARE @a INT
            SET @a = ‘abd’

        END TRY
        BEGIN CATCH

            IF ERROR_NUMBER() IS NULL
                RETURN 0

            PRINT ‘ERROR rethrown from CATCH block: Number:’
                + CONVERT(VARCHAR(10), ERROR_NUMBER()) + ‘ Message:’
                + ISNULL(ERROR_MESSAGE(), ‘-’) + ‘ Procedure: ‘
                + ISNULL(ERROR_PROCEDURE(), ‘-’) + ‘ Line ‘
                + CONVERT(VARCHAR(10), ERROR_LINE())

        END CATCH
    END

August 16, 2010

Delegate live example

Filed under: Uncategorized — pankajmittal @ 5:27 am

Simple example of delegate

I have to 2 controls on a a page and in first control’s action I have to hide second control

Create in first control

public delegate void DelegateCallStatusChange(bool blStatus);

   public event DelegateCallStatusChange OnCallStatusChange;

In first control pass the value to the delegate event

OnCallStatusChange(status);

 

on page Handle this event

 

ucRsvpDetail.OnCallStatusChange += delegate(bool result)
       {
           if (result == true)
               dvAalosagarReference.Attributes.Add("style", "display:block");
           else
               dvAalosagarReference.Attributes.Add("style", "display:none");
       };

 

:)

August 4, 2010

Create table structure in MSSQL from excel file

Filed under: Uncategorized — pankajmittal @ 11:42 am

 

Most of the software developers first create database structure in excel then transfer it into ms-sql. If they use the Excel to mssql.xls file which is attached. Then it save some time.

You can enhance this file if  you do this then please comment so all user got benefit from this. :)

Download from here http://pankajmittal.files.wordpress.com/2010/08/excel-to-mssql.doc

Failed to query a list of database names from the SQL server.

Filed under: Uncategorized — pankajmittal @ 9:53 am

If you find this error while creating membership data then try this on studio command prompt.

 

aspnet_regsql.exe -U <Enter user id here> -P <Enter Password here> -S <Server Instance name> -A mrp -d <Database name>

Ex:

aspnet_regsql.exe -U userid -P pwd -S DWSERVER\SQL2005 -A mrp -d TestDatabase

if you want to use system user id and password then

aspnet_regsql.exe -E -S <Server Instance name> -A mrp -d <Database name>

:)

Next Page »

Theme: Rubric. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.