更新时间:2022-10-21 10:37:14
检查你的表'CoursesTable'是否有'CourseName'列或有任何拼写错误。
在运行应用程序之前,尝试使用SQL IDE中的必需参数详细信息执行以下查询,并确保结果具有预期的列。Quote:GetData(选择CourseRegistration.CourseCode,CoursesTable.CourseName,CoursesTable.CourseUnit from(DepartmentTable内部连接CoursesTable,取自DepartmentTable.DepartmentID = CoursesTable) .DepartmentID)内部联接CourseRegistration on CoursesTable.CourseCode = CourseRegistration.CourseCode,其中MatricNo ='+ matricno +');
添加两个NameSpace
使用system.data;
使用system.data.sqlclient;
然后写b elow code in youe(!Ispostback)功能if ( !IsPostBack)
{
SqlDataAdapter ad = new SqlDataAdapter( select CourseRegistration.CourseCode,CoursesTable。 CourseName,CoursesTable.CourseUnit from(DepartmentTable内部连接CoursesTable on DepartmentTable.DepartmentID = CoursesTable.DepartmentID)内部连接CourseRegistration on CoursesTable.CourseCode = CourseRegistration.CourseCode其中MatricNo =' + matricno + ,cn);
DataSet ds = new DataSet();
ad.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
更改BoundFields
,如下所示......< asp:BoundField DataField = CourseCode HeaderText = ID 可见 = false / >
< asp:BoundField DataField = CourseCode HeaderText = 课程代码 / >
< asp:BoundField DataField = CourseName HeaderText = CourseName / >
< asp:BoundField DataField = CourseUnit HeaderText = 课程单元 / >
试试这个并告诉我。
this code below gives me an error
DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'CourseName'.
am hoping to get a solution to it thanks
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.SqlClient;
using System.Configuration;
public partial class CourseRegistration : System.Web.UI.Page
{
Connector newconnect = new Connector();
protected void Page_Load(object sender, EventArgs e)
{
if (Session["Student"] != null)
{
try
{
string matricno = Session["Student"].ToString();
if (!IsPostBack)
{
GridView1.DataSource = GetData("select CourseRegistration.CourseCode, CoursesTable.CourseName, CoursesTable.CourseUnit from (DepartmentTable inner join CoursesTable on DepartmentTable.DepartmentID=CoursesTable.DepartmentID) inner join CourseRegistration on CoursesTable.CourseCode = CourseRegistration.CourseCode where MatricNo = '" + matricno + "'");
GridView1.DataBind();
}
}
catch (Exception ex)
{
Label1.Text = ex.Message;
}
}
}
private DataSet GetData(string query)
{
string matricno = Session["Student"].ToString();
string conString = ConfigurationManager.ConnectionStrings["OnlineExamination_String"].ConnectionString;
SqlCommand cmd = new SqlCommand(query);
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataSet ds = new DataSet())
{
sda.Fill(ds);
return ds;
}
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string matricno = Session["Student"].ToString();
SqlCommand command = new SqlCommand();
command = newconnect.HelpCommand("CourseChecker");
command.Parameters.AddWithValue("@MatricNo", matricno);
command.Parameters.AddWithValue("@CourseCode", DropDownList3.SelectedValue);
SqlDataReader rv = command.ExecuteReader();
if (rv.HasRows == false)
{
try
{
SqlCommand commander = new SqlCommand();
commander = newconnect.HelpCommand("CourseRegInsert");
commander.Parameters.AddWithValue("@MatricNo", matricno);
commander.Parameters.AddWithValue("@CourseCode", DropDownList3.SelectedValue);
commander.Parameters.AddWithValue("@Session", DropDownList4.SelectedValue);
commander.ExecuteNonQuery();
}
catch (Exception ex)
{
Label1.Text = ex.Message;
}
}
else
{
}
}
}
Check whether your table 'CoursesTable' has 'CourseName' column or has any spelling mistake.
Try executing the following query with required parameter details in SQL IDE before running the application and ensure that result has expected columns.Quote:GetData("select CourseRegistration.CourseCode, CoursesTable.CourseName, CoursesTable.CourseUnit from (DepartmentTable inner join CoursesTable on DepartmentTable.DepartmentID=CoursesTable.DepartmentID) inner join CourseRegistration on CoursesTable.CourseCode = CourseRegistration.CourseCode where MatricNo = '" + matricno + "'");
Add Two NameSpace
Using system.data;
using system.data.sqlclient;
Then write below code in to youe (!Ispostback) Functionif (!IsPostBack) { SqlDataAdapter ad = new SqlDataAdapter("select CourseRegistration.CourseCode, CoursesTable.CourseName, CoursesTable.CourseUnit from (DepartmentTable inner join CoursesTable on DepartmentTable.DepartmentID=CoursesTable.DepartmentID) inner join CourseRegistration on CoursesTable.CourseCode = CourseRegistration.CourseCode where MatricNo = '" + matricno + "'",cn); DataSet ds = new DataSet(); ad.Fill(ds); GridView1.DataSource=ds; GridView1.DataBind(); }
Change theBoundFields
like below...<asp:BoundField DataField="CourseCode" HeaderText="ID" Visible="false"/> <asp:BoundField DataField="CourseCode" HeaderText="Course Code"/> <asp:BoundField DataField="CourseName" HeaderText="CourseName"/> <asp:BoundField DataField="CourseUnit" HeaderText="Course Unit" />
Try this and let me know.