WebForm1.aspx和WebForm1.aspx.cs代码都已贴 上WebForm1.aspx如下:
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="t.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<FONT face="宋体">
<asp:CheckBoxList id="ckbEmployees" style="Z-INDEX: 101; LEFT: 24px; POSITION: absolute; TOP: 32px"
RepeatLayout="Table" RepeatDirection="Vertical" RepeatColumns="3" CellPadding="9" CellSpacing="18"
TextAlign="Right" AutoPostBack="True" runat="server"></asp:CheckBoxList>
<asp:DataGrid id="dgEmployee" style="Z-INDEX: 102; LEFT: 16px; POSITION: absolute; TOP: 168px"
runat="server"></asp:DataGrid></FONT></form>
</body>
</HTML>
WebForm1.aspx.cs如下:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;namespace t
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid dgEmployee;
protected System.Web.UI.WebControls.CheckBoxList ckbEmployees;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!IsPostBack)
{
string strConnection="server=127.0.0.1;user id=sa;password=qiujun;database=Northwind;";
string strCommand="SELECT * FROM Employees ORDER BY LastName;";
SqlConnection Connection=new SqlConnection(strConnection);
SqlCommand Command=new SqlCommand(strCommand,Connection);
Connection.Open();
ckbEmployees.DataSource=Command.ExecuteReader();
ckbEmployees.DataTextField="LastName";
ckbEmployees.DataValueField="EmployeeID";
ckbEmployees.DataBind();
Connection.Close();
}
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{    
this.ckbEmployees.SelectedIndexChanged += new System.EventHandler(this.ListChange);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void ListChange(object sender, System.EventArgs e)
{
string strConnection="server=127.0.0.1;user id=sa;password=qiujun;database=Northwind;";
string strWhereClause="";
//ListItem liThisOne;
foreach(ListItem liThisOne in ckbEmployees.Items)
{
if(liThisOne.Selected)
strWhereClause+="EmployeeID="+liThisOne.Value+" OR ";
}
Response.Write("strWhereClause="+strWhereClause+"<hr/>");
if (strWhereClause.Length>0)
{
strWhereClause=strWhereClause.Substring(0,strWhereClause.Length-4);
strWhereClause="WHERE "+strWhereClause;
string strCommand="SELECT TitleOfCourtesy,FirstName,LastName,Country,Region,City,Notes FROM Employees"+strWhereClause;
SqlConnection Connection=new SqlConnection(strConnection);
SqlCommand Command=new SqlCommand(strCommand,Connection);
Connection.Open();
Response.Write("strCommand="+strCommand+"<hr/>");
dgEmployee.DataSource=Command.ExecuteReader();
dgEmployee.DataBind();
Connection.Close(); } }
}
}

解决方案 »

  1.   

    具体错在哪里我也不知道,大概是在数据库多项查询部分有错.
    第 1 行: '=' 附近有语法错误。 
    说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Data.SqlClient.SqlException: 第 1 行: '=' 附近有语法错误。源错误: 
    行 83:  Connection.Open();
    行 84:  Response.Write("strCommand="+strCommand+"<hr/>");
    行 85:  dgEmployee.DataSource=Command.ExecuteReader();
    行 86:  dgEmployee.DataBind();
    行 87:  Connection.Close();
     
      

  2.   

    怎么还没人帮忙?错误的地方大概是这里吧:
    private void ListChange(object sender, System.EventArgs e)
    {
    string strConnection="server=127.0.0.1;user id=sa;password=qiujun;database=Northwind;";
    string strWhereClause="";
    //ListItem liThisOne;
    foreach(ListItem liThisOne in ckbEmployees.Items)
    {
    if(liThisOne.Selected)
    strWhereClause+="EmployeeID="+liThisOne.Value+" OR ";
    }
    Response.Write("strWhereClause="+strWhereClause+"<hr/>");
    if (strWhereClause.Length>0)
    {
    strWhereClause=strWhereClause.Substring(0,strWhereClause.Length-4);
    strWhereClause="WHERE "+strWhereClause;
    string strCommand="SELECT TitleOfCourtesy,FirstName,LastName,Country,Region,City,Notes FROM Employees"+strWhereClause;
    SqlConnection Connection=new SqlConnection(strConnection);
    SqlCommand Command=new SqlCommand(strCommand,Connection);
    Connection.Open();
    Response.Write("strCommand="+strCommand+"<hr/>");
    dgEmployee.DataSource=Command.ExecuteReader();
    dgEmployee.DataBind();
    Connection.Close(); } }
      

  3.   

    string strCommand="SELECT TitleOfCourtesy,FirstName,LastName,Country,Region,City,Notes FROM Employees"+strWhereClause;这句有问题where子句前要加空格的