Selectcommand Variable

D

Dave

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"/>

When the username is hard-coded it works fine:
<asp:AccessDataSource id="AccessDataSource1" runat="server" DataFile =
"upload.mdb" SelectCommand="Select * from Customers WHERE
Login='djohnson'"/>

I don't understand it. This is getting frustrating.
 
G

Guest

You need to supply a value for the @current_user using the SelectParameters
collection and the Selecting event handler.

<asp:AccessDataSource ....>
<SelectParameters>
<asp:parameter Direction="Input" Name="current_user" Type="string" />
</SelectParameters>
</asp:AccessDataSource>

Define a handler for the Selecting event of the AccessDataSource and assign
the value for current_user:

protected void AccessDataSource_Selecting (object sender,
SqlDataSourceSelectingEventArgs e)
{
e.Command.Parameters["current_user"] = <value>;
}
 

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

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top