Dear all..
I have a table as -
| ID | User | Location | Flag |
| 1 | Ashok | Mumbai | 1 |
| 2 | Reema | Mumbai | 1 |
| 3 | Kesar | Haryana | 1 |
| 4 | Hardik | Kolkata | 1 |
| 5 | Rajat | Kolkata | 1 |
Front-End -
1. CheckListbox - List of users.
2. CheckListbox - List of Locations.
User can select multiple Users or/and Locations.
For null values (eg - if no Location is selected, System.DBValue.Null is passed)
1. If User - Ashok, then only one record must be displayed.
SQL Stored Procedure -
@User varchar(max),
@Location varchar(max)
declare @SQLQuery nvarchar(4000)
declare @Whereclause nvarchar(4000)
set @SQLQuery = 'select ID, User, Location from UserLocationTable where Flag = 1'
if @User is not null
set @Whereclause =' and User in ('+@User+)'
if @Location is not null
set @Whereclause =' and Location in ('+@Location+)'
set @SQLQuery = @SQLQuery + @Whereclause -
Issue - Passing System.DBNull.Value.ToString() from front-end makes the query as -
select ID, User, Location from UserLocationTable where Flag =1 and Location in ('')
and User in ('Ashok')
----------
How to solve this ?? Please guide
Midhun TpRank #388 · 6.5K reputationPosted Dec 26, 2016, 12:05 AM EST
Fabio Silva LimaRank #750 · 3.1K reputationPosted Dec 25, 2016, 11:31 PM EST
IF(LEN(ISNULL(Location, 0)) = 0)
Riddhi ValechaRank #794 · 2.9K reputationPosted Dec 26, 2016, 12:07 AM EST
Manas MohapatraRank #182 · 13.2K reputationPosted Dec 26, 2016, 12:27 AM EST
sudipta sanyalRank #1,945 · 753 reputationPosted Dec 26, 2016, 12:45 AM EST
Riddhi ValechaRank #794 · 2.9K reputationPosted Dec 26, 2016, 1:17 AM EST
Midhun TpRank #388 · 6.5K reputationPosted Dec 26, 2016, 1:30 AM EST
Riddhi ValechaRank #794 · 2.9K reputationPosted Dec 26, 2016, 3:51 AM EST
Midhun TpRank #388 · 6.5K reputationPosted Dec 26, 2016, 6:16 PM EST
Riddhi ValechaRank #794 · 2.9K reputationPosted Dec 27, 2016, 12:30 AM EST
Any idea - what value can I pass from front-end ?
I tried passing -"", String.Empty, null - But none worked..
What value to pass from front-end and what to write in if-condition
so that I can get the desired query -
select id, location, user from table where user in ('Ashok','reema')
The query that I am getting is -
select id, location, user from table where user in ('Ashok','reema')
Please guide..
Midhun TpRank #388 · 6.5K reputationPosted Dec 27, 2016, 12:37 AM EST
Fabio Silva LimaRank #750 · 3.1K reputationPosted Dec 27, 2016, 12:44 AM EST
Riddhi ValechaRank #794 · 2.9K reputationPosted Dec 27, 2016, 2:56 AM EST
Rafnas T PRank #280 · 8.8K reputationPosted Jan 25, 2017, 2:05 AM EST
@Location varchar(max)
declare @SQLQuery nvarchar(4000)
declare @Whereclause nvarchar(4000)
set @SQLQuery = 'select ID, User, Location from UserLocationTable where Flag = 1'
if @User is not null
set @Whereclause =' and User in ('+@User+')'
if @Location is not null
set @Whereclause =' and Location in ('+@Location+')'
set @SQLQuery = @SQLQuery + @Whereclause
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts