从数据库中选择数据并将其显示到gridview [英] selecting data from database and showingg it to gridview
本文介绍了从数据库中选择数据并将其显示到gridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
{
OleDbConnection cn = new OleDbConnection();
cn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=E:\placement cell\project.mdb";
cn.Open();
string sql1 = "select name from student where 10thper>=''" + DropDownList3.Text + "''AND 12thper>=''" + DropDownList4 + "''AND aggregate>=''" + DropDownList5 + "''AND backlog<=''" + DropDownList6 + "''AND academicgap<=''" + DropDownList7 + "''";
System.Data.OleDb.OleDbCommand cmd1 = new System.Data.OleDb.OleDbCommand(sql1, cn);
System.Data.OleDb.OleDbDataAdapter adp = new OleDbDataAdapter();
DataSet ds = new DataSet();
adp.SelectCommand = cmd1;
adp.Fill(ds);
DataTable dt = ds.Tables[0];
OleDbDataReader dr = cmd1.ExecuteReader();
while (dr.Read() == true)
{
GridView1.DataSource = dt;
}
cn.Close();
}
这是代码.
错误是
this is the code.
the error is
Server Error in ''/placement cell'' Application.<br />
Syntax error (missing operator) in query expression ''10thper>=''55''AND 12thper>=''System.Web.UI.WebControls.DropDownList''AND aggregate>=''System.Web.UI.WebControls.DropDownList''AND backlog<=''System.Web.UI.WebControls.DropDownList''AND academicgap<=''System.Web.UI.WebControls.DropDownList''''.<br />
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.<br />
<br />
Exception Details: System.Data.OleDb.OleDbException: Syntax error (missing operator) in query expression ''10thper>=''55''AND 12thper>=''System.Web.UI.WebControls.DropDownList''AND aggregate>=''System.Web.UI.WebControls.DropDownList''AND backlog<=''System.Web.UI.WebControls.DropDownList''AND academicgap<=''System.Web.UI.WebControls.DropDownList''''.<br />
<br />
Source Error:<br />
<br />
Line 146: DataSet ds = new DataSet();<br />
Line 147: adp.SelectCommand = cmd1;<br />
Line 148: adp.Fill(ds);<br />
Line 149: DataTable dt = ds.Tables[0];<br />
Line 150: OleDbDataReader dr = cmd1.ExecuteReader();<br />
<br />
Source File: e:\placement cell\recsearch.aspx.cs Line: 148
推荐答案
给个space before all AND operator
.
string sql1 = "select name from student where 10thper>=''" + DropDownList3.Text + "'' AND 12thper>=''" + DropDownList4 + "'' AND aggregate>=''" + DropDownList5 + "'' AND backlog<=''" + DropDownList6 + "'' AND academicgap<=''" + DropDownList7 + "''";
您需要在''和AND之间留一个空格("''AND aggregate>
"应为"'' AND aggregate
")
请注意,在查询中,您正在直接与下拉列表进行比较,例如2thper> =''System.Web.UI.WebControls.DropDownList''对我来说似乎是错误的.您应该将内容内部下拉列表进行比较.
You need to give a space between '' and AND ("''AND aggregate>
" should be "'' AND aggregate
")
A note, in your query, you are doing comparisons directly to the dropdownlist e.g. 2thper>=''System.Web.UI.WebControls.DropDownList'' which to me appears to be wrong. You should compare to the content inside the drop down list.
主要是语法错误.请按以下方式重写查询,
Hi,
Mostly syntax error.Please rewrite the query as follows,
{
OleDbConnection cn = new OleDbConnection();
cn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=E:\placement cell\project.mdb";
cn.Open();
string sql1 = "select name from student where 10thper>=''" + DropDownList3.SelectedValue + "'' AND 12thper>=''" + DropDownList4.SelectedValue + "'' AND aggregate>=''" + DropDownList5.SelectedValue + "''AND backlog<=''" + DropDownList6.SelectedValue + "'' AND academicgap<=''" + DropDownList7.SelectedValue + "''";
System.Data.OleDb.OleDbCommand cmd1 = new System.Data.OleDb.OleDbCommand(sql1, cn);
System.Data.OleDb.OleDbDataAdapter adp = new OleDbDataAdapter();
DataSet ds = new DataSet();
adp.SelectCommand = cmd1;
adp.Fill(ds);
DataTable dt = ds.Tables[0];
OleDbDataReader dr = cmd1.ExecuteReader();
while (dr.Read() == true)
{
GridView1.DataSource = dt;
}
cn.Close();
}
希望这会有所帮助.
Hope this will help.
这篇关于从数据库中选择数据并将其显示到gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文