如何在SqlDataSource中传递属性值select select where where condition?

时间:2012-04-27 08:36:56

标签: c# asp.net sqldatasource

我的page2.aspx.cs页面中有一个属性

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

我正在使用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>

我想在我的条件下传递Id。怎么可能。

请有人帮助我。

4 个答案:

答案 0 :(得分:4)

在这里,看看

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

答案 1 :(得分:1)

1)向您选择查询

插入带有参数名称@Id的占位符

2)将Id参数的参数规范添加到SqlDataSource的<SelectParameter>部分

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

    <SelectParameters>
         <asp:Parameter Name="Id" Type="Int32" />
    </SelectParameters>

</asp:SqlDataSource>

3)在选择事件方法中将参数值放入命令

protected void SqlDataSourceGridView_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
    e.Command.Parameters["@Id"].Value = (int)(Id ?? 0); // nullable ind is not applicable here
}

答案 2 :(得分:0)

您必须在SQLDataSource下使用Node。

在这里查看:MSDN ReferenceTutorial

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

   <SelectParameters>
     <asp:Parameter Name="ParameterName" />
   </SelectParameters>

</asp:SqlDataSource>

OT:您在属性ID上有错误。你设置ViewState [Id],但你试图检索 视图状态[Page.id]

答案 3 :(得分:0)

您可以从后面的代码设置此项。只需在sql数据源中定义一个select参数,如

    <asp:SqlDataSource ID="SqlDataSourceGridView" runat="server" 
   ProviderName="System.Data.SqlClient" 
   SelectCommand="SELECT [Name],[Address] FROM [Table_Emp] where Id =@id" 
   OnSelecting="SqlDataSourceGridView_Selecting">
  <SelectParameters>
        <asp:Parameter Name="@id" Type="Int32" />
    </SelectParameters>

然后在您的代码中,您可以手动设置参数,如

SqlDataSourceGridView.SelectParameters["@id"].DefaultValue = yourValue;
Gridview1.DataBind();