p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


Go Back   p2p.wrox.com Forums > C# and C > C# 2005 > C# 2005
I forgot my password Register Now
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
C# 2005 For discussion of Visual C# 2005.

Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 section of the Wrox p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.

Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old October 18th, 2009, 03:39 PM
Authorized User
Points: 56, Level: 1
Points: 56, Level: 1 Points: 56, Level: 1 Points: 56, Level: 1
Activity: 15%
Activity: 15% Activity: 15% Activity: 15%
 
Join Date: Jul 2009
Posts: 13
Thanks: 0
Thanked 1 Time in 1 Post
Default SQL-Injection and multiple parameters when iterating a ListBox for example

Hello everybody,

I deceided to use SQL-Injection as I heard of the security issues that arise.

My problem is that I want to select multiple items of an ListBox and use those as paramaters in a SQL INSERT statement. The thing is, that you can use a paramameter-name only one time in a select statement. I tricked the thing creating a 'new' command object everytime I retrieve another item of the ListBox.

This is not the finest method, I guess. Does anybody know how to solve this issue?

My code looks like this:
...
currentMessageId = ....
int id = 0;

for ( i = 0; i< listBoxUsers.SelectedItems.Count; i+)
{
sqlStr = "";
row = ((DataRowView)this.listBoxUsers.SelectedItems[i]).Row;
id = Convert.ToInt16(row[listBoxUsers.ValueMember]);
sqlStr = "INSERT INTO user_messages (M_Id, User_Id) VALUES (@messageId, @userId)";

command = new SqlCommand (sqlStr, sqlConnectionString);
command.Parameters.AddWithValue("messageId", currentMessageId);
command.Parameters.AddWithValue("userId", id);

command.CommandText = sqlStr;
command.ExecuteNonQuery();
}

is there a way to do an successful insert to the n:m related table without creating a new instance of the command class every time?

Best regards
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #2 (permalink)  
Old October 19th, 2009, 05:43 AM
samjudson's Avatar
Friend of Wrox
Points: 4,453, Level: 28
Points: 4,453, Level: 28 Points: 4,453, Level: 28 Points: 4,453, Level: 28
Activity: 60%
Activity: 60% Activity: 60% Activity: 60%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 1,359
Thanks: 0
Thanked 31 Times in 31 Posts
Default

Firstly, you are not 'using SQL Injection' but avoiding it...

If you create the SqlCommand outside of your loop, and then use SqlCommand.Parameter.Add to create a new SqlParameter object, then simply set its Value and execute the command inside the loop.
__________________
/- Sam Judson : Wrox Technical Editor -/
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #3 (permalink)  
Old October 19th, 2009, 06:23 AM
Authorized User
Points: 56, Level: 1
Points: 56, Level: 1 Points: 56, Level: 1 Points: 56, Level: 1
Activity: 15%
Activity: 15% Activity: 15% Activity: 15%
 
Join Date: Jul 2009
Posts: 13
Thanks: 0
Thanked 1 Time in 1 Post
Default

Ok,

It worked.

I placed the command object outside the loop and used the clear method of the command.parameters object to clear the variable-name.

Thank you.

Last edited by 4thhorseman : October 19th, 2009 at 06:29 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #4 (permalink)  
Old October 19th, 2009, 06:25 AM
Friend of Wrox
Points: 3,152, Level: 23
Points: 3,152, Level: 23 Points: 3,152, Level: 23 Points: 3,152, Level: 23
Activity: 20%
Activity: 20% Activity: 20% Activity: 20%
 
Join Date: Jun 2003
Location: Tehran, Iran
Posts: 922
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via Yahoo to melvik
Default

or u can use Dataset & DataAdapter
u'll add ur items in dataSet\DataTable\DataRow(s) & call Update() method of it
DataAdapter will all make its inserts & ....
__________________
Always[:)],
Hovik Melkomian.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #5 (permalink)  
Old October 19th, 2009, 06:35 AM
samjudson's Avatar
Friend of Wrox
Points: 4,453, Level: 28
Points: 4,453, Level: 28 Points: 4,453, Level: 28 Points: 4,453, Level: 28
Activity: 60%
Activity: 60% Activity: 60% Activity: 60%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 1,359
Thanks: 0
Thanked 31 Times in 31 Posts
Default

I actually meant this:

Code:
currentMessageId = ....
string sqlStr = "INSERT INTO user_messages (M_Id, User_Id) VALUES (@messageId, @userId)";
 
 SqlCommand command = new SqlCommand (sqlStr, sqlConnectionString);
 command.Parameters.AddWithValue("messageId", currentMessageId);
 SqlParameter userParam = command.Parameters.Add("userId", SqlType.Int);
 
for (int i = 0; i< listBoxUsers.SelectedItems.Count; i+)
{
row = ((DataRowView)this.listBoxUsers.SelectedItems[i]).Row;
int id = Convert.ToInt16(row[listBoxUsers.ValueMember]);
userParam.Value = id;
command.ExecuteNonQuery();
}
__________________
/- Sam Judson : Wrox Technical Editor -/
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
sql injection trufla Classic ASP Basics 2 June 16th, 2008 03:54 PM
SQl Injection through ASP and MS SQl 2000 cancer10 Classic ASP Databases 1 October 27th, 2007 04:21 AM
Pass Multiple Parameters from C# to SQL RS ms_code_bsuter BOOK: Professional SQL Server Reporting Services ISBN: 0-7645-6878-7 2 July 25th, 2005 07:34 PM
SQL Injection cygnusx04 Classic ASP Databases 1 November 6th, 2004 11:06 AM
What SQL Injection is ? minhtri Classic ASP Basics 2 October 20th, 2004 11:11 PM



All times are GMT -4. The time now is 01:22 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
© 2008 Wiley Publishing, Inc