C# 自动回发:给定错误变量名已声明

C# 自动回发:给定错误变量名已声明,c#,asp.net,sql-server,telerik,C#,Asp.net,Sql Server,Telerik,我正在尝试使用asp c#.net从mssql数据库绑定telerik autocomplete文本框 <telerik:RadAutoCompleteBox AllowCustomEntry="true" ID="RadAutoCompleteBox2" runat="server" InputType="Text"> <TextSettings SelectionMode="Single" /> </telerik:RadAutoCompleteBox&

我正在尝试使用asp c#.net从mssql数据库绑定telerik autocomplete文本框

<telerik:RadAutoCompleteBox AllowCustomEntry="true" ID="RadAutoCompleteBox2" runat="server" InputType="Text">
    <TextSettings SelectionMode="Single" />
</telerik:RadAutoCompleteBox>
当我删除参数时,代码工作正常,但在参数行中,我仍然得到错误

已声明变量名@cityname。变量名在查询批处理或存储过程中必须是唯一的

我之所以出错,是因为当我按下任意键时,telerik autosuggesbox会一次又一次地自动回发和绑定数据

从标题上看,它看起来像是一部电影。但是一个简单的
if(!IsPostBack)
就可以了。您的问题是基本上您应该只在第一次执行此代码
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        LoadCity(Request.QueryString["city"].ToString());
    }

}

protected void LoadCity(string _city)
{
    try
    {
        SqlDataSource SqlDataSource1 = new SqlDataSource();
        SqlDataSource1.ID = "SqlDataSource1";
        this.Page.Controls.Add(SqlDataSource1);
        SqlDataSource1.ConnectionString = "my connection";
        SqlDataSource1.SelectCommand = "select citylocation from citylocationtbl where cityname=@cityname";
        SqlDataSource1.SelectParameters.Add("@cityname", _city);
        RadAutoCompleteBox2.DataSourceID = "SqlDataSource1";
        RadAutoCompleteBox2.DataTextField = "citylocation";
        RadAutoCompleteBox2.DataBind();
    }
    catch (Exception ex)
    {

    }

}
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        LoadCity(Request.QueryString["city"].ToString());
    }

}

protected void LoadCity(string _city)
{
    try
    {
        SqlDataSource SqlDataSource1 = new SqlDataSource();
        SqlDataSource1.ID = "SqlDataSource1";
        this.Page.Controls.Add(SqlDataSource1);
        SqlDataSource1.ConnectionString = "my connection";
        SqlDataSource1.SelectCommand = "select citylocation from citylocationtbl where cityname=@cityname";
        SqlDataSource1.SelectParameters.Add("@cityname", _city);
        RadAutoCompleteBox2.DataSourceID = "SqlDataSource1";
        RadAutoCompleteBox2.DataTextField = "citylocation";
        RadAutoCompleteBox2.DataBind();
    }
    catch (Exception ex)
    {

    }

}