Click here to Skip to main content
15,885,956 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All,

I am working on a Windows Form Application where I need to use below StoredProcedure for search voter list.
I have below StoredProcedure

SQL
USE [VMS]
GO
/****** Object:  StoredProcedure [dbo].[GetVotersList]    Script Date: 11/13/2013 16:13:11 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


ALTER procedure [dbo].[GetVotersList]         
@language nvarchar(255),
@firstName nvarchar(255),
@middleName nvarchar(255),
@lastNAme nvarchar(255),
@SrNo int,
@sex nvarchar(10),
@minAge int,
@maxAge int,
@vCardId nvarchar(50),
@address nvarchar(255),
@yadiNo nvarchar(255),
@mLanguage nvarchar(50),
@Religion nvarchar(50),
@Caste nvarchar(50)

as            
begin

if(@language = 'English')
begin
 if(@sex = 'A')
	begin
		Select SrNo as 'Serial #', eLastName + ' '+  eFirstName + ' '+ eMiddleName as 'Name', sex, age, 
		VCardId, GharNo, eAddress as 'Address',  YadiNo, mobileNo as 'Mobile No',
		from Voter
		where eFirstName like @firstName+'%' and emiddleName like @middleName+'%' and 
		eLastName like @lastName+ '%' and age > @minAge and age < @maxAge and
		VCardId like @vCardId +'%' and eAddress like '%'+@address+'%' and yadiNo like @yadiNo +'%' 
		
		--  Get count of Male records
		select count(*) from Voter
		where eFirstName like @firstName+'%' and emiddleName like @middleName+'%' and 
		eLastName like @lastName+ '%' and age > @minAge and age < @maxAge and
		VCardId like @vCardId +'%' and eAddress like '%'+@address+'%' and yadiNo like @yadiNo +'%' 
		and sex='M' 
		     end
 else
  begin
		Select SrNo as 'Serial #', eLastName + ' '+  eFirstName + ' '+ eMiddleName as 'Name', sex, 
		age, VCardId, GharNo, eAddress as 'Address',  YadiNo, mobileNo as 'Mobile No', 
		emailId as 'Email Id'
		from Voter 
		where eFirstName like @firstName+'%' and emiddleName like @middleName+'%' and 
		eLastName like @lastName+ '%' and age > @minAge and age < @maxAge and sex = @sex           and
		VCardId like @vCardId +'%' and eAddress like '%'+@address+'%' and yadiNo like @yadiNo +'%'
		--  Get count of Male records
		select count(*) from Voter 
		where eFirstName like @firstName+'%' and emiddleName like @middleName+'%' and 
		eLastName like @lastName+ '%' and age > @minAge and age < @maxAge and sex = @sex and
		VCardId like @vCardId +'%' and eAddress like '%'+@address+'%' and yadiNo like @yadiNo +'%' 
		and sex='M'
		end 
             end

 else --Language is MARRATHI
  begin
   if(@sex = 'A')
	begin
		Select SrNo as 'Serial #', mLastName + ' '+  mFirstName + ' '+ mMiddleName as 'Name', sex, 
		age, VCardId, GharNo, mAddress as 'Address',  YadiNo, mobileNo as 'Mobile No', 
		emailId as 'Email Id'
		from Voter 
		where eFirstName like @firstName+'%' and emiddleName like @middleName+'%' and 
		eLastName like @lastName+ '%' and age > @minAge and age < @maxAge and
		VCardId like @vCardId +'%' and eAddress like '%'+@address+'%' and yadiNo like @yadiNo +'%'
		---  Get count of Male records
		select count(*) from Voter 
		where eFirstName like @firstName+'%' and emiddleName like @middleName+'%' and 
		eLastName like @lastName+ '%' and age > @minAge and age < @maxAge and
		VCardId like @vCardId +'%' and eAddress like '%'+@address+'%' and yadiNo like @yadiNo +'%' 
		and sex='M'
		end
   else
	begin
		Select SrNo as 'Serial #', mLastName + ' '+  mFirstName + ' '+ mMiddleName as 'Name', sex, age, VCardId, GharNo, mAddress as 'Address',  YadiNo, mobileNo as 'Mobile No', emailId as 'Email Id' from Voter 
		where eFirstName like @firstName+'%' and emiddleName like @middleName+'%' and 
		eLastName like @lastName+ '%' and age > @minAge and age < @maxAge and sex = @sex and
		VCardId like @vCardId +'%' and eAddress like '%'+@address+'%' and yadiNo like @yadiNo +'%' 
		and sex='M'
		
		--  Get count of Male records
		select count(*) from Voter 
		where eFirstName like @firstName+'%' and emiddleName like @middleName+'%' and 
		eLastName like @lastName+ '%' and age > @minAge and age < @maxAge and sex = @sex and
		VCardId like @vCardId +'%' and eAddress like '%'+@address+'%' and yadiNo like @yadiNo +'%' 
		and sex='M'
		end
  end
end


this works fine (gives me list of voter exactly match the search query like sure name, first name etc.)but when I add some more functionality to it gives wrong output. Additional functionality code is below

Firstly I am searching for below values only

@language nvarchar(255),
@firstName nvarchar(255),
@middleName nvarchar(255),
@lastNAme nvarchar(255),
@SrNo int,
@sex nvarchar(10),
@minAge int,
@maxAge int,
@vCardId nvarchar(50),
@address nvarchar(255),
@yadiNo nvarchar(255),

and now I want to add three more values to search they are
@mLanguage nvarchar(50),
@Religion nvarchar(50),
@Caste nvarchar(50)

I have coded as values coded earlier but it gives me only first 2 rows only.

Please advise me what's wrong here.

Sorry for incomplete info. Hope it's little bit clear now.
Thanks in advance.
Posted
Updated 13-Nov-13 2:19am
v3
Comments
ArunRajendra 13-Nov-13 7:51am    
You need to be more specific. Just mentioning not working will not help.
♥…ЯҠ…♥ 13-Nov-13 7:58am    
Do you want us to go through line by line and find out why its throwing error, without even knowing error?
phil.o 13-Nov-13 8:04am    
It's unlikely we are going to compare both pieces of code you provided.
Please just mention what you changed in the second version of your stored procedure.
Please also define 'working properly' (i.e. what is the actual problem?)
[no name] 13-Nov-13 8:21am    
Dear all I have updated the question hope it's ok now

1 solution

I guess, issue is with @Caste in your count queries.

SQL
--  Get count of Male records
		select count(*) from Voter 
		where eFirstName like @firstName+'%' and emiddleName like @middleName+'%' and 
		eLastName like @lastName+ '%' and age > @minAge and age < @maxAge and sex = @sex and
		VCardId like @vCardId +'%' and eAddress like '%'+@address+'%' and yadiNo like @yadiNo +'%' 
		and sex='M'
		and mLanguage like @mLanguage+'%' and eReligion like @Religion+'%'
		and eCaste like @Caste


It should be -

SQL
and eCaste like '%'+@Caste+'%'
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900