Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hello everyone, thanks for the help on my last problem.

 

My problem is this. When I pass my variables to the sql server, one of the strings has a chunk of text and if the user puts a ' in the string it makes my program crash. So lets say the user enters for my string variable reason = "Didn't make it on time" the ' in didn't would crash my program. Here is my code.

 

commsql.CommandText = "INSERT INTO TIMELOG ([uSERNAME],[LOGTIME],[REASON]) VALUES ('" + username + "','" + loginTime + "','" + reason + "')"

 

Anyone have any ideas on that one?

 

Thanks much

 

LostProgrammer

  • Moderators
Posted

Replace all single quotes with 2 single quotes.

commsql.CommandText = "INSERT INTO TIMELOG ([uSERNAME],[LOGTIME],[REASON]) VALUES ('" & username.Replace("'","''") & "','" & loginTime & "','" & reason & "')"

Visit...Bassic Software
  • *Experts*
Posted

Another reason to use Stored Procs when possible - you don't have to worry about this (unless you're passing your parameters through a full-string, such as "exec procA 'param1', 123, 'param3'".

 

Want to see something fun? Try this, set your variable reason to:

"my reason') DROP TABLE TIMELOG --"

If you look at the string you'll be building, you'll get:

INTO TIMELOG ([uSERNAME],[LOGTIME],[REASON]) VALUES ('dan', '1:52PM', 'my reason') DROP TABLE TIMELOG -- )

 

This is NOT something you want a malicious user to be able to do. Watch your single quotes - could be more than just a syntax error :)

 

-nerseuse

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...