473,509 Members | 2,950 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Simple Variable Pass on to SQL Where Query question.

I'm a newbie using VS2005 to learn and test. I'm testing against the
Northwind DB.
I created a simple variable called "myVariable" and I assigned it the
character "c". I want my SQL query to use this variable in the "Where"
statement where it will pull up all names with the letter "c" in it. How do
I pass this variable on the the WHERE statement? I thought all I had to do
was use the <% %> with my variable inside and that would work but when I ran
it, nothing appeared. I have enclosed the acutal codes.

Please help and thank you!
Newbie Phil


Actual Code Begins here:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default2.aspx.vb"
Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>Untitled Page</title>

</head>

<body>

<form id="form1" runat="server">

<% Dim myVariable As String

myVariable = "c"

%>

<div>

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$
ConnectionStrings:NorthwindConnectionString %>"

SelectCommand="SELECT ContactName FROM Customers WHERE
(ContactName LIKE N'%<%myVariable %>%')">

</asp:SqlDataSource>
</div>

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1">

<Columns>

<asp:BoundField DataField="ContactName" HeaderText="ContactName"
SortExpression="ContactName" />

</Columns>

</asp:GridView>

</form>

</body>

</html>
Apr 2 '06 #1
4 2219
Do something for example:-
SELECT * FROM Persons
WHERE FirstName LIKE '%YourVariable%'
Or just use stored procedure.
Patrick

"Phillip Vong" <phillip_vong*at*yahoo*dot*com> wrote in message
news:u9**************@TK2MSFTNGP12.phx.gbl...
I'm a newbie using VS2005 to learn and test. I'm testing against the
Northwind DB.
I created a simple variable called "myVariable" and I assigned it the
character "c". I want my SQL query to use this variable in the "Where"
statement where it will pull up all names with the letter "c" in it. How
do I pass this variable on the the WHERE statement? I thought all I had
to do was use the <% %> with my variable inside and that would work but
when I ran it, nothing appeared. I have enclosed the acutal codes.

Please help and thank you!
Newbie Phil


Actual Code Begins here:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default2.aspx.vb"
Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>Untitled Page</title>

</head>

<body>

<form id="form1" runat="server">

<% Dim myVariable As String

myVariable = "c"

%>

<div>

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"

SelectCommand="SELECT ContactName FROM Customers WHERE
(ContactName LIKE N'%<%myVariable %>%')">

</asp:SqlDataSource>
</div>

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1">

<Columns>

<asp:BoundField DataField="ContactName" HeaderText="ContactName"
SortExpression="ContactName" />

</Columns>

</asp:GridView>

</form>

</body>

</html>

Apr 2 '06 #2
I'll have look up stored procedure and see what that's all about.

I did do it just like you said where I put '%Varible%' but the Where
statement actually thinks I'm using the word Variable versus looking at the
variable.

Please help.
"Patrick.O.Ige" <na********@hotmail.com> wrote in message
news:OI**************@tk2msftngp13.phx.gbl...
Do something for example:-
SELECT * FROM Persons
WHERE FirstName LIKE '%YourVariable%'
Or just use stored procedure.
Patrick

"Phillip Vong" <phillip_vong*at*yahoo*dot*com> wrote in message
news:u9**************@TK2MSFTNGP12.phx.gbl...
I'm a newbie using VS2005 to learn and test. I'm testing against the
Northwind DB.
I created a simple variable called "myVariable" and I assigned it the
character "c". I want my SQL query to use this variable in the "Where"
statement where it will pull up all names with the letter "c" in it. How
do I pass this variable on the the WHERE statement? I thought all I had
to do was use the <% %> with my variable inside and that would work but
when I ran it, nothing appeared. I have enclosed the acutal codes.

Please help and thank you!
Newbie Phil


Actual Code Begins here:
<%@ Page Language="VB" AutoEventWireup="false"
CodeFile="Default2.aspx.vb" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>Untitled Page</title>

</head>

<body>

<form id="form1" runat="server">

<% Dim myVariable As String

myVariable = "c"

%>

<div>

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"

SelectCommand="SELECT ContactName FROM Customers WHERE
(ContactName LIKE N'%<%myVariable %>%')">

</asp:SqlDataSource>
</div>

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1">

<Columns>

<asp:BoundField DataField="ContactName" HeaderText="ContactName"
SortExpression="ContactName" />

</Columns>

</asp:GridView>

</form>

</body>

</html>


Apr 2 '06 #3
+1 for using Stored Procedures - life's easier without SQL scattered
through your pages, but...
<asp:SqlDataSource id="myDataSource" runat="server"
ConnectionString="..."
SelectCommand="SELECT Column1, Column2 FROM table WHERE key =
@MyKeyName">

<SelectParameters>
<asp:Parameter Name="MyKeyName" DefaultValue="SomeValue" />
</SelectParameters>
</asp:SqlDataSource>
You can use a ControlParameter to bind the value to a control, a
QueryStringParameter to bind the value to a query string field, or just
use bog-standard Parameter and set the value yourself.

