Jan 31, 2011

STORED PROCEDURES


Stored procedures- an important aspect for the developers in SQL
stored procedure is nothing more than prepared SQL code that you save so you can reuse the code over and over again.  So if you think about a query that you write over and over again, instead of having to write that query each time you would save it as a stored procedure and then just call the stored procedure to execute the SQL code that you saved as part of the stored procedure.

In addition to running the same SQL code over and over again you also have the ability to pass parameters to the stored procedure, so depending on what the need is the stored procedure can act accordingly based on the parameter values that were passed.

There are various options to create a strored procedure. 
First we will learn to create to a simple stored procedure

Before you create a stored procedure you need to know what your end result is, whether you are selecting data, inserting data, etc.. 
In this simple example we will just select all data from the Person.Address table that is stored in the AdventureWorks database.

So the simple T-SQL code would be as follows which will return all rows from this table.
SELECT * FROM AdventureWorks.Person.Address

To create stored procedure to do this the code would look like this:
CREATE PROCEDURE uspGetAddress
AS
SELECT * FROM AdventureWorks.Person.Address
GO

To call the procedure to return the contents from the table specified, the code would be:
EXEC uspGetAddress
--or just simply
uspGetAddress


How to create a SQL Server stored procedure with parameters


OverviewThe real power of stored procedures is the ability to pass parameters and have the stored procedure handle the differing requests that are made.  In this topic we will look at passing parameter values to a stored procedure.
ExplanationJust like you have the ability to use parameters with your SQL code you can also setup your stored procedures to except one or more parameter values.
One Parameter
In this example we will query the Person.Address table from the AdventureWorks database, but instead of getting back all records we will limit it to just a particular city.  This example assumes there will be an exact match on the City value that is passed.
CREATE PROCEDURE uspGetAddress @City nvarchar(30)
AS
SELECT * 
FROM AdventureWorks.Person.Address
WHERE City = @City
GO
To call this stored procedure we would execute it as follows:
EXEC uspGetAddress @City = 'New York'
We can also do the same thing, but allow the users to give us a starting point to search the data.  Here we can change the "=" to a LIKE and use the "%" wildcard.
CREATE PROCEDURE uspGetAddress @City nvarchar(30) 
AS 
SELECT * 
FROM AdventureWorks.Person.Address 
WHERE City LIKE @City + '%' 
GO
In both of the proceeding examples it assumes that a parameter value will always be passed. If you try to execute the procedure without passing a parameter value you will get an error message such as the following:
Msg 201, Level 16, State 4, Procedure uspGetAddress, Line 0
Procedure or function 'uspGetAddress' expects parameter '@City', which was not supplied.
Default Parameter Values
In most cases it is always a good practice to pass in all parameter values, but sometimes it is not possible.  So in this example we use the NULL option to allow you to not pass in a parameter value.  If we create and run this stored procedure as is it will not return any data, because it is looking for any City values that equal NULL.
CREATE PROCEDURE uspGetAddress @City nvarchar(30) = NULL
AS
SELECT *
FROM AdventureWorks.Person.Address
WHERE City = @City
GO
We could change this stored procedure and use the ISNULL function to get around this.  So if a value is passed it will use the value to narrow the result set and if a value is not passed it will return all records.
CREATE PROCEDURE uspGetAddress @City nvarchar(30) = NULL
AS
SELECT *
FROM AdventureWorks.Person.Address
WHERE City = ISNULL(@City,City)
GO
Multiple Parameters
Setting up multiple parameters is very easy to do.  You just need to list each parameter and the data type separated by a comma as shown below.
CREATE PROCEDURE uspGetAddress @City nvarchar(30) = NULL, @AddressLine1 nvarchar(60) = NULL
AS
SELECT *
FROM AdventureWorks.Person.Address
WHERE City = ISNULL(@City,City)
AND AddressLine1 LIKE '%' + ISNULL(@AddressLine1 ,AddressLine1) + '%'
GO
To execute this you could do any of the following:
EXEC uspGetAddress @City = 'Calgary'
--or
EXEC uspGetAddress @City = 'Calgary', @AddressLine1 = 'A'
--or
EXEC uspGetAddress @AddressLine1 = 'Acardia'
-- etc...

Returning stored procedure parameter values to a calling stored procedure
(OUTPUT)



