将数据绑定到网格视图 [英] Binding Data to a Grid View

查看:80
本文介绍了将数据绑定到网格视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码给出了一个错误

 DataBinding:'  System.Data.DataRowView '不包含名称为'  CourseName'的属性。 

 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Web.UI;
使用 System.Web.UI.WebControls;
使用 System.Data;
使用 System.Data.SqlClient;
使用 System.Configuration;

public partial class CourseRegistration:System.Web.UI.Page
{
Connector newconnect = new Connector();

受保护 void Page_Load(对象发​​件人,EventArgs e)
{
if (会话[ 学生]!= null
{
尝试
{
字符串 matricno =会话[ 学生]。ToString();

if (!IsPostBack)
{
GridView1.DataSource = GetData( 选择CourseRegistration.CourseCode,CoursesTable.CourseName,CoursesTable.CourseUnit(DepartmentTable内部连接CoursesTable on DepartmentTable.DepartmentID = CoursesTable.DepartmentID)内部联接CourseRegistration对CoursesTable。 CourseCode = CourseRegistration.CourseCode,其中MatricNo =' + matricno + ');
GridView1.DataBind();
}
}
catch (例外情况)
{
Label1.Text = ex.Message;
}
}

}

私有 DataSet GetData( string query)
{
string matricno = Session [ 学生]。ToString();
string conString = ConfigurationManager.ConnectionStrings [ OnlineExamination_String]的ConnectionString。
SqlCommand cmd = new SqlCommand(query);
使用(SqlConnection con = new SqlConnection(conString))
{
使用(SqlDataAdapter sda = new SqlDataAdapter())
{
cmd .Connection = con;
sda.SelectCommand = cmd;
使用(DataSet ds = new DataSet())
{
sda.Fill(ds);
return ds;
}
}
}
}
受保护 void Button1_Click( object sender,EventArgs e)
{
string matricno =会话[ 学生]。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 (例外情况)
{
Label1.Text = ex.Message;
}
}
其他
{

}
}
}

解决方案

检查你的表'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   = < span class =code-keyword> 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) Function

if (!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 the BoundFields 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.


这篇关于将数据绑定到网格视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