评估查询中的参数

时间:2019-07-02 10:49:38

标签: c# asp.net

我创建了一个在itemdatabound事件中传递ID的变量,我想将该ID用作sqldatasource中select的参数,我不知道该怎么做

这是事件:

    protected void ListView2_ItemDataBound(object sender, 
    ListViewItemEventArgs e)
    {

        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            Label ID;
            ID = (Label)e.Item.FindControl("ID");
            ID.Font.Italic = true;

            System.Data.DataRowView view = e.Item.DataItem as 
            System.Data.DataRowView;

        }
    }

这是sqldatasource:

    protected void SqlDataSource5_Load(object sender, EventArgs e)
    {
        var connectionString = 
    ConfigurationManager.ConnectionStrings["dbConnection"].ConnectionString;
        string query = "SELECT ID, RagioneSociale, Email, Piva FROM 
        Persona WHERE Tipocliente = 2 AND ID_Società = @ID";
        using (SqlConnection con = new SqlConnection(connectionString))
        {
            using (var cmd = new SqlCommand(query, con))
            {

            }
        }
    }

1 个答案:

答案 0 :(得分:2)

您尝试过吗:

using( var cmd  = new SqlCommand(query,con)
{
    cmd.Parameters.Add("@ID", "this.ID");
    cmd.ExecuteNonQuery();
}

也请参见:

在事件之外创建一个int变量。

private int iD;

然后将事件的访问级别从受保护更改为私有或公共,您甚至可以将其保留为空。

private void ListView2_ItemDataBound(object sender, 
ListViewItemEventArgs e)
{

    if (e.Item.ItemType == ListViewItemType.DataItem)
    {
        Label ID;
        ID = (Label)e.Item.FindControl("ID");
        ID.Font.Italic = true;
        this.iD  = Convert.ToInt32(ID)
        System.Data.DataRowView view = e.Item.DataItem as 
        System.Data.DataRowView;

    }
}

(我正在通过电话接听,希望您能原谅一半的答复和漫长的等待时间)