473,473 Members | 1,947 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Building a Dynamic Where Clause

I have 3 combo boxes and two date text boxes on a .aspx page. The user can
fill in any of the 5 controls or none to filter a datagrid. I was hoping
someone could explain how to efficiently build the where clause of a sql
string to send to SQL 2000 for a data set.

Currenly I check each control with an IF statement to determine if something
is filled in. If there is I begin building the where clause. Below is what
I have done (and it works just fine) but am curious if there is a better
way.

Thanks

Controls are:
txtOrdNum
ctlThru.DateField 'User Control with a calandar control. References a
text box
ctlFrom.DateField 'User Control with a calandar control. References a
text box
cboTask
cboEmployee
'Combo boxes display a list item with a blank text and -1 as the value
Currenly if the control is null then I skip it

Private Function WhereFunction() As String
Dim OrdNum As String
Dim Employee As String
Dim DateFrom As String
Dim DateThru As String
Dim OrderNumber As String
Dim x As StringBuilder
Dim y As Int16
Dim lastIndexof As Int16

DateThru = Me.ctlThru.DateField
DateFrom = Me.ctlFrom.DateField

If Not Me.txtOrdNum.Text = String.Empty Then
WhereFunction = WhereFunction & "j_recref = " &
Me.txtOrdNum.Text & " AND "
End If

If Me.cboEmployee.SelectedItem.Value <> -1 Then
WhereFunction = WhereFunction & "j_UserID = " &
cboEmployee.SelectedItem.Value & " AND "
End If

If Me.cboTask.SelectedItem.Value <> -1 Then
WhereFunction = WhereFunction & "j_taskid = " &
cboTask.SelectedItem.Value & " AND "
End If

If DateFrom = String.Empty Then
'Nothing
ElseIf DateThru = String.Empty Then
WhereFunction = WhereFunction & "j_date = " & gloQuote(DateFrom)
& " AND "
Else
WhereFunction = WhereFunction & "j_date BETWEEN " &
gloQuote(DateFrom) _
& " AND " & gloQuote(DateThru) & " AND "
End If

If WhereFunction <> String.Empty Then
WhereFunction = " WHERE j_timein <> '' AND j_timeout <> '' AND "
& WhereFunction
x = New StringBuilder(WhereFunction)
y = x.Length
lastIndexof = x.ToString.LastIndexOf(" ")
If y - 1 = lastIndexof Then
WhereFunction = WhereFunction.Remove(y - 5, 5)
End If
End If
Return WhereFunction
End Function
Nov 18 '05 #1
4 3786
Thanks for the input on SQL injection attacks. Although I believe my
strategy will prevent that for the drop downs are restricted to the values
binded to the control and the two date text boxes require a valid date. So,
I hope I haven't missed anything to allow such unwanted attacks.

Although you are correct that I should use SQLParameters being that is the
more efficient way of doing Data Access events. I think I will change my
strategy to your suggested method.

By placing a '%' sign for null values will that hit performance since it's
doing a search all?

Thanks
"John Sivilla" <Jo*********@discussions.microsoft.com> wrote in message
news:DE**********************************@microsof t.com...
Appending text directly from web controls and putting them into a sql query, especially without validating them is a huge security hole. You are leaving your site open to SQL injection attacks.

An alternative to what you have, you could build one query using
SQLParameters. Make a parameter for each of your fields that are dynamic;
give them default values of wildcards '%' so that if there are not used the they will return everything. And give the parameters that are used the values of what is entered.

This is much more secure, and it is even easier to develop and program.

hope this helps,
John

"Brian Shannon" wrote:
I have 3 combo boxes and two date text boxes on a .aspx page. The user can fill in any of the 5 controls or none to filter a datagrid. I was hoping someone could explain how to efficiently build the where clause of a sql
string to send to SQL 2000 for a data set.

Currenly I check each control with an IF statement to determine if something is filled in. If there is I begin building the where clause. Below is what I have done (and it works just fine) but am curious if there is a better
way.

Thanks

Controls are:
txtOrdNum
ctlThru.DateField 'User Control with a calandar control. References a
text box
ctlFrom.DateField 'User Control with a calandar control. References a
text box
cboTask
cboEmployee
'Combo boxes display a list item with a blank text and -1 as the value
Currenly if the control is null then I skip it

Private Function WhereFunction() As String
Dim OrdNum As String
Dim Employee As String
Dim DateFrom As String
Dim DateThru As String
Dim OrderNumber As String
Dim x As StringBuilder
Dim y As Int16
Dim lastIndexof As Int16

