时间段筛选 出现“无法绑定由多个部分组成的标识符”
test2.aspx.cs代码:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class test2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
//连接SQL SERVER
string ConnSqlServer = "Data Source=.;Initial Catalog=Northwind;Integrated Security=True;User ID=***; pwd=***";
SqlConnection Conn = new SqlConnection(ConnSqlServer);
//打开数据库
Conn .Open();
//获取客户名称和联系人名称
string OrderID = tbOrderID.Text.Trim();
string CustomerID = tbCustomerID.Text.Trim();
string EmployeeID = tbEmployeeID.Text.Trim();
string ShipCity = tbShipCity.Text.Trim();
string dt1 = tbShippedDateBegin.ToString ();
string dt2 = tbShippedDateEnd.ToString();
//声明查询语句变量
string OrderIDSql = "";
string CustomerIDSql = "";
string EmployeeIDSql = "";
string ShipCitySql = "";
//string dt1Sql = "";
//string dt2Sql = "";
string dtSql = "";
string OrderSql = "";
//如果OrderID不为空,生成OrderSql语句
if (OrderID != "")
OrderIDSql = " AND OrderID LIKE '%" + OrderID + "%'";
//如果CustomerID不为空,生成CustomerIDSql语句
if (CustomerID != "")
CustomerIDSql = " AND CustomerID LIKE '%" + CustomerID + "%'";
////如果EmployeeID不为空,生成EmployeeIDSql语句
if (EmployeeID != "")
EmployeeIDSql = " AND EmployeeID LIKE '%" + EmployeeID + "%'";
////如果ShipCityID不为空,生成ShipCitySql语句
if (ShipCity != "")
ShipCitySql = " AND ShipCity LIKE '%" + ShipCity + "%'";
////如果ShippedDate不为空,生成dt1Sql语句
if (dt1 != "" || dt2 !="")
dtSql = " AND ShippedDate BETWEEN " + dt1 +" AND "+ dt2 +" ";
//综合查询语句
OrderSql = "SELECT * FROM Orders WHERE OrderID<>''" + OrderIDSql + CustomerIDSql + EmployeeIDSql + ShipCitySql + dtSql;
//填充DataSet
SqlDataAdapter Adapter = new SqlDataAdapter(OrderSql, Conn);
DataSet Ds = new DataSet();
Adapter.Fill(Ds, "Orders");
//执行命令
gvClient.DataSource = Ds.Tables["Orders"];
gvClient.DataBind();
//关闭数据库
Conn.Close();
}
}
test.aspx代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test2.aspx.cs" Inherits="test2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1