Gridview分页问题

时间:2012-08-11 03:33:51

标签: c# asp.net gridview

我有一个问题让我堆叠,我无法找到任何解决方案,非常需要你的帮助:实际上我需要在员工表上进行某种搜索,就像我需要搜索所有员工一样是女性还是有博士等等,我为此创建了一个函数,使用if条件查询employee表,这个搜索的结果出现在gridview中,allowpaging为true,问题是每当我点击下一步转到其他页面时在gridview中它做了一个完整的回发并再次绑定到gridview数据源,因为我给gridview数据源下面的函数(DetailedSearch)if if cluases当它进入函数时它开始用第一个if条件读取而不去那个特定的if条件,然后抛出异常对象引用未设置为对象的实例

这是我的代码(它只是它的一部分而不是完整的代码):

    public DataTable DetailedSearch()
    {
        con.Open();
        SqlDataAdapter da=new SqlDataAdapter();


         if (FirstName != string.Empty)
         {
             if (FirstName != string.Empty && LastName != "--Letters--")
             {
                 da = new SqlDataAdapter("select * from employee where firstname ='" + FirstName.ToString() + "'"+"and lastname like '"+LastName.ToString()+"%'", con);
             }
             else if(FirstName!=string.Empty && Gender!="{Please Select}")
             {
                 da = new SqlDataAdapter("select * from employee where firstname ='" + FirstName.ToString() + "'" + "and gender='" + Gender.ToString() + "'", con);

             }
             else if(FirstName!=string.Empty && MaritalStatus!="{Please Select}")
             {
                 da = new SqlDataAdapter("select * from employee where firstname ='" + FirstName.ToString() + "'" + "and maritalstatus='" + MaritalStatus.ToString() + "'", con);
             }
             else if(FirstName!=string.Empty && Qualification!="{Please Select}")
             {
                 da = new SqlDataAdapter("select * from employee where firstname ='" + FirstName.ToString() + "'" + "and qualification='" + Qualification.ToString() + "'", con);
             }
             else if(FirstName!=string.Empty && GraduationDate!=DateTime.MinValue)
             {
                 da = new SqlDataAdapter("select * from employee where firstname ='" + FirstName.ToString() + "'" + "and graduationdate='" + GraduationDate.ToString() + "'", con);
             }
             else if(FirstName!=string.Empty && Province!="{Please Select}")
             {
                 da = new SqlDataAdapter("select * from employee where firstname ='" + FirstName.ToString() + "'" + "and province='" + Province.ToString() + "'", con);
             }
             else if(FirstName!=string.Empty && LastEmployer!=string.Empty)
             {
                 da = new SqlDataAdapter("select * from employee where firstname ='" + FirstName.ToString() + "'" + "and lastemployer='" + LastEmployer.ToString() + "'", con);
             }
            else if(FirstName!=string.Empty && EnteredBy!="{Please Select}")
             {
                 da = new SqlDataAdapter("select * from employee where firstname ='" + FirstName.ToString() + "'" + "and enteredby='" + EnteredBy.ToString() + "'", con);
             }
             else if (FirstName != string.Empty && DateEntered != DateTime.MinValue)
             {
                 da = new SqlDataAdapter("select * from employee where firstname ='" + FirstName.ToString() + "'" + "and dateentered='" + DateEntered.ToString() + "'", con);
             }
             else
             {
                 da = new SqlDataAdapter("select * from employee where firstname ='" + FirstName.ToString() + "'", con);
             }
         }
         else if (LastName != "--Letters--")
         {
                if (LastName != "--Letters--" && Province != "{Please Select}")
                 {
                     da = new SqlDataAdapter("select * from employee where lastname like'" + LastName.ToString() + "%'" + "and province='" + Province.ToString() + "'", con);
                 }
                else if (Gender != "{Please Select}" && LastName != "--Letters--")
                {
                    da = new SqlDataAdapter("select * from employee where gender='" + Gender.ToString() + "'" + "and lastname like '" + LastName.ToString() + "%'", con);
                }
        DataTable dt = new DataTable();
        da.Fill(dt);
        con.Close();
        return dt;

    }

请帮我做什么,因为我总是空白,没有逻辑出现在我脑海中,我必须把if子句作为查询可能不是基于搜索相同,但如何使gridview分页意识到这一点:(

提前致谢

1 个答案:

答案 0 :(得分:0)

真的,在那段代码中有很多东西可以改进......

  1. 你反复做FirstName != string.Empty,但你已经 在第一个if中进行了测试。
  2. 您应该更改为一个if(!String.IsNullOrEmpty(FirstName))
  3. 您应该使用sql parameters而不是字符串连接,因为这可以避免潜在的SQL注入
  4. 您不应该传递字段的默认用户界面,例如“请选择”。如果 选择默认值,传递空值或空值
  5. 虽然这没有具体回答或问题,但这是一个很好的指导 - 实际上,在您尝试应用这些更改的情况下,它甚至可能在此过程中解决您的问题