Selectcommand with variable

  • Thread starter Thread starter djohnson
  • Start date Start date
D

djohnson

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.... !
 
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.
 
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.
 
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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top