How to pass property value in SqlDataSource selectCommand where conditon?

Rocky picture Rocky · Apr 27, 2012 · Viewed 11.5k times · Source

I have a property in my page2.aspx.cs page

public int? Id
{
        get
        {
            if (ViewState[Page1.Id] != null)
                return Convert.ToInt32(ViewState[Page1.Id]);
            else return null;
        }
        set
        {
            ViewState[Id] = value;
        }
    }

and I am using asp: data sourse:

<asp:SqlDataSource ID="SqlDataSourceGridView" runat="server" 
   ProviderName="System.Data.SqlClient" 
   SelectCommand="SELECT [Name],[Address] FROM [Table_Emp] where Id = need to pass property value >" 
   OnSelecting="SqlDataSourceGridView_Selecting">
</asp:SqlDataSource>

I want to pass Id in my where condition. how is it possible.

Please some one help me.

Answer

Johnny_D picture Johnny_D · Apr 27, 2012

Here, take a look

 <asp:sqlDataSource ID="EmployeeDetailsSqlDataSource" 
   SelectCommand="SELECT EmployeeID, LastName, FirstName FROM Employees WHERE EmployeeID = @EmpID"

   InsertCommand="INSERT INTO Employees(LastName, FirstName) VALUES (@LastName, @FirstName); 
                  SELECT @EmpID = SCOPE_IDENTITY()"
   UpdateCommand="UPDATE Employees SET LastName=@LastName, FirstName=@FirstName 
                    WHERE EmployeeID=@EmployeeID"
   DeleteCommand="DELETE Employees WHERE EmployeeID=@EmployeeID"

   ConnectionString="<%$ ConnectionStrings:NorthwindConnection %>"
   OnInserted="EmployeeDetailsSqlDataSource_OnInserted"
   RunAt="server">

   <SelectParameters>
     <asp:Parameter Name="EmpID" Type="Int32" DefaultValue="0" />
   </SelectParameters>

   <InsertParameters>
     <asp:Parameter Name="EmpID" Direction="Output" Type="Int32" DefaultValue="0" />
   </InsertParameters>

 </asp:sqlDataSource>