Best place to do that is in Page_Load if your value won't change eg.
if( ! Page.IsPostback)
MyDataSource.SelectParameters["MyKeyName"].DefaultValue =
TheUserBrowsingThisPage.UserId;

Or in the SqlDataSourceSelecting event if you want to change it
dynamically for each select.

Futher info here
http://msdn2.microsoft.com/en-us/lib...ad(VS.80).aspx
and here
http://msdn2.microsoft.com/en-US/lib...ng(VS.80).aspx
HTH. But you should still prefer stored procedures, or at least keeping
your SQL in a data access class.

Apr 2 '06 #4
You have a fundamental problem which you will need to address in short
order, or you will find yourself totally frustrated with ASP.Net before you
know it. It looks to me like you have done classic ASP in the past, and that
you are now beginning to work with ASP.Net. However, these 2 technologies
are very different. The chief difference is that classic ASP is procedural
in nature, while ASP.Net is fully object-oriented and event-driven. If you
try to do ASP.Net in a procedural fashion, you will be frustrated and
confused at nearly every turn. Because ASP.Net is fully object-oriented and
event-driven, program execution does not simply flow from top to bottom. It
is event-driven, which means that your program flow is going to bounce all
over the place, from one event and one class to the next. It is also
multi-threaded, although you won't have to deal with that very much in the
beginning. But its object-orientation, the Microsoft System.Web.UI.Page
execution model, and its event-driven nature tend to compel the developer to
design his/her application in accordance with these. Do yourself a favor and
read up on all of them before you try to go any further. It is better to
start out right than to have to re-start over and over again.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Show me your certification without works,
and I'll show my certification
*by* my works.

"Phillip Vong" <phillip_vong*at*yahoo*dot*com> wrote in message
news:u9**************@TK2MSFTNGP12.phx.gbl...
I'm a newbie using VS2005 to learn and test. I'm testing against the
Northwind DB.
I created a simple variable called "myVariable" and I assigned it the
character "c". I want my SQL query to use this variable in the "Where"
statement where it will pull up all names with the letter "c" in it. How
do I pass this variable on the the WHERE statement? I thought all I had
to do was use the <% %> with my variable inside and that would work but
when I ran it, nothing appeared. I have enclosed the acutal codes.

Please help and thank you!
Newbie Phil


Actual Code Begins here:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default2.aspx.vb"
Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>Untitled Page</title>

</head>

<body>

<form id="form1" runat="server">

<% Dim myVariable As String

myVariable = "c"

%>

<div>

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"

SelectCommand="SELECT ContactName FROM Customers WHERE
(ContactName LIKE N'%<%myVariable %>%')">

</asp:SqlDataSource>
</div>

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1">

<Columns>

<asp:BoundField DataField="ContactName" HeaderText="ContactName"
SortExpression="ContactName" />

</Columns>

</asp:GridView>

</form>

</body>

</html>

Apr 2 '06 #5

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

Similar topics

4
9616
by: Wayne Baswell | last post by:
I want to pass a date variable to a sql statement from Crystal Reports. The part of the query accepting the variables looks like: "Calendar_Date between To_Date('1-JUN-03','mm/dd/yy') and...
17
2614
by: Paul | last post by:
HI! I get an error with this code. <SCRIPT language="JavaScript"> If (ifp==""){ ifp="default.htm"} //--></SCRIPT> Basicly I want my iframe to have a default page if the user enters in...
3
1544
by: Eli Criffield | last post by:
I'm getting odd results from my fuction that takes variable aguments. What i want to do is take a printf like format and pass that to mysql_query then i do some processing of the results and...
3
1872
by: JezB | last post by:
I know I can use the querystring mechanism to pass simple parameters to a Page using Response.Redirect, but Im having problems getting this to work using Server.Transfer or Server.Execute - is this...
3
1747
by: John Baker | last post by:
Hi:7 Newby here to ASP, and using the ASP.NET Web Matrix development tool. While that tool looks great for a Newby, I have run into a snag. I have an HTML Text Box which I have named HireInput,...
3
1969
by: James Robertson | last post by:
I am new to the ASP and VB thing so be kind. Question I have is that I have created an ASPX web site to use as an E-Mail page. But I want to use this for a lot of users. Can I create the link on...
6
8339
by: Vyoma | last post by:
This is quite a bit of problem I am facing, and I cannot point exactly where I am going wrong. I have been lurking around at several forums with regard to login and user authentication scripts and...
3
1836
by: nembo kid | last post by:
I have an issue with a simple function that has to make a linear search for a key into an array. If the key is found in the array, the function it has to return 1 to the caller and pass array...
1
4823
by: pretzelboy | last post by:
Hi, I last wrote software 13years ago in the pascal, dbase, clipper days. I have recently built a Ubuntu Box and with C++ (and help from the web) setup a serial barcode reader program using Mysql...
0
7136
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
7344
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
7505
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
5652
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
4730
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3203
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1570
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 ...
1
775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
441
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.