Overview
In a previous topic we discussed how to pass parameters into a stored procedure, but another option is to pass parameter values back out from a stored procedure.  One option for this may be that you call another stored procedure that does not return any data, but returns parameter values to be used by the calling stored procedure.
Explanation
Setting up output paramters for a stored procedure is basically the same as setting up input parameters, the only difference is that you use the OUTPUT clause after the parameter name to specify that it should return a value.  The output clause can be specified by either using the keyword "OUTPUT" or just "OUT".
Simple Output
CREATE PROCEDURE uspGetAddressCount @City nvarchar(30), @AddressCount int OUTPUT
AS
SELECT @AddressCount = count(*) 
FROM AdventureWorks.Person.Address 
WHERE City = @City
Or it can be done this way:
CREATE PROCEDURE uspGetAddressCount @City nvarchar(30), @AddressCount int OUT
AS
SELECT @AddressCount = count(*) 
FROM AdventureWorks.Person.Address 
WHERE City = @City
To call this stored procedure we would execute it as follows.  First we are going to declare a variable, execute the stored procedure and then select the returned valued.
DECLARE @AddressCount int
EXEC uspGetAddressCount @City = 'Calgary', @AddressCount = @AddressCount OUTPUT
SELECT @AddressCount
This can also be done as follows, where the stored procedure parameter names are not passed.
DECLARE @AddressCount int
EXEC uspGetAddressCount 'Calgary', @AddressCount OUTPUT
SELECT @AddressCount



But as now you mastered the stored procedures 

we will see how best can be a stored procedure used...The best way of writing a stored procedure  is described below.
USE [DATABASE NAME]
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO

IF EXISTS ( SELECT name FROM SYSOBJECTS WHERE type = 'P' AND name = 'storedprocedurename')
DROP PROCEDURE dbo.[storedprocedurename]
GO

CREATE PROCEDURE [dbo].[storedprocedurename] 
  -- Adding parameters for the stored procedure
  @parameter1   nvarchar(50)  
AS


/******************************************************************************
**    File: storedprocedurename.sql
**    Name: storedprocedurename
**    Desc: SP to get a new record in storedprocedurename
**          If a record alredy exists for the given parameter then return the existing record.
**    NOTE: 
**            
**
**              
**    Return values:
** 
**    Called by:   
**              
**    Parameters:
**    Input                                 Description
**    ----------                            -----------
**  @parameter1     parameter description
**  
**
**    Author: Sandeep kumar
**    Date:   31-01-2011
*******************************************************************************
**    Change History
*******************************************************************************
**    Date:       Author:                       Description:
**    --------    --------                      -----------------------------
**
**
*******************************************************************************/

BEGIN
 -- SET NOCOUNT ON added to prevent extra result sets from
 -- interfering with SELECT statements.
  SET NOCOUNT ON; 
   SELECT columnname1,
columnname2,
  columnname3 
FROM  tablename
  WHERE columnname = @parameter1
 
END




By now you should have mastered the usage of stored procedure.
CHEERS...
THESANDEEP
WWW.THESANDEEP.COM

Jan 25, 2011

Calling an exe from another application and passing an argument to it .Net

Calling an exe from another application and passing an argument to it .Net
Calling an exe from another application is always easy in .net.
You need to just add System.diagnostics namespace to your project and choose the properties.
Properties
ProcessStartInfo
Stores information about the process.
FileName
The program or filename you want to run. It can be a file such as "example.txt". It can be a program such as "WINWORD.EXE".
Arguments
Stores the arguments, such as -flags or filename.
CreateNoWindow Allows you to run a command line program silently. It does not flash a console window.
WindowStyle Use this to set windows as hidden.
ProcessWindowStyle.Hidden used often.
UserName WorkingDirectory
Domain These control OS-specific parameters.
For more complex situations, where Windows features are used.
Examples
using System.Diagnostics;
class Program
{
static void Main()
{
// Open the file "example.txt" that is in the same directory as
// your .exe file you are running.
// To call any other exe you need to just specify the path of the exe.
Process.Start("example.txt");
}
}
This example is to call an exe and pass the arguments.
using System.Diagnostics;
class Program
{
static void Main()
{
// A.
// Open specified Word file.
OpenMicrosoftWord(@"C:\Users\Sam\Documents\Gears.docx");
}
///
/// Open specified word document.
///
static void OpenMicrosoftWord(string f)
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "WINWORD.EXE";
startInfo.Arguments = f;
Process.Start(startInfo);
}
}
  

