How can I get a value from webforms dropDownList box to a class file

  • Thread starter Thread starter jonny
  • Start date Start date
J

jonny

Hi guys,

I am trying to figure out how to get the value that is in the
dropdownlist box on webform1 to my class file where I am actually
writing the sql query.

for example here is my sql query:

Dim sql As String = "SELECT distinct Column2 FROM MySqlTable Where
Column1 = '" & Webform.DropDownList & "'"

The problem is where I have...Webform.DropDownList ....because it does
not recognize the webform or its dropdownlist from within the class
file.

So please tell me how I can get the class file to recognize the
webform and its dropdownlist.

I really appreciate your guys help if anyone out there knows how to do
this.
 
Hi guys,

I am trying to figure out how to get the value that is in the
dropdownlist box on webform1 to my class file where I am actually
writing the sql query.

for example here is my sql query:

Dim sql As String = "SELECT distinct Column2 FROM MySqlTable Where
Column1 = '" & Webform.DropDownList & "'"

The problem is where I have...Webform.DropDownList ....because it does
not recognize the webform or its dropdownlist from within the class
file.

So please tell me how I can get the class file to recognize the
webform and its dropdownlist.

I really appreciate your guys help if anyone out there knows how to do
this.

This is the direction you need to head. I am not stating, however, this
is the correct code, just that this should give you a direction:

Public Class MyClass

Public Shared Function RunSqlCode(ByVal input As String) As DataSet
Dim sql As String = "SELECT distinct Column2 FROM MySqlTable" & _
" Where Column1 = '" & input & "'"
End Function

End Class

'UI Code
Dim s As String = DropDownList1.SelectedValue
DataSet ds = MyClass.RunSqlCode(s)

The point is you DO NOT pull items from a webform in a class library.
Possible to do? Yes. You reference the proper web libraries and you can
do it. Why not do it? You tightly couple the library to a web
implementation, which means you cannot reuse it on other types of apps
without a major overhaul.

Peace and Grace,


--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

My vacation and childhood cancer awareness site:
http://www.crazycancertour.com

*******************************************
| Think outside the box! |
*******************************************
 
(e-mail address removed):










This is the direction you need to head. I am not stating, however, this
is the correct code, just that this should give you a direction:

Public Class MyClass

   Public Shared Function RunSqlCode(ByVal input As String) As DataSet
      Dim sql As String = "SELECT distinct Column2 FROM MySqlTable" & _    
          " Where Column1  = '" & input & "'"
   End Function

End Class

'UI Code
Dim s As String = DropDownList1.SelectedValue
DataSet ds = MyClass.RunSqlCode(s)

The point is you DO NOT pull items from a webform in a class library.
Possible to do? Yes. You reference the proper web libraries and you can
do it. Why not do it? You tightly couple the library to a web
implementation, which means you cannot reuse it on other types of apps
without a major overhaul.

Peace and Grace,

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog:http://gregorybeamer.spaces.live.com

My vacation and childhood cancer awareness site:http://www.crazycancertour.com

*******************************************
|      Think outside the box!             |
*******************************************

Thanks Gregory. I am confused. Do i need to put the Public Class
MyClass code you listed on my class file page and then put the 'UI
Code on the webform page?
 
Thanks Gregory. I am confused. Do i need to put the Public Class
MyClass code you listed on my class file page and then put the 'UI
Code on the webform page?

Yes, the Shared code is for the class. your data access should be separate
from the webform. And you should pass the value into the class and not try
to pull directly from the control. That is a job for the webform code.

The idea here is the application is contained in the library(s) and the
webform is UI, so it deals with pulling any user input. UI should be like a
"skin" on top of the functional app code.

Peace and Grace,

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

My vacation and childhood cancer awareness site:
http://www.crazycancertour.com

*******************************************
| Think outside the box! |
*******************************************
 
jonny said:
for example here is my sql query:

Dim sql As String = "SELECT distinct Column2 FROM MySqlTable Where
Column1 = '" & Webform.DropDownList & "'"

And what happens when Webform.DropDownList.SelectedValue contains an
apostrophe/single quote (e.g. a surname like O'Neal)? The query breaks. This
is the direct route for an SQL injection attack; you need to use parameters
for the query.

http://en.wikipedia.org/wiki/Sql_injection

Little Bobby Tables:
http://xkcd.com/327/

HTH

Andrew
 
Back
Top