473,406 Members | 2,710 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Insert text contain code

Hello !

how can i insert text containg code examples from a textbox into a database
using SQL insert statment.

i have no problem to just add text that dont contains code and script
examples or the illegal chars for the insert command

is it possible to encasulate the text/string so the server doesnt reads the
string as a command?

i guess this is something with sql injections and regular expressions

Thanx
Nov 19 '05 #1
6 2045
well you wanna escape your string quotes so it doesn't get
misinterpretted. everything else should be ok, just make sure you
escape your string quotes.

INSERT INTO table_name VALUES (1, "string x = \"test\";")

Nov 19 '05 #2
Yes i understand that and i can insert som code but for example i can not
insert this

- text in the textbox that contains the data to insert
If e.Item.ItemType <> ListItemType.Header And e.Item.ItemType <>
ListItemType.Footer Then
e.Item.Cells(2).Attributes("onclick") = "javascript:return
confirm('Are you sure u wanna delete? \n " & _
DataBinder.Eval(e.Item.DataItem, "Subject") & "')"
End If

because the ' signs the command gets misiterpreted, i could make a replace
( as i do when i wanna make sure this signs dont exist in my SQL statements
injection problem) but then i cant copy and pase the code at later time so
in this particular case it would be nice to use the illegal chars...

"ech0" <th******@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
well you wanna escape your string quotes so it doesn't get
misinterpretted. everything else should be ok, just make sure you
escape your string quotes.

INSERT INTO table_name VALUES (1, "string x = \"test\";")

Nov 19 '05 #3
Hello Lelle,

Why dont you use parameters? This would handle any issues around sql injection.

--
Matt Berther
http://www.mattberther.com
Yes i understand that and i can insert som code but for example i can
not insert this

- text in the textbox that contains the data to insert
If e.Item.ItemType <> ListItemType.Header And e.Item.ItemType <>
ListItemType.Footer Then
e.Item.Cells(2).Attributes("onclick") = "javascript:return
confirm('Are you sure u wanna delete? \n " & _
DataBinder.Eval(e.Item.DataItem, "Subject") & "')"
End If
because the ' signs the command gets misiterpreted, i could make a
replace ( as i do when i wanna make sure this signs dont exist in my
SQL statements injection problem) but then i cant copy and pase the
code at later time so in this particular case it would be nice to use
the illegal chars...

"ech0" <th******@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
well you wanna escape your string quotes so it doesn't get
misinterpretted. everything else should be ok, just make sure you
escape your string quotes.

INSERT INTO table_name VALUES (1, "string x = \"test\";")


Nov 19 '05 #4
Matt is right - parameters is the solution. But if it's not possible,
encrypt the text before sending to db and decrypt it before displaying/using
when requesting it from db.
Regards,
Kikoz.

"Matt Berther" <mb******@hotmail.com> wrote in message
news:22***************************@news.microsoft. com...
Hello Lelle,

Why dont you use parameters? This would handle any issues around sql
injection.

--
Matt Berther
http://www.mattberther.com
Yes i understand that and i can insert som code but for example i can
not insert this

- text in the textbox that contains the data to insert
If e.Item.ItemType <> ListItemType.Header And e.Item.ItemType <>
ListItemType.Footer Then
e.Item.Cells(2).Attributes("onclick") = "javascript:return
confirm('Are you sure u wanna delete? \n " & _
DataBinder.Eval(e.Item.DataItem, "Subject") & "')"
End If
because the ' signs the command gets misiterpreted, i could make a
replace ( as i do when i wanna make sure this signs dont exist in my
SQL statements injection problem) but then i cant copy and pase the
code at later time so in this particular case it would be nice to use
the illegal chars...

"ech0" <th******@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
well you wanna escape your string quotes so it doesn't get
misinterpretted. everything else should be ok, just make sure you
escape your string quotes.

INSERT INTO table_name VALUES (1, "string x = \"test\";")


Nov 19 '05 #5
thanx guys ... im kinda newbie to this and im not sure what you mean with
parameters. Can u give me an example?

Dim Solution textbox1.text as string and then i set in my sql string
like this

Me.DL.InsertData("INSERT INTO data (subject omrade, solution, kategori)
VALUES ('" & Me.tbsubject.Text & "','" & Me.ddlOmrade.SelectedItem.Text &
"','" & solution & "','" & Me.ddlKat1.SelectedItem.Text & "')")

This works good for oridinary text
"Kikoz" <ki***@hotmail.com> wrote in message
news:u3**************@TK2MSFTNGP12.phx.gbl...
Matt is right - parameters is the solution. But if it's not possible,
encrypt the text before sending to db and decrypt it before
displaying/using when requesting it from db.
Regards,
Kikoz.

"Matt Berther" <mb******@hotmail.com> wrote in message
news:22***************************@news.microsoft. com...
Hello Lelle,

Why dont you use parameters? This would handle any issues around sql
injection.

--
Matt Berther
http://www.mattberther.com
Yes i understand that and i can insert som code but for example i can
not insert this

- text in the textbox that contains the data to insert
If e.Item.ItemType <> ListItemType.Header And e.Item.ItemType <>
ListItemType.Footer Then
e.Item.Cells(2).Attributes("onclick") = "javascript:return
confirm('Are you sure u wanna delete? \n " & _
DataBinder.Eval(e.Item.DataItem, "Subject") & "')"
End If
because the ' signs the command gets misiterpreted, i could make a
replace ( as i do when i wanna make sure this signs dont exist in my
SQL statements injection problem) but then i cant copy and pase the
code at later time so in this particular case it would be nice to use
the illegal chars...

"ech0" <th******@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...

well you wanna escape your string quotes so it doesn't get
misinterpretted. everything else should be ok, just make sure you
escape your string quotes.

INSERT INTO table_name VALUES (1, "string x = \"test\";")



Nov 19 '05 #6
Lelle. You're using so called "dynamic sql" which you construct right from
your code. Although it works just fine for small projects, this approach has
several issues, from security to performance to manageability. By saying
"parameters" we mean using stored procedures or parameterized statements. In
short, the process is fairly simple:

1. Create a stored procedure that would select whatever data your page
needs; most definitely it would have some parameters (say, if you select
some orders for specific dates, then parameters would be those dates and
customerID); check out SqlServer's Help to learn all about them, if you need
to (in Query Analyzer click Help / Transact-SQL Help option);
2. You use DataAdapter to fill a dataset; Microsoft has released a good
collection of classes called "Application Blocks"; it's free, just search
msdn.microsoft.com to find and download it; it includes documentation, too,
I believe;
3. Or use your own class; it's also not difficult to create one; the best
thing would be to compile such classes as a separate project, so you can
reuse it across multiple applications. Below is the sample of data access
method of such class (sorry, I use C# :)):

public static DataSet ExecSPDataSet(SqlConnection conn,int
commandTimeout,string procName,params object[] arrayOfParams)

{

try

{

SqlCommand comm = new SqlCommand(procName,conn); // create new Command
object

comm.CommandType = CommandType.StoredProcedure; // tell the code that you
gonna use stored procedure

SqlCommandBuilder.DeriveParameters(comm); //go to the Sql Server to get
names and data types of sproc parameters

comm.CommandTimeout = commandTimeout; // set timeout, useful if you have a
large chuncks of data to be returned

for( int i=1; i < comm.Parameters.Count && i < arrayOfParams.Length+1; i++ )

comm.Parameters[i].Value = arrayOfParams[i-1]; // "transform" your array of
parameters into Command.Parameters object

SqlDataAdapter da = new SqlDataAdapter(comm); // create a new instance of
SqlDataAdapter object

DataSet ds = new DataSet(); // ...and new dataset which will contain your
data, too

da.Fill(ds); // physically go to get your data and fill dataset

comm.Parameters.Clear(); // just in case if you'll reuse this Command again

if(conn.State == ConnectionState.Open)

conn.Close(); // no matter what books say about pooling and garbage
collection, always close your connection

return ds; // enjoy your data

}

catch(SqlException ex)

{

if(ex.Number == 1205) //check for deadlocks, it would be MUCH better to
start a timer for the random number of seconds, so other requests would not
cause an infinitive loop

return ExecSPDataSet(conn,commandTimeout,procName,arrayOf Params); // call
this method again if deadlock happened

else throw; // something else happened - let the rest of the code know about
it

}

}

As you can see, this method calls stored procedure and expects an array of
objects, which are your parameters (key word "params" has nothing to do with
sql, it tells C# that those methods are not required).

Hope this helps a bit :)
Kikoz.

"Lelle" <sv****@hotmail.com> wrote in message
news:42********@news.wineasy.se...
thanx guys ... im kinda newbie to this and im not sure what you mean with
parameters. Can u give me an example?

Dim Solution textbox1.text as string and then i set in my sql string
like this

Me.DL.InsertData("INSERT INTO data (subject omrade, solution, kategori)
VALUES ('" & Me.tbsubject.Text & "','" & Me.ddlOmrade.SelectedItem.Text &
"','" & solution & "','" & Me.ddlKat1.SelectedItem.Text & "')")

This works good for oridinary text
"Kikoz" <ki***@hotmail.com> wrote in message
news:u3**************@TK2MSFTNGP12.phx.gbl...
Matt is right - parameters is the solution. But if it's not possible,
encrypt the text before sending to db and decrypt it before
displaying/using when requesting it from db.
Regards,
Kikoz.

"Matt Berther" <mb******@hotmail.com> wrote in message
news:22***************************@news.microsoft. com...
Hello Lelle,

Why dont you use parameters? This would handle any issues around sql
injection.

--
Matt Berther
http://www.mattberther.com

Yes i understand that and i can insert som code but for example i can
not insert this

- text in the textbox that contains the data to insert
If e.Item.ItemType <> ListItemType.Header And e.Item.ItemType <>
ListItemType.Footer Then
e.Item.Cells(2).Attributes("onclick") = "javascript:return
confirm('Are you sure u wanna delete? \n " & _
DataBinder.Eval(e.Item.DataItem, "Subject") & "')"
End If
because the ' signs the command gets misiterpreted, i could make a
replace ( as i do when i wanna make sure this signs dont exist in my
SQL statements injection problem) but then i cant copy and pase the
code at later time so in this particular case it would be nice to use
the illegal chars...

"ech0" <th******@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...

> well you wanna escape your string quotes so it doesn't get
> misinterpretted. everything else should be ok, just make sure you
> escape your string quotes.
>
> INSERT INTO table_name VALUES (1, "string x = \"test\";")
>



Nov 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: PhilM | last post by:
Kindly break out the 4x2 and bring me to my senses. I seem to have gotten myself totally confused with php.net, mysql.org pages on dates etc, as well as groups.google. Could someone explain,...
3
by: jason | last post by:
How does one loop through the contents of a form complicated by dynamic construction of checkboxes which are assigned a 'model' and 'listingID' to the NAME field on the fly in this syntax:...
24
by: deko | last post by:
I'm trying to log error messages and sometimes (no telling when or where) the message contains a string with double quotes. Is there a way get the query to insert the string with the double...
3
by: Hai Nguyen | last post by:
Hi all I was attempting to insert multiple row by using a loop into a database.A table has 2 primary keys and one regular field (PR) (PR) ID Project Ans 1 2 a 1 ...
20
by: Guadala Harry | last post by:
In an ASCX, I have a Literal control into which I inject a at runtime. litInjectedContent.Text = dataClass.GetHTMLSnippetFromDB(someID); This works great as long as the contains just...
11
by: sm | last post by:
Hi All, Can anybody give me the syntax to insert a record into SQL server through VB code using variables? The following statement is failing! sInsertQuery = "INSERT INTO TestTab (Col1, Col2,...
2
by: mirandacascade | last post by:
O/S: Win2K Vsn of Python: 2.4 Example: <a> <b createAnotherWhenCondition="x"> <c>text for c</c> <d>text for d</d> </b>
0
by: rshivaraman | last post by:
BULK INSERT bill_tbl FROM 'd:\ftp_Data\in\baddress.dat' WITH ( FIELDTERMINATOR = ';', ROWTERMINATOR = '\n' ) --------------------------------- This is the query used to populate bill_tbl....
24
by: Henry J. | last post by:
My app needs to insert thousand value rows into a mostly empty table (data are read from a file). I can either use inserts, or use merge. The advantage of using merge is that in the few cases...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.