Jan 24, 2011

Exception: Failed to load the .Net framework / .net Application working fine in one system but not working in other system.


Failed to load the .Net framework


.net Application working fine in one system but not working in other system.

This exception occurs at times because of the mismatch path to the GAC [Global assembly cache] So failure to load the dlls in the GAC happens leading to an exception.
TO resolve the issue
You need to set the framework path in environment variables path as mentioned below, go to MyComputer-Properties--;Advanced-Environment Variables

In this click new button under system variables
and add the path of .net framework
example
C:\Program Files\Microsoft.NET\v3.5

Hope it helped u...
Cheers!!!!

WPF application getting struck in windows7/Xp




WPF application getting struck in windows 7/Xp
In windows you have a service that runs in background that stores the font that are used. So at times due to the overlap of the fonts and over storage of the same in Fontcache 3.0.0.0.dat file it leads in the balnk screen when you run the exe.
To resolve the issue follow the steps given below.
  1. Exit all WPF-based applications that are running on the computer.
  2. Stop the Windows Presentation Foundation Font Cache 3.0.0.0 service. To stop the Windows Presentation Foundation Font Cache 3.0.0.0 service, use one of the following methods.

    Method 1: Use Microsoft Management Console
    1. Click Start, click Run, type Services.msc, and then click OK.
    2. Right-click Windows Presentation Foundation Font Cache 3.0.0.0, and then click Stop.
    Method 2: Use the command prompt
    1. Click Start, click Run, type cmd, and then click OK.
    2. At the command prompt, type net stop "Windows Presentation Foundation Font Cache 3.0.0.0", and then press ENTER.
  3. Delete the old Fontcache3.0.0.0.dat file.

    Note By default, the Fontcache3.0.0.0.dat file is located in the %windir%\ServiceProfiles\LocalService\AppData\Local folder in Windows Vista. By default, the Fontcache3.0.0.0.dat file is located in the %systemdrive%\Documents and Settings\LocalService\Local Settings\Application Data folder in Windows XP and in earlier versions of Windows.
Hope this helped u....
CHEERS!!!!!


SANDEEP

Wcf application working fine in Xp but not working in windows7


Wcf application working fine in Xp but not working in windows7
In xp you not need to register your url [end point address] but in windows7 you need to register the url for someone to access the endpoint and its service.
To register the url you need to follow the steps mentioned below.
Step1: Run the command prompt as a administrator
Step2: Add ur url using the netsh tool
Example
In command prompt type this
Run command prompt as an administrator--> type this,
netsh http add urlacl url=http://Sandeep:8000/WcfService user=username

here the http://Sandeep:8000/WcfService is my end point address and username should be given the username of the system or the computer name if the username of the system does not work.

Hope that it helped U
CHEERS!!!!!

Apr 6, 2008

jus put in ur head !



Word of the Day






Dec 6, 2007

Study Abroad

Study Abroad for Grad Students
The National Science Foundation offers an opportunity for summer study abroad for graduate students in biological sciences, computer and information science and technology, STEM education, engineering, geosciences, math, plus social, behavioral or economic sciences. Apply by Dec. 12. See our calendar page for details.
Study Abroad is for Everybody!
Can you go abroad if you are in a technical major? If you don't speak any foreign languages? If you don't have a lot of extra money? Yes, you can!
Our office is dedicated to helping as many Purdue students as possible to have overseas experiences. We help you to overcome academic, financial, or personal concerns that might prevent you from going abroad and we're especially devoted to removing obstacles for first-time travelers. Begin your adventure today by exploring our searchable list of programs.
There will also be a series of Information Sessions for specific programs early this semester. For a list of scheduled information meetings, consult the Study Abroad calendar.
Passports required!
In the past, students could apply for Study Abroad, and then wait until finding out if they had been accepted before applying for a passport. This is no longer possible.
You now need to show the Study Abroad Office proof that you have a passport, or that you have applied for one, at the time you apply for one of our programs.
(* NOTE: This requirement is waived if you are applying for an International Programs In Agriculture [IPIA] program.)
Students who are even thinking of going abroad are strongly encouraged to apply for a passport as soon as possible!