如何在页面加载时从表格到gridview显示记录,还使用textbox vb.net搜索特定字段的记录?

时间:2011-01-23 07:48:28

标签: asp.net vb.net visual-studio-2008 sql-server-2005

我的数据库:table1

ID             FIRSTNAME              AGE
1              Sumit                  22
2              Sanjeev                23

我有gridview 1和textbox1和button1

我希望当页面加载gridview时显示来自table1的所有记录,并且... ...想通过在textbox1中输入sumit搜索记录firstname,然后单击button1然后在gridview中显示sumit记录。默认情况下gridview显示来自table1的alll记录

怎么做?

我的Selct查询如下:但它不显示所有记录...但如果您搜索特定记录,它可以显示记录......

SELECT a1_admins.EmployeeId, a1_admins.Firstname, a1_admins.Lastname, a1_admins.Email
, a1_admins.City, a1_admins.State, a1_admins.Country, aspnet_Membership.LastLoginDate
, aspnet_Membership.CreateDate, a1_admins.Password, a1_admins.pan_no, a1_admins.Contactno
, a1_admins.Address 
FROM a1_admins 
INNER JOIN aspnet_Membership 
ON a1_admins.UserId = aspnet_Membership.UserId 
WHERE (a1_admins.EmployeeId LIKE N'%' + @EmployeeId + N'%') 
    OR (a1_admins.City LIKE N'%' + @City + N'%') 
    OR (a1_admins.State LIKE N'%' + @State + N'%') 
    OR (a1_admins.Country LIKE N'%' + @Country + N'%')

1 个答案:

答案 0 :(得分:0)

我假设未过滤时变量设置为NULL? 这会导致LIKE失败,因为X LIKE NULL不可能是真的。

SELECT
  a1_admins.EmployeeId, a1_admins.Firstname, a1_admins.Lastname, a1_admins.Email, 
  a1_admins.City, a1_admins.State, a1_admins.Country, aspnet_Membership.LastLoginDate, 
  aspnet_Membership.CreateDate, a1_admins.Password, a1_admins.pan_no, a1_admins.Contactno, a1_admins.Address 
FROM a1_admins 
INNER JOIN aspnet_Membership ON a1_admins.UserId = aspnet_Membership.UserId 
WHERE (a1_admins.EmployeeId LIKE N'%' + @EmployeeId + N'%') 
   OR (a1_admins.City LIKE N'%' + @City + N'%') 
   OR (a1_admins.State LIKE N'%' + @State + N'%') 
   OR (a1_admins.Country LIKE N'%' + @Country + N'%')
   OR (Coalesce(@employeeid,@city,@state,@country) is null)

您可以添加最后一个子句,如果未设置所有过滤器,则显示所有内容。