DateThru = Me.ctlThru.DateField
DateFrom = Me.ctlFrom.DateField

If Not Me.txtOrdNum.Text = String.Empty Then
WhereFunction = WhereFunction & "j_recref = " &
Me.txtOrdNum.Text & " AND "
End If

If Me.cboEmployee.SelectedItem.Value <> -1 Then
WhereFunction = WhereFunction & "j_UserID = " &
cboEmployee.SelectedItem.Value & " AND "
End If

If Me.cboTask.SelectedItem.Value <> -1 Then
WhereFunction = WhereFunction & "j_taskid = " &
cboTask.SelectedItem.Value & " AND "
End If

If DateFrom = String.Empty Then
'Nothing
ElseIf DateThru = String.Empty Then
WhereFunction = WhereFunction & "j_date = " & gloQuote(DateFrom) & " AND "
Else
WhereFunction = WhereFunction & "j_date BETWEEN " &
gloQuote(DateFrom) _
& " AND " & gloQuote(DateThru) & " AND "
End If

If WhereFunction <> String.Empty Then
WhereFunction = " WHERE j_timein <> '' AND j_timeout <> '' AND " & WhereFunction
x = New StringBuilder(WhereFunction)
y = x.Length
lastIndexof = x.ToString.LastIndexOf(" ")
If y - 1 = lastIndexof Then
WhereFunction = WhereFunction.Remove(y - 5, 5)
End If
End If
Return WhereFunction
End Function

Nov 18 '05 #2
Hi Brian:

Just to follow up on your post:

I could write a program or a script to POST a value to the server
which is not in the drop down list. You really can't trust anything
that comes in over the network - there are ways to circumvent the UI.

Wildcard matches will generally take longer than finding an exact
match, but you'll have to measure with your application to see if the
performance hit is noticeable or acceptable.

--
Scott
http://www.OdeToCode.com
On Thu, 5 Aug 2004 09:35:12 -0500, "Brian Shannon"
<bs******@lbrspec.com> wrote:
Thanks for the input on SQL injection attacks. Although I believe my
strategy will prevent that for the drop downs are restricted to the values
binded to the control and the two date text boxes require a valid date. So,
I hope I haven't missed anything to allow such unwanted attacks.

Although you are correct that I should use SQLParameters being that is the
more efficient way of doing Data Access events. I think I will change my
strategy to your suggested method.

By placing a '%' sign for null values will that hit performance since it's
doing a search all?

Thanks
"John Sivilla" <Jo*********@discussions.microsoft.com> wrote in message
news:DE**********************************@microso ft.com...
Appending text directly from web controls and putting them into a sql

query,
especially without validating them is a huge security hole. You are

leaving
your site open to SQL injection attacks.

An alternative to what you have, you could build one query using
SQLParameters. Make a parameter for each of your fields that are dynamic;
give them default values of wildcards '%' so that if there are not used

the
they will return everything. And give the parameters that are used the

values
of what is entered.

This is much more secure, and it is even easier to develop and program.

hope this helps,
John

"Brian Shannon" wrote:
> I have 3 combo boxes and two date text boxes on a .aspx page. The usercan > fill in any of the 5 controls or none to filter a datagrid. I washoping > someone could explain how to efficiently build the where clause of a sql
> string to send to SQL 2000 for a data set.
>
> Currenly I check each control with an IF statement to determine ifsomething > is filled in. If there is I begin building the where clause. Below iswhat > I have done (and it works just fine) but am curious if there is a better
> way.
>
> Thanks
>
> Controls are:
> txtOrdNum
> ctlThru.DateField 'User Control with a calandar control. References a
> text box
> ctlFrom.DateField 'User Control with a calandar control. References a
> text box
> cboTask
> cboEmployee
> 'Combo boxes display a list item with a blank text and -1 as the value
> Currenly if the control is null then I skip it
>
> Private Function WhereFunction() As String
> Dim OrdNum As String
> Dim Employee As String
> Dim DateFrom As String
> Dim DateThru As String
> Dim OrderNumber As String
> Dim x As StringBuilder
> Dim y As Int16
> Dim lastIndexof As Int16
>
> DateThru = Me.ctlThru.DateField
> DateFrom = Me.ctlFrom.DateField
>
> If Not Me.txtOrdNum.Text = String.Empty Then
> WhereFunction = WhereFunction & "j_recref = " &
> Me.txtOrdNum.Text & " AND "
> End If
>
> If Me.cboEmployee.SelectedItem.Value <> -1 Then
> WhereFunction = WhereFunction & "j_UserID = " &
> cboEmployee.SelectedItem.Value & " AND "
> End If
>
> If Me.cboTask.SelectedItem.Value <> -1 Then
> WhereFunction = WhereFunction & "j_taskid = " &
> cboTask.SelectedItem.Value & " AND "
> End If
>
> If DateFrom = String.Empty Then
> 'Nothing
> ElseIf DateThru = String.Empty Then
> WhereFunction = WhereFunction & "j_date = " &gloQuote(DateFrom) > & " AND "
> Else
> WhereFunction = WhereFunction & "j_date BETWEEN " &
> gloQuote(DateFrom) _
> & " AND " & gloQuote(DateThru) & " AND "
> End If
>
> If WhereFunction <> String.Empty Then
> WhereFunction = " WHERE j_timein <> '' AND j_timeout <> ''AND " > & WhereFunction
> x = New StringBuilder(WhereFunction)
> y = x.Length
> lastIndexof = x.ToString.LastIndexOf(" ")
> If y - 1 = lastIndexof Then
> WhereFunction = WhereFunction.Remove(y - 5, 5)
> End If
> End If
> Return WhereFunction
> End Function
>
>
>


