Click here to Skip to main content
15,885,214 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi Team,

somewhere i am getting confused.
i have three column as follows:
Month
Tech
Circle
according to this column i need to fetch the data.
In Month Column, Data is :Jan,feb.March... and so on.
In Tech Column, Data is : Gsmnqi,Gsmboi ... and so on.
In Circle Column, Data is :Ap,Kol,Mumbai..and so on ...

I want to prove four condition as follows:
1 st condition
if i select the month it will fetch the data related to month ,Tech and circle will not be selected.
2 st condition
if i select the month and Tech it will fetch the data related to month and Tech ,circle will not be selected.
3 st condition
if i select the month and circle it will fetch the data related to month and circle ,Tech will not be selected.
4 st condition
if i select month,Tech and Circle it will fetch the data related to Month,tech and Circle.

But my if condition is not working ,seriously i get confused in if ,else and else if for this four condition.

SQL
if (nqiSqiEntity.Month != string.Empty)
                {
                    query.AppendLine("select * from K2_NQISQI with (nolock) where MONTH = '" + nqiSqiEntity.Month + "'  order by id asc");
                }
                else if (nqiSqiEntity.Month != string.Empty && nqiSqiEntity.Tech != string.Empty)
                {
                    query.AppendLine("select * from K2_NQISQI with (nolock) where MONTH = '" + nqiSqiEntity.Month + "' and TECH = '" + nqiSqiEntity.Tech + "' order by id asc");
                }
                else if (nqiSqiEntity.Month != string.Empty && nqiSqiEntity.Circle != string.Empty)
                {
                    query.AppendLine("select * from K2_NQISQI with (nolock) where MONTH = '" + nqiSqiEntity.Month + "' and CIRCLE = '" + nqiSqiEntity.Circle + "' order by id asc");
                }
                else
                {
                    query.AppendLine("select * from K2_NQISQI with (nolock) where MONTH = '" + nqiSqiEntity.Month + "' and CIRCLE = '" + nqiSqiEntity.Circle + "' and TECH '" + nqiSqiEntity.Tech + "' order by id asc");
                }


In condition, Instead of string.Empty i need to put the value or i need to check if the value is present then only condition should get executed.if i check nqiSqiEntity.Month >0 it gives me error Error 45 Operator '>' cannot be applied to operands of type 'string' and 'int'.

Please help me from this situation.


Thanks in advance
Harshal
Posted

Month is a string. '>' operator is an arithmetic operator. You can't do arithmetic on strings.

Simplest solution is to convert your string to an integer, and then you can. Use int.Parse(nqiSqiEntity.Month).

Also, if your struggling with primitive types, its probably too early to be getting involved with constructs such as with (nolock)!
 
Share this answer
 
Comments
R Harshal 18-Jul-14 8:39am    
Hi Rob .
will you guide me for my if else ,else if condition.

Thanks in advance
Harshal
R Harshal 18-Jul-14 8:45am    
if else condition are not working .
Please help.. help..
Just Use this below Query Instead of that Code
Its Simple and much fast :)


SQL
declare @month nvarchar(max),@Tech nvarchar(max),@circle Nvarchar(max),@sql nvarchar(max),@orderby nvarchar(max)
set @month=''--pass parameter as varchar
set @Tech='' --pass parameter as varchar
set @circle='' --pass parameter as varchar
set @sql='select * from K2_NQISQI with (nolock) where 1=1'
set @orderby='order by id asc'
if @month!=''
set @sql=@sql+' and  MONTH ='''+@month+''''
if @Tech!=''
set @sql=@sql+' and TECH ='''+@Tech+''''
if @circle!=''
set @sql=@sql+' and circle ='''+@circle+''''
set @sql=@sql+''+@orderby;
exec sp_executesql @sql
 
Share this answer
 
Comments
R Harshal 18-Jul-14 9:07am    
Oh my God ,
the King_Fisher (Guinness of Code project ).Thanks for your reply Sir, But According to the company requirement i don't want to use stored procedure.So will you please help me in my C# If Else and Else If condition .It will be great thankful to me.

Thanks in Advance
Harshal
King Fisher 18-Jul-14 9:27am    
i am not recommended you for use Sp .
R Harshal 18-Jul-14 9:52am    
No Issue Sir,But is there any luck to improve my If Else ,Else If Condition .So that my confusion will get cleared.

Thanks in Advance
harshal
R Harshal 18-Jul-14 10:43am    
Hi King_Fisher ,I got the Solution on This .
if (nqiSqiEntity.Month != string.Empty)
{
query.AppendFormat("select * from K2_NQISQI with (nolock) where MONTH = '{0}'", nqiSqiEntity.Month);
if (nqiSqiEntity.Tech != string.Empty || nqiSqiEntity.Tech != "Select")
{
query.AppendFormat("' and TECH = '{0}'", nqiSqiEntity.Tech);
}

if (nqiSqiEntity.Circle != string.Empty || nqiSqiEntity.Circle != "Select")
{
query.AppendFormat("' and CIRCLE = '{0}'", nqiSqiEntity.Circle);
}

query.AppendLine(" order by id asc");
}
ITS WORKING SIR bUT THERE IS A TWIST IN MY CONDITION.sometime i am getting nqiSqiEntity.Circle = "Select" ,if this include it should not satisfy the Circle Condition.I wrote this code if (nqiSqiEntity.Circle != string.Empty || nqiSqiEntity.Circle !="Select") dont read the Circle Value ,But its reading the Circle Value .Please Guide me Where I am getting Wrong.
Thanks Harshal
R Harshal 18-Jul-14 10:54am    
Please Someone Guide ME.
Thanks Harshal

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