Click here to Skip to main content
15,063,239 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
this code below gives me an error
C#
DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'CourseName'.
am hoping to get a solution to it thanks
C#
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
{

}
}
}
Posted
v2
Comments
   
On which line you encounter this exception?
Can you post the GridView Markup?
segedy 18-Feb-14 9:29am
   
am loading the grid view at page load....but once i click the link to the page it gives the error without loading the page......and no line number specified
Krunal Rohit 16-Feb-14 21:49pm
   
post your .aspx code.
agent_kruger 17-Feb-14 0:05am
   
sir, please tell us the line where error is seen
segedy 18-Feb-14 9:32am
   
this is the aspx code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CourseRegistration.aspx.cs" Inherits="CourseRegistration" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.style1
{
width: 970px;
height: 160px;
}
.style2
{
height: 160px;
}
.style3
{
height: 26px;
}
.style6
{
height: 21px;
}
.style7
{
height: 9px;
}
.style8
{
height: 14px;
}
.style9
{
height: 23px;
}
.style10
{
height: 26px;
width: 330px;
}
.style11
{
height: 160px;
width: 330px;
}
.style12
{
height: 9px;
width: 28px;
}
.style13
{
height: 14px;
width: 28px;
}
.style14
{
height: 21px;
width: 28px;
}
.style15
{
height: 23px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>

<table align="center" style="width:100%; height: 343px;">
<tr>
<td colspan="4">
<img alt="" class="style1" src="WebSite/top_banner_oou.jpg" /></td>
</tr>
<tr>
<td class="style10">
</td>
<td class="style3" colspan="2">
</td>
<td class="style3">
</td>
</tr>
<tr>
<td class="style11" rowspan="7">
</td>
<td class="style12">
Faculty:</td>
<td class="style7">
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" DataSourceID="FacultyString" DataTextField="FacultyName" DataValueField="FacultyName">

<asp:SqlDataSource ID="FacultyString" runat="server"
ConnectionString="<%$ ConnectionStrings:OnlineExamination_String %>"
SelectCommand="SELECT FacultyName FROM dbo.FacultyTable">

</td>
<td class="style2" rowspan="7">
</td>
</tr>
<tr>
<td class="style13">
Department:</td>
<td class="style8">
<asp:SqlDataSource ID="DepartmentString" runat="server" ConnectionString="<%$ ConnectionStrings:OnlineExamination_String %>"
SelectCommand="select DepartmentTable.DepartmentName from FacultyTable inner join DepartmentTable on FacultyTable.FacultyID=DepartmentTable.FacultyID where([FacultyName] = @FacultyName)">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="FacultyName" PropertyName="SelectedValue"
Type="string" />
</SelectParameters>

<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="true" DataSourceID="DepartmentString" DataTextField="DepartmentName" DataValueField="DepartmentName">

</td>
</tr>
<tr>
<td class="style14">
Course:</td>
<td cl
   
Where is GridView in this code? I can't see it.
segedy 19-Feb-14 15:26pm
   
<tr>
<td class="style15" colspan="2">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<columns>
<asp:BoundField DataField="CourseRegistration.CourseCode" HeaderText="ID" Visible="false"/>
<asp:BoundField DataField="CourseRegistration.CourseCode" HeaderText="Course Code"/>
<asp:BoundField DataField="CoursesTable.CourseName" HeaderText="CourseName"/>
<asp:BoundField DataField="CoursesTable.CourseUnit" HeaderText="Course Unit" />


</td>
</tr>
   
Please check my answer. :)

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

SQL
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...
XML
<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.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900