Click here to Skip to main content
15,901,122 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
data in my database table

HTML
id    int
name  varchar(50)
mode  char(10)


HTML
data in this table like
1   abc   L
2   mnj   C
3   oio   F
4   trt   L
5   lkl   L
6   rer   F


now on page load i get single value for mode
like
HTML
var q=(from a in obj.datas
       where a.id==1
       select a.mode).single();
now i want to compare this value with "L"
then how can i compare it


i used
HTML
if(q=="L")
{
  Response.write("This is for L");
}



but did no compare
how can i compare please give me a proper solution

thank you
Posted

1 solution

Try using q.Trim() == "L". VarChar type adds extra spaces to fill the excess space so trim will remove these and the comparison should then work.

Hope this helps,
Ed
 
Share this answer
 
Comments
PKriyshnA 25-Feb-12 16:39pm    
thanx a lot
its working....
why we need to use trim() ??
Ed Nutting 25-Feb-12 17:08pm    
So in your database you have set the data type to VarChar(50) - what this means is that regardless of what you set the value to, the database will store 50 characters worth of data. Therefore, if you put in just one letter, it fills it with 49 spaces. Using the trim method removes this space from your string that you pull out of the database and so you get just the letter and thus the comparison works. Using varchar(50) for just one letter is poor database design though. I would suggest that: If you know it's only ever going to be one letter then set it to either varchar(1) or if you think it could be longer use nvarchar(50) - nvarchar (with the 'n') means that the database will store as only the characters you set the value to but a maximum of 50 characters. This allows you to remove the Trim method. It also minimises the amount of data the database stores but can reduce efficiency. The best thing you could do would be to use an Int data type and compare integers but this may not fit your design.

Hope this answers your question fully,
Ed

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