将sqldatareader绑定到gridview c#
我正在为我正在创建的asp.net类创建一个应用程序。 应用程序中的一个页面需要允许用户通过姓氏或用户ID搜索特定学生。 找到学生后,页面应显示学生数据和他/她的课程表。
除了class级时间表外,我已经完成了所有工作。 我采用的方法(正如我们在课堂上学到的)是通过SqlDataReader获取查询结果并将其绑定到GridView。 这是在showStudentSchedule()中完成的。
当我针对我创建的数据库测试它时,此函数中的查询返回正确的结果,但显示学生计划的网格视图不会显示在页面上。
//StudentInformation.aspx
//StudentInformation.aspx.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.SqlClient; public partial class StudentInformation : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { string userStr = TextBox1.Text; int userInt; bool isNum = int.TryParse(userStr, out userInt); string sqlSelectFindUserByName; if (isNum) sqlSelectFindUserByName = string.Format("SELECT LastName FROM Personal_Info JOIN Students ON Personal_Info.ID = Students.Student_ID WHERE Personal_Info.ID = '{0}'", userInt); else sqlSelectFindUserByName = string.Format("SELECT LastName FROM Personal_Info JOIN Students ON Personal_Info.ID = Students.Student_ID WHERE Personal_Info.LastName LIKE '%{0}%'", userStr); SqlConnection connection = new SqlConnection(); connection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; SqlCommand commandFindUserByName = new SqlCommand(sqlSelectFindUserByName, connection); connection.Open(); SqlDataReader readerFindUserByName = commandFindUserByName.ExecuteReader(); DropDownList1.Items.Clear(); DropDownList1.Items.Add("Please make a selection"); while (readerFindUserByName.Read()) DropDownList1.Items.Add(readerFindUserByName["LastName"].ToString()); if (DropDownList1.Items.Count == 2) DropDownList1.SelectedIndex = 1; DropDownList1_SelectedIndexChanged(null, null); connection.Close(); } protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { string nameLast = DropDownList1.SelectedItem.Value; displayStudent(nameLast); } private void displayStudent(String nameLast) { clearStudentLabel(); int userInt; bool isNum = int.TryParse(nameLast, out userInt); SqlConnection connection = new SqlConnection(); connection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; string sqlSelectFindUserInfoByName; sqlSelectFindUserInfoByName = string.Format("SELECT ID, FirstName, LastName, City, Phone FROM Personal_Info WHERE LastName LIKE '%{0}%'", nameLast); SqlCommand commandFindUserInfo = new SqlCommand(sqlSelectFindUserInfoByName, connection); connection.Open(); SqlDataReader readerFindUserInfo = commandFindUserInfo.ExecuteReader(); int i = 0; while (readerFindUserInfo.Read()) { Label1.Text = "Student ID: " + readerFindUserInfo["ID"].ToString(); Label2.Text = "First name: " + readerFindUserInfo["FirstName"].ToString(); Label3.Text = "Last name: " + readerFindUserInfo["LastName"].ToString(); Label4.Text = "City: " + readerFindUserInfo["City"].ToString(); Label5.Text = "Phone: " + readerFindUserInfo["Phone"].ToString(); } connection.Close(); showStudentSchedule(userInt); } private void showStudentSchedule(int id) { SqlConnection connection = new SqlConnection(); connection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; string sqlSelectFindUserInfoByName = string.Format("SELECT Class_Schedule.Section_ID, Class_Schedule.Course_ID, Class_Schedule.Days, Class_Schedule.Time, CASE WHEN Personal_Info.FirstName IS NULL THEN 'Staff' ELSE (Personal_Info.LastName + Personal_Info.FirstName) END AS Name FROM Class_Schedule JOIN Student_Enrollment ON Class_Schedule.Section_ID = Student_Enrollment.Section_ID JOIN Personal_Info ON Class_Schedule.Instructor_ID = Personal_Info.ID WHERE Student_Enrollment.Student_ID = {0}", id); SqlCommand commandFindUserInfo = new SqlCommand(sqlSelectFindUserInfoByName, connection); connection.Open(); SqlDataReader readerFindUserInfo = commandFindUserInfo.ExecuteReader(); GridView1.DataSource = readerFindUserInfo; GridView1.DataBind(); /* string connectionString = "Data Source=LocalHost;Initial Catalog=Northwind;Persist Security Info=True;User ID=sa;Password=sa_0001"; string commandString = "Select * from Customers"; SqlConnection conn = new SqlConnection(connectionString); SqlCommand command = new SqlCommand(commandString); conn.Open(); command.Connection = conn; SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection); GridView1.DataSource = reader; GridView1.DataBind(); */ } private void clearStudentLabel() { Label1.Text = ""; Label2.Text = ""; Label3.Text = ""; Label4.Text = ""; Label5.Text = ""; } }
试试这个:
上述就是C#学习教程:将sqldatareader绑定到gridview c#分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
SqlConnection connection = new SqlConnection(); connection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; SqlCommand command = new SqlCommand(sqlSelectFindUserByName); connection.Open(); command.Connection = connection; SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection); GridView1.DataSource = reader; GridView1.DataBind();
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1012152.html