2 nananihaoya nananihaoya 于 2016.09.14 12:51 提问

用Gride view控件实现信息的查询,运行没出错,就是点击查询没反应,大神,帮帮忙!急急急

using System.Data.SqlClient;

public partial class studentinfoM : System.Web.UI.Page
{
//SqlConnection conn;
//SqlDataAdapter da;

protected void Page_Load(object sender, EventArgs e)
{
string str = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ms.mdf;Integrated Security=True;User Instance=True";
SqlConnection conn = new SqlConnection(str);
conn.Open();
SqlDataAdapter da = new SqlDataAdapter();
string SelectSql = "select * from student";
da.SelectCommand = new SqlCommand(SelectSql, conn);
DataSet ds = new DataSet();
da.Fill(ds);
conn.Close();
GridView1.DataSource = ds;
GridView1.DataBind();

}
protected void LinkButton1_Click(object sender, EventArgs e)
{
Response.Redirect("addstudent.aspx");
}
protected void Button2_Click(object sender, EventArgs e)
{
Response.Redirect("studentlist.aspx");
}

protected void Button1_Click1(object sender, EventArgs e)
{
string str = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ms.mdf;Integrated Security=True;User Instance=True";
SqlConnection conn = new SqlConnection(str);
conn.Open();
SqlDataAdapter da = new SqlDataAdapter();
string SelectSql = "";
switch (MySelect.SelectedIndex)
{
case 0:
SelectSql = "select * from student where classID like '%" + TextBox1.Text + "%'";
break;
case 1:
SelectSql = "select * from student where studentID like '%" + TextBox1.Text + "%'";
break;
case 2:
SelectSql = "select * from student where studentName like '%" + TextBox1.Text + "%'";
break;
}
da.SelectCommand = new SqlCommand(SelectSql, conn);
DataSet ds = new DataSet();
da.Fill(ds);
conn.Close();
GridView1.DataSource = ds;图片说明
GridView1.DataBind();
}

1个回答

fleetingevent
fleetingevent   2016.09.14 13:27
已采纳

既然原本能显示,只是查询问题,我觉得可能有两个问题呀,一个就是case0,1,2下面的sql语句可能有问题,你可以拿去数据库试一下能不能查询。
还有可能就是你在switch (MySelect.SelectedIndex)这里设置一个断点,监控一下MySelect.SelectedIndex,看一下具体是不是按照你的选择为0,1,2

我是这么理解的,来自一个同样初学的人。

Csdn user default icon
上传中...
上传图片
插入图片