用的是系统数据库:pubs。表是:sales,stores
要实现的是对结果进行过滤:
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
string ConnectionString=ConfigurationSettings.AppSettings["MSDEconnectString"];
SqlConnection myconnection=new SqlConnection(ConnectionString);
try
{
string CommandText="select stor_id,stor_name from stores";
SqlCommand myCommand=new SqlCommand(CommandText,myconnection);
myconnection.Open();
DropDownList1.DataSource=myCommand.ExecuteReader();
DropDownList1.DataTextField="stor_name";
DropDownList1.DataValueField="stor_id";
DropDownList1.DataBind();
DropDownList1.Items.Insert(0,new ListItem("--all stor_name--","0"));
}
catch(Exception ex)
{
throw(ex);
}
finally
{
myconnection.Close(); }
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string ConnectionString = ConfigurationSettings.AppSettings["MSDEconnectString"];
SqlConnection connstr = new SqlConnection(ConnectionString);
try
{
string cmdtext = "select sales.title_id,stores.city from sales INNER JOIN stores on sales.stor_id=stores.stor_id";
string value=DropDownList1.SelectedValue;
if(value!="0")
{
cmdtext+="where sales.stor_id="+value;
cmdtext+="order by sales.title_id";
}
SqlCommand cmd2 = new SqlCommand(cmdtext, connstr);
connstr.Open();
GridView1.DataSource = cmd2.ExecuteReader();
GridView1.DataBind();
}
catch(Exception es)
{
throw (es);
}
finally
{
connstr.Close();
}
}报错:第一行"sales"附近有语法错误!
为什么?
要实现的是对结果进行过滤:
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
string ConnectionString=ConfigurationSettings.AppSettings["MSDEconnectString"];
SqlConnection myconnection=new SqlConnection(ConnectionString);
try
{
string CommandText="select stor_id,stor_name from stores";
SqlCommand myCommand=new SqlCommand(CommandText,myconnection);
myconnection.Open();
DropDownList1.DataSource=myCommand.ExecuteReader();
DropDownList1.DataTextField="stor_name";
DropDownList1.DataValueField="stor_id";
DropDownList1.DataBind();
DropDownList1.Items.Insert(0,new ListItem("--all stor_name--","0"));
}
catch(Exception ex)
{
throw(ex);
}
finally
{
myconnection.Close(); }
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string ConnectionString = ConfigurationSettings.AppSettings["MSDEconnectString"];
SqlConnection connstr = new SqlConnection(ConnectionString);
try
{
string cmdtext = "select sales.title_id,stores.city from sales INNER JOIN stores on sales.stor_id=stores.stor_id";
string value=DropDownList1.SelectedValue;
if(value!="0")
{
cmdtext+="where sales.stor_id="+value;
cmdtext+="order by sales.title_id";
}
SqlCommand cmd2 = new SqlCommand(cmdtext, connstr);
connstr.Open();
GridView1.DataSource = cmd2.ExecuteReader();
GridView1.DataBind();
}
catch(Exception es)
{
throw (es);
}
finally
{
connstr.Close();
}
}报错:第一行"sales"附近有语法错误!
为什么?
解决方案 »
- aspx 中在 div下动态添加html元素
- .net 当中的datagrid问题!!!!!
- 网址形如 http://test.com/search.dll 的疑问
- 关于Url转向问题!求助
- 小小的登陆验证问题(I AM 菜鸟)
- 求框架集问题,等待.
- 谁有asp。net+xml的 电子书籍 100分送 急!!!!
- 只有10分了,诚心求教
- 错误信息:“CrystalReportViewer”在命名空间“CrystalDecisions.Web”中不明确”怎么产生的
- socket通讯
- 只有50分,都给了(关于从Excel中将数据导入页面中)
- 微软的那个TREEVIEW好像没有SELECTED属性啊?怎么才能给选定的节点标记为已选择?
第一个sql语句如果表名没有错应该没问题