473,385 Members | 1,925 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,385 software developers and data experts.

Selectcommand with variable

Hello, I'm wondering why a variable in my selectcommand is not
working.

In Page_Load I have this:

String current_user = User.Identity.Name;
Response.Write(current_user); // verified the string 'djohnson' is
present

But this errors out in the Body ('No value given for one or more
required parameters'):
<asp:AccessDataSource id="AccessDataSource1" runat="server" DataFile =
"upload.mdb" SelectCommand="Select * from Customers WHERE
Login=@current_user"/>

This, however, works:
<asp:AccessDataSource id="AccessDataSource1" runat="server" DataFile =
"upload.mdb" SelectCommand="Select * from Customers WHERE
Login='djohnson'"/>
This is very confusing.... !

May 14 '07 #1
4 2807
djohnson,

The AccessDataSource has no way of knowing how to access your local
variables. If you want to parameterize your query, you will need to place
extra tags in your AccessDataSource tag indicating the parameter type.

Then, in your code, you would get the AccessDataSource instance and then
set the values in the parameters returned by the SelectParameters property.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"djohnson" <on********@gmail.comwrote in message
news:11*********************@w5g2000hsg.googlegrou ps.com...
Hello, I'm wondering why a variable in my selectcommand is not
working.

In Page_Load I have this:

String current_user = User.Identity.Name;
Response.Write(current_user); // verified the string 'djohnson' is
present

But this errors out in the Body ('No value given for one or more
required parameters'):
<asp:AccessDataSource id="AccessDataSource1" runat="server" DataFile =
"upload.mdb" SelectCommand="Select * from Customers WHERE
Login=@current_user"/>

This, however, works:
<asp:AccessDataSource id="AccessDataSource1" runat="server" DataFile =
"upload.mdb" SelectCommand="Select * from Customers WHERE
Login='djohnson'"/>
This is very confusing.... !

May 14 '07 #2
I can't just use a session variable?

Anyway, I looked at the link,
http://msdn2.microsoft.com/en-us/lib...ollection.aspx.

Based on this technique would I need to setup a hidden field in which
to keep the username string?

May 14 '07 #3
djohnson,

Well, you would have to use the appropriate tag in the AccessDataSource
to set up the parameter value. If you want to get the value from the
session, then you have to use the SessionParameter tag.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"djohnson" <on********@gmail.comwrote in message
news:11**********************@e51g2000hsg.googlegr oups.com...
>I can't just use a session variable?

Anyway, I looked at the link,
http://msdn2.microsoft.com/en-us/lib...ollection.aspx.

Based on this technique would I need to setup a hidden field in which
to keep the username string?

May 14 '07 #4
That works.

So I put this is page_load:
Session["username"] = User.Identity.Name;

And setup the data source like this:
<asp:AccessDataSource id="AccessDataSource1" runat="server" DataFile
= "secretinfo.mdb" SelectCommand="Select * from Customers WHERE
Login=?">
<SelectParameters>
<asp:SessionParameter
Name="username"
SessionField="username"
DefaultValue="" />
</SelectParameters>
</asp:AccessDataSource>
Thanks!

May 14 '07 #5

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

Similar topics

0
by: John Celmer | last post by:
My datagrid has a dataset that has only one table that is populated with a given SelectCommand->Text. I want to change the SelectCommand->Text so that the datagrid displays updated data with a...
0
by: Gancy | last post by:
Hi, I have data access tier designed in such a way, just by changnging application settings, same lines of code is made to work with both MS SQL Server or MS Access. Code works fine with MS SQL...
0
by: Tammy B | last post by:
Hiya How do I programatically set the selectcommand? Select col1, col2 from TABLE_1 next time I want it to be Select col1, col2 from TABLE_2 I won't know which table till runtime
2
by: phil | last post by:
Hi, I want to put a recordset in a gridview but i don't know how to pass the value of the variable in the 'where' statement. The value of the variable is set in the code-behind. i added a tag...
0
by: mlfblom | last post by:
Hi, I know I am not the only one struggling with the following, but so far I have not seen a solution. Problem: I have an asp.net 2.0 web page with a gridview which is bound to an...
4
by: googlegroup | last post by:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionString:SalesDBConnectionString %>" SelectCommand="<%=strSQL%>" /> In ASP.NET , how do you assign a dynamic...
1
by: John Kotuby | last post by:
Hello again... I have tried using the SQLDatsource control as part of a user control that just conatins a Repeater and the SQLDatasource control which is designated as the Datsource for the...
1
by: Dave | last post by:
Hello, I'm wondering why a variable in my selectcommand is not working. In Page_Load I have this: String current_user = User.Identity.Name; Response.Write(current_user); // verified the...
5
by: =?Utf-8?B?SGFycnkgVg==?= | last post by:
I'm using a StringBuilder to build a SelectCommand string that returns a number of records. If that number is >0, I want to fill an arraylist with fields from the records for a listbox. Is it...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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,...

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.