读书人

GridView出错!(菜鸟希望大家不要笑

发布时间: 2012-02-01 16:58:19 作者: rapoo

GridView出错!(初学者,希望大家不要笑话)
运行以后,在zonghe1.aspx.cs里面出错,提示:

用户代码未处理 SqlException
第1行: '> '附近有语法错误。

应该是zonghe.aspx.cs里面Button3代码里面有出错吧,但是现在还没找到!
相关代码如下:
public partial class zonghe1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string showCols = Request[ "showCols "];
string totalQuery = Request[ "totalQuery "];
Label1.Text = "查询结果 " + totalQuery;
//通过DataView控件显示符合查询条件的数据
DataSet ds=new DataSet();
SqlConnection myConnection = new SqlConnection( "server=localhost;uid=sa;pwd=860712;database=Northwind ");
string strSQL = "select " + showCols + "from [Order Details] where " + totalQuery+ " ";
SqlDataAdapter myCommand = new SqlDataAdapter(strSQL, myConnection);
myCommand.Fill(ds, "OrderDetails1 ");//问题就指示在这里
if (ds.Tables[ "OrderDetails1 "].Rows.Count != 0)
{
GridView1.DataSource = ds.Tables[ "OrderDetails1 "].DefaultView;
GridView1.DataBind();
}
else
{
Label1.Text = "没有符合查询条件的数据! ";
}
}
}

相关“第1行: '> '附近有语法错误”的代码:
protected void Button3_Click(object sender, EventArgs e)
{
string totalQuery= " ";//存放所有的查询条件
string showCols= " ";//存放要显示的所有字段
//获取列表框中的所有查询条件
if(ListBox1.Items.Count==0)
{
Response.Write( " <script language= 'javascript '> alert( '请设定查询条件! ') </script> ");
}
for(int i=0;i <ListBox1.Items.Count;i++)


{
if(ListBox1.Items[i].Text.StartsWith( " and "))
{
ListBox1.Items[i].Text=ListBox1.Items[0].Text.Substring(5);
}
if(ListBox1.Items[i].Text.StartsWith( " or "))
{
ListBox1.Items[i].Text=ListBox1.Items[0].Text.Substring(4);
}
totalQuery+=ListBox1.Items[i].Text.ToString();
}
//获取复选框列表的所有被选择的字段
if(CheckBoxList1.SelectedIndex> -1)
{
for(int j=0;j <CheckBoxList1.Items.Count;j++)
{
if(CheckBoxList1.Items[j].Selected==true)
{
showCols+=CheckBoxList1.Items[j].Text.ToString()+ ", ";
}
}
}
else
{
Response.Write( " <script language= 'javascript '> alert( '请选择要显示字段的名称! ') </script> ");
}
showCols= " "+showCols.TrimEnd( ', ')+ " ";
//将查询结果通过另一个文件显示
Response.Redirect( "zonghe1.aspx?showCols= "+showCols+ "&totalQuery= "+totalQuery);
}


[解决办法]
改为:
protected void Button3_Click(object sender, EventArgs e)
{
string totalQuery= " ";//存放所有的查询条件
string showCols= " ";//存放要显示的所有字段


//获取列表框中的所有查询条件
if(ListBox1.Items.Count==0)
{
Response.Write( " <script language= 'javascript '> alert( '请设定查询条件! ') </script> ");
}
for(int i=0;i <ListBox1.Items.Count;i++)
{
if(ListBox1.Items[i].Text.StartsWith( " and "))
{
ListBox1.Items[i].Text=ListBox1.Items[i].Text.Substring(5);
}
if(ListBox1.Items[i].Text.StartsWith( " or "))
{
ListBox1.Items[i].Text=ListBox1.Items[i].Text.Substring(4);
}
totalQuery+=ListBox1.Items[i].Text.ToString();
}
//获取复选框列表的所有被选择的字段
if(CheckBoxList1.SelectedIndex> -1)
{
for(int j=0;j <CheckBoxList1.Items.Count;j++)
{
if(CheckBoxList1.Items[j].Selected==true)
{
showCols+=CheckBoxList1.Items[j].Text.ToString()+ ", ";
}
}
}
else
{
Response.Write( " <script language= 'javascript '> alert( '请选择要显示字段的名称! ') </script> ");
}
showCols= " "+showCols.TrimEnd( ', ')+ " ";
//将查询结果通过另一个文件显示
Response.Redirect( "zonghe1.aspx?showCols= "+showCols+ "&totalQuery= "+totalQuery);
}

读书人网 >C#

热点推荐