PC Review


Reply
Thread Tools Rate Thread

Casting Error!!!!

 
 
=?Utf-8?B?QUo=?=
Guest
Posts: n/a
 
      24th Aug 2006
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;

}

}
 
Reply With Quote
 
 
 
 
Laurent Bugnion
Guest
Posts: n/a
 
      24th Aug 2006
Hi,

AJ wrote:
> 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
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
Private/Malaysia: http://mypage.bluewin.ch/lbugnion
Support children in Calcutta: http://www.calcutta-espoir.ch
 
Reply With Quote
 
=?Utf-8?B?QXVndXN0aW4gUHJhc2FubmE=?=
Guest
Posts: n/a
 
      24th Aug 2006
find out the exact type by debugging the code.

check out
http://www.velocityreviews.com/forum...ast-error.html

Regards,
Augustin
http://augustinprasanna.blogspot.com

"AJ" wrote:

> 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;
>
> }
>
> }

 
Reply With Quote
 
Kevin Jones
Guest
Posts: n/a
 
      24th Aug 2006
Why are you expecting e.Item.DataItem to be a DbDataRecord, in my
testing I see the DataItem as a DataRowView type,

Kevin

AJ wrote:
> 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;
>
> }
>
> }

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Casting error - why? Anodes Microsoft C# .NET 4 27th Mar 2008 04:12 PM
Down-casting of Typed Collection (Casting Generic Types?) conchur Microsoft C# .NET 1 3rd Jul 2006 11:45 AM
Casting Error mohit Microsoft ASP .NET 2 23rd Dec 2005 07:32 AM
C# Casting error =?Utf-8?B?QnJ5Y2U=?= Microsoft Dot NET 5 6th Mar 2004 08:19 AM
casting Error Microsoft Dot NET Framework Forms 1 26th Nov 2003 09:13 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:36 AM.