Nov 18 '05 #3
Thanks for the followup. Would wildcard matches take care of the sql
injection?

How does someone script something to be posted to the server that is not
entered in the dropdown box? My sql statements are based off the values
from the control?

Just curious? I guess I am not all that familiar with sql injection
although I have heard a lot about it. Does using sql paramaters take care
of it?

Thanks
"Scott Allen" <bitmask@[nospam].fred.net> wrote in message
news:4d********************************@4ax.com...
Hi Brian:

Just to follow up on your post:

I could write a program or a script to POST a value to the server
which is not in the drop down list. You really can't trust anything
that comes in over the network - there are ways to circumvent the UI.

Wildcard matches will generally take longer than finding an exact
match, but you'll have to measure with your application to see if the
performance hit is noticeable or acceptable.

--
Scott
http://www.OdeToCode.com
On Thu, 5 Aug 2004 09:35:12 -0500, "Brian Shannon"
<bs******@lbrspec.com> wrote:
Thanks for the input on SQL injection attacks. Although I believe my
strategy will prevent that for the drop downs are restricted to the valuesbinded to the control and the two date text boxes require a valid date. So,I hope I haven't missed anything to allow such unwanted attacks.

Although you are correct that I should use SQLParameters being that is themore efficient way of doing Data Access events. I think I will change my
strategy to your suggested method.

By placing a '%' sign for null values will that hit performance since it'sdoing a search all?

Thanks
"John Sivilla" <Jo*********@discussions.microsoft.com> wrote in message
news:DE**********************************@microso ft.com...
Appending text directly from web controls and putting them into a sql

query,
especially without validating them is a huge security hole. You are

leaving
your site open to SQL injection attacks.

An alternative to what you have, you could build one query using
SQLParameters. Make a parameter for each of your fields that are dynamic; give them default values of wildcards '%' so that if there are not used

the
they will return everything. And give the parameters that are used the

values
of what is entered.

This is much more secure, and it is even easier to develop and program.

hope this helps,
John

"Brian Shannon" wrote:

> I have 3 combo boxes and two date text boxes on a .aspx page. The user
can
> fill in any of the 5 controls or none to filter a datagrid. I was

hoping
> someone could explain how to efficiently build the where clause of a
sql > string to send to SQL 2000 for a data set.
>
> Currenly I check each control with an IF statement to determine if

something
> is filled in. If there is I begin building the where clause. Below iswhat
> I have done (and it works just fine) but am curious if there is a
better > way.
>
> Thanks
>
> Controls are:
> txtOrdNum
> ctlThru.DateField 'User Control with a calandar control. References a > text box
> ctlFrom.DateField 'User Control with a calandar control. References a > text box
> cboTask
> cboEmployee
> 'Combo boxes display a list item with a blank text and -1 as the value > Currenly if the control is null then I skip it
>
> Private Function WhereFunction() As String
> Dim OrdNum As String
> Dim Employee As String
> Dim DateFrom As String
> Dim DateThru As String
> Dim OrderNumber As String
> Dim x As StringBuilder
> Dim y As Int16
> Dim lastIndexof As Int16
>
> DateThru = Me.ctlThru.DateField
> DateFrom = Me.ctlFrom.DateField
>
> If Not Me.txtOrdNum.Text = String.Empty Then
> WhereFunction = WhereFunction & "j_recref = " &
> Me.txtOrdNum.Text & " AND "
> End If
>
> If Me.cboEmployee.SelectedItem.Value <> -1 Then
> WhereFunction = WhereFunction & "j_UserID = " &
> cboEmployee.SelectedItem.Value & " AND "
> End If
>
> If Me.cboTask.SelectedItem.Value <> -1 Then
> WhereFunction = WhereFunction & "j_taskid = " &
> cboTask.SelectedItem.Value & " AND "
> End If
>
> If DateFrom = String.Empty Then
> 'Nothing
> ElseIf DateThru = String.Empty Then
> WhereFunction = WhereFunction & "j_date = " &

