2

I have a sqlDataSource in which I use select parameter as below :

<asp:sqlDataSource ID="EmployeeDetailsSqlDataSource" 
  SelectCommand="SELECT EmployeeID, LastName, FirstName FROM Employees WHERE EmployeeID = @EmpID"     
  ConnectionString="<%$ ConnectionStrings:NorthwindConnection %>" RunAt="server">
  <SelectParameters>
    <asp:Parameter Name="EmpID" Type="Int32" DefaultValue="<%#userid%>" />
  </SelectParameters>    
</asp:sqlDataSource>

In my backend code, I have assigned a value to userId and also written this.DataBind();

The data-binding doesn't work. Does anyone know a solution to this ?

3
  • 1
    A good way for us to be able to help you find the problem is that if you include the C# code and the Gridview code you are using Commented May 26, 2015 at 12:06
  • Like @CarstenLøvboAndersen said, include the c# code. Probably you parameter is EmpID is not binding Commented May 26, 2015 at 12:18
  • Is there a reason why you need to create this and you couldn't this into a SQL stored procedure? Commented May 26, 2015 at 12:55

1 Answer 1

4

One approach is to just set the value with the userid variable in the code behind Page_Load event or after you bind your variable.

 //set SqlDataSource parameters value with empID as your variable
EmployeeDetailsSqlDataSource.SelectParameters.Add("@EmpID", userID);

Asp:

 <asp:sqlDataSource ID="EmployeeDetailsSqlDataSource" 
          SelectCommand="SELECT EmployeeID, LastName, FirstName FROM Employees WHERE EmployeeID = @EmpID"
          RunAt="server"> 
<SelectParameters>
    <asp:Parameter Name="EmpID" Type="Int32" DefaultValue="0" />
      </SelectParameters>
        </asp:sqlDataSource>

Another option is to set the value from code in the selecting event which occurs before a data retrieval.

SqlDataSource.Selecting Event

Here is a link to a more complete example Full Example

<asp:sqlDataSource ID="EmployeeDetailsSqlDataSource" 
          SelectCommand="SELECT EmployeeID, LastName, FirstName FROM Employees WHERE EmployeeID = @EmpID"
          RunAt="server" OnSelecting=OnSelectingHandler">
 <SelectParameters>
<asp:Parameter Name="EmpID" Type="Int32" DefaultValue="0" />
 </SelectParameters>
 </asp:sqlDataSource>

Code behind:

Public void OnSelectingHandler(object sender, SqlDataSourceSelectingEventArgs e)
{

e.Command.Parameters[0].Value=userID;

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.