Casting Error!!!!

G

Guest

Hi All,

I am having a problem with the following code:

During the Item Bound event is am getting a 'Cast' error for this line:
if(!((DbDataRecord)e.Item.DataItem).IsDBNull(2))

Any thoughts why this is happening?

Cheers,
Adam

private DataView GetEnrollments()
{

//create db connection
SqlConnection Cn = new
SqlConnection(ConfigurationSettings.AppSettings["ConnStr"]);

//open db connection
Cn.Open();

//sql command to retrieve students
SqlCommand cmdGetElligibleStudents = new
SqlCommand("GetElegibleStudents",Cn);
cmdGetElligibleStudents.CommandType = CommandType.StoredProcedure;
cmdGetElligibleStudents.Parameters.Add("@CourseID",
Request.QueryString["CourseID"]);

DataSet dsElligibleStudents = new DataSet();

DataAdapter daElligibleStudents = new
SqlDataAdapter(cmdGetElligibleStudents);
daElligibleStudents.Fill(dsElligibleStudents);

//create DataView out of DataSource
DataView dvEnrollments = new DataView(dsElligibleStudents.Tables[0]);

//close db connection
Cn.Close();

return dvEnrollments;

}

public void dgEnrollments_ItemBound(object sender, DataGridItemEventArgs e)
{

if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{

if(!((DbDataRecord)e.Item.DataItem).IsDBNull(2))
{

((CheckBox)e.Item.FindControl("chkEnrolled")).Checked = true;

}

}
 
L

Laurent Bugnion

Hi,
Hi All,

I am having a problem with the following code:

During the Item Bound event is am getting a 'Cast' error for this line:
if(!((DbDataRecord)e.Item.DataItem).IsDBNull(2))

Any thoughts why this is happening?

Cheers,
Adam

Are you sure that e.Item.DataItem exists and is not null?

HTH,
Laurent
 
K

Kevin Jones

Why are you expecting e.Item.DataItem to be a DbDataRecord, in my
testing I see the DataItem as a DataRowView type,

Kevin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top