gloQuote(DateFrom)
> & " AND "
> Else
> WhereFunction = WhereFunction & "j_date BETWEEN " &
> gloQuote(DateFrom) _
> & " AND " & gloQuote(DateThru) & " AND "
> End If
>
> If WhereFunction <> String.Empty Then
> WhereFunction = " WHERE j_timein <> '' AND j_timeout <>

''AND "
> & WhereFunction
> x = New StringBuilder(WhereFunction)
> y = x.Length
> lastIndexof = x.ToString.LastIndexOf(" ")
> If y - 1 = lastIndexof Then
> WhereFunction = WhereFunction.Remove(y - 5, 5)
> End If
> End If
> Return WhereFunction
> End Function
>
>
>

Nov 18 '05 #4
Hi Brian:

1) Using parameters will certainly help prevent sql injection
problems.

There is an example of writing a program to programattically fill out
a web form here:
http://msdn.microsoft.com/msdnmag/is...n/default.aspx
2) When the browser sends the value of the drop down the user selected
to the server, it just sends along a simple text string over the
network, like:
dropDownListID=SomeValue

I could write a program, which just like the browser opens an HTTP
connection to the server and sends a value for the selected drop down
item, but my program will send whatever I tell it to send, not
nessecarily a value from the drop down list.

dropDownListID=SomeSQLCommand
What I would do with SomeSQLCommand is enter escape characters to stop
your SQL command and then execute my own. There is an example in the
SQL Injection section here:
http://msdn.microsoft.com/library/de...SecNetch12.asp

--s

On Thu, 5 Aug 2004 11:53:54 -0500, "Brian Shannon"
<bs******@lbrspec.com> wrote:
Thanks for the followup. Would wildcard matches take care of the sql
injection?

How does someone script something to be posted to the server that is not
entered in the dropdown box? My sql statements are based off the values
from the control?

Just curious? I guess I am not all that familiar with sql injection
although I have heard a lot about it. Does using sql paramaters take care
of it?


--
Scott
http://www.OdeToCode.com
Nov 18 '05 #5

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

Similar topics

7
by: diroddi | last post by:
I am having a problem using a dynamic where clause. I have a feeling that I am overlooking something very simple, although I can't seem to figure it out. The error i'm getting is: You have an...
4
by: Robert Scheer | last post by:
Hi. I have a stored procedure on a Oracle 8.1.6 database that generates a dynamic sql statement. This stored procedure has an output parameter that needs to return a count from a view. I can...
2
by: klh | last post by:
We use DB2 Connect v 7.2 FP7 in Windows NT hitting a OS/390 DB2 v7.1 database. We have a Websphere (java) application that issues dynamic SQL. Most of the time when we issue dynamic SQL SELECT...
1
by: Arijit Chatterjee | last post by:
Dear Faculties, I have a query on this statement.. =============================== CREATE PROCEDURE Check_Manage ( ) DYNAMIC RESULT SETS 1 ============================== I want to know the...
1
by: Robert McLay | last post by:
I have been trying to build python on Cray X1. As far as I can tell it does not support dynamic loading. So the question is: How to build 2.4 without dynamic loading? That is: can I build...
5
by: WebMatrix | last post by:
Hello, I am developing ASP.NET 1.1 application against SQL Server 2000 db, where users have ability to construct their own select criteria (for a where clause). I already developed a user...
7
by: Hal Vaughan | last post by:
I have a problem with port forwarding and I have been working on it for over 2 weeks with no luck. I have found C programs that almost work and Java programs that almost work, but nothing that...
1
by: Diffident | last post by:
Hello All, I have a question as to why my users are noticing error when I am building the project on the production system. Here is the problem's background. In order to build the project on...
4
by: Ronald Raygun | last post by:
I need to dynamically create a page that allows me to edit records in a a database table. Basically, something very similar to how PHP MyAdmin works only slighly less complicated. There does not...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.