String Comparison

Posted by Sibin.rhythm under ASP.NET on 8/26/2009 | Views : 2245 | Status : [Member] | Replies : 4
Hi
I completed my project work . , but only one problem is Sql injection. For this project I used asp.net platform with c#.

I need to compare each letters in the textbox value., if thre has any letters like <,>,.,!,<!,create ,etc..then the the error message should display.
For that i wrote


if(String.Compare(text_compname.text,"<"))
{
lablel1.visible=true; // lbale1=" Invalid Charrecter"
}
else
{
// regular statements
}

When Run the program with this code there is an error like "cannot implicitly convert type'int' to 'bool'

can u find any mistake in this code?????
weather the string comparison syntax is wrong???

weather there has any other coding for string comparison in asp.net with c#??

please help me............




Responses

Posted by: Vuyiswamb on: 8/27/2009 [Member] [MVP] [Administrator] NotApplicable

Up
0
Down
Good Morning Sibin.rhythm

write this Statement like this


If(text_compname.text == "<")
{
lablel1.visible=true; // lbale1=" Invalid Charrecter"
}
else
{
// regular statements
}


if you want more info on SQL Injection look at this Article

http://www.dotnetfunda.com/articles/article295.aspx


One more thing Sibin.rhythm

When you post a code in the Forum, there is a link that says code. This will Format everything in good way.

Thank you for posting at Dotnetfunda

Vuyiswa Maseko

Thank you for posting at Dotnetfunda
[Administrator]

Sibin.rhythm, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sibin.rhythm on: 8/27/2009 [Member] Starter

Up
0
Down
Thankyou Mr.Vuyiswamb

But I need to check the value in the textbox charrecter by charrecter. If "<" symbol any where in the word should detect..if it is the first letter or last letter,or in the middles.So I need to check the Textbox value by each charrecter.. Plese help me...........

Sibin.rhythm, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vuyiswamb on: 8/27/2009 [Member] [MVP] [Administrator] NotApplicable

Up
0
Down
Good Day

That can be easily done this was

String Test = "VuytiswaMaseko<";

if (Test.Contains("<"))
{
Response.Write("<script>alert('It has the Character');</script>");
}
else
{
Response.Write("<script>alert('Oops naa its not there');</script>");
}


now that means your code will look like this

String text_compname = text_compname.text;


if(text_compname.Contains("<"))
{
lablel1.visible=true; // lbale1=" Invalid Charrecter"
}
else
{
// regular statements
}



Thank you for posting at Dotnetfunda

Vuyiswa Maseko



Thank you for posting at Dotnetfunda
[Administrator]

Sibin.rhythm, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Lakhangarg on: 8/27/2009 [Member] [Moderator] Silver

Up
0
Down
Hi Sibin-

Check this JavaScript Function to Check special charcters:

function chkSpecialChars(input)

{
var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?";
for (var i = 0; i < input.length; i++)
{
if (iChars.indexOf(input.charAt(i)) != -1)
{
alert (
"You are using special characters. \nThese are not allowed.\n Please remove them and try again."); return false;
}
}
return true;
}



Replace This line var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?"; with your character that you want to validate.

input is document.getElementId('yourtextboxid');

Thanks & Regards
Lakhan Pal Garg
http://lakhangarg.blogspot.com

Sibin.rhythm, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response