使用Repeater的ASP.net SQL查询问题

时间:2011-04-19 17:42:15

标签: asp.net sql sql-server-2005 repeater

这是我在存储过程中的查询。我正在使用转发器来显示信息。问题是如果我搜索让我们说

  

Ename = Jim
ELocation = Smith Center
  ECity =亚特兰大

通过查询构建器,我得到了两个匹配的结果。但是当我将数据源绑定到转发器并添加参数时,请尝试运行查询,我的转发器为空。变量从Textbox Controls传入。此外,如果我只传入一个变量,比如标题,它就可以了。但是当我尝试传入两个或更多变量时,我什么也得不到。任何人对如何做有任何想法?

    @title varchar(150),
    @venue varchar(150),
    @city varchar(100),
    @state varchar(50),
    @country varchar(100),
    @desc varchar(150),
    @date smalldatetime = null
AS

SELECT     EID, EName, EDate, EDEnd, ELocation, ECity, EState, EDesc, EWebsite
FROM         esc
WHERE     (@title IS NULL OR EName LIKE '%' + @title + '%') 
           AND (@venue IS NULL OR ELocation LIKE '%' + @venue + '%')
           AND (@city IS NULL OR ECity LIKE '%' + @city + '%') 
           AND (@state IS NULL OR EState LIKE '%' + @state + '%') 
           AND (@country IS NULL OR ECountry = @country) 
           AND (@desc IS NULL OR EDesc LIKE '%' + @desc + '%') 
           AND (@date IS NULL OR EDate = @date)

单击按钮时的压缩代码。:

        SqlConnection conn = null;
        try
        {
            conn = new SqlConnection("");
            SqlCommand command = new SqlCommand();
            command.Connection = conn;

            command.CommandText = "seesc";
            command.CommandType = CommandType.StoredProcedure;

            SqlParameter title = new SqlParameter();
            title.ParameterName = "@title";
            title.SqlDbType = SqlDbType.VarChar;
            title.Direction = ParameterDirection.Input;
            title.Value = TitleTextBox.Text;

            //other parameters declared here

                command.Parameters.Add(title);
                //other parameters added here

                conn.Open();
                SqlDataReader reader = command.ExecuteReader();
                if (reader.HasRows)
                {
                    evrep.DataSource = reader;
                    evrep.DataBind();
                }
        }
        catch { }

1 个答案:

答案 0 :(得分:0)

查询本身有一些拼写错误。

       AND (@city IS NULL OR ECity LIKE '+' + @city + '%') 
       AND (@state IS NULL OR EState LIKE '+' + @state + '%')

应该是

       AND (@city IS NULL OR ECity LIKE '%' + @city + '%') 
       AND (@state IS NULL OR EState LIKE '%' + @state + '%')

另外

       title.Value = TitleTextBox.Text;

使用参数不为null但是具有空字符串的值。这将使条件失败

       AND (@country IS NULL OR ECountry = @country)