mydr = mycom.ExecuteReader(); 标准表达式中数据类型不匹配。
using System;using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;
namespace WebApplication1
{
public partial class ddlist : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.Label1.Text = "";
if (!Page.IsPostBack)
{
//if (Session["username"] == null)
//{
// Session.RemoveAll();
// Response.Redirect("Login.aspx");
//}
//if (Session["姓名"] != null & (Session["quanxian"].ToString() == "1" || Session["quanxian"].ToString() == "9"))
//{
OleDbConnection myconn;
OleDbCommand mycom;
OleDbDataReader mydr;
this.DropDownList1.Items.Clear();
this.DropDownList2.Items.Clear();
myconn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; " + "Data Source=" + Server.MapPath("App_Data/db1.mdb"));
mycom = new OleDbCommand("select 所在院系 from luquxinxi ", myconn);
myconn.Open();
mydr = mycom.ExecuteReader();
this.DropDownList1.Items.Add("-请选择-");
while (mydr.Read())
{
this.DropDownList1.Items.Add(mydr["所在院系"].ToString());
}
mydr.Close();
mydr.Dispose();
mycom.Dispose();
myconn.Close();
myconn.Dispose();
//}
//else
//{
// Session.RemoveAll();
// Response.Redirect("Login.aspx");
//}
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
//this.TextBox1.Text = "";
//this.TextBox2.Text = "";
//this.TextBox3.Text = "";
//this.TextBox4.Text = "";
//this.TextBox5.Text = "";
//this.DropDownList3.Text = "";
//this.TextBox6.Text = "";
//this.TextBox8.Text = "";
//this.Label12.Text = "";
//this.TextBox9.Text = "";
//this.DropDownList2.AutoPostBack = true;
OleDbConnection myconn;
OleDbCommand mycom;
OleDbDataReader mydr;
this.DropDownList2.Items.Clear();
this.DropDownList2.Items.Add("-请选择-");
myconn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; " + "Data Source=" + Server.MapPath("App_Data/db1.mdb"));
mycom = new OleDbCommand("select 学号,姓名 from gaokaochengjixinx where 所在院系='" + this.DropDownList1.Text + "'", myconn);
myconn.Open();
mydr = mycom.ExecuteReader();
while (mydr.Read())
{
this.DropDownList2.Items.Add(mydr["学号"].ToString().Trim() + " " + mydr["姓名"].ToString().Trim());
}
mydr.Close();
mydr.Dispose();
mycom.Dispose();
myconn.Close();
myconn.Dispose();
//this.DropDownList2.AutoPostBack = false;
}
不知道哪里错了,老师给的代码,我只是改了一下中文。。 --------------------编程问答-------------------- this.DropDownList1.Items.Add(mydr["所在院系"].ToString());
this.DropDownList2.Items.Add(mydr["学号"].ToString().Trim() + " " + mydr["姓名"].ToString().Trim());
将这两个修改成:
this.DropDownList1.Items.Add(mydr.GetString(0));
this.DropDownList2.Items.Add(mydr.GetString(0) + " " + mydr.GetString(1));
再试试。
--------------------编程问答-------------------- 不行,他说指令转换无效。。。 --------------------编程问答-------------------- 你先调试,看看mydr.GetString(0) 能获得数据吗,是什么类型。 --------------------编程问答-------------------- 那就直接用
this.DropDownList1.Items.Add(mydr[0]);
this.DropDownList2.Items.Add(mydr[0] + " " + mydr[1]);
这样试试。
--------------------编程问答-------------------- 有效了,不过问题还是在,改不了。。会不会是数据库的问题。。
补充:.NET技术 , ASP.NET