Solved

ASP.Net/C# - Checkbox Databinder Specified Cast Invalid? Bool?

Posted on 2008-06-16
4
1,668 Views
Last Modified: 2010-04-21
Hello all.  I have a static datatable where I set the datacolumn "W9" to a Boolean datatype but my ASCX code is still throwing a cast error, any idea?  My code is in the snippet here.  The W9 column is the one I am testing with to get this to work.
code behind: Datatable code:

  private DataTable Contributors

    {

        get

        {

            //object obj = this.Session["Employees"];

            //if ((!(obj == null)))

            //{

            //    return ((DataTable)(obj));

            //}

            //DataTable myDataTable = new DataTable();

            //myDataTable = GetDataTable("SELECT * FROM Employees");

            //this.Session["Employees"] = myDataTable;

            //return myDataTable;

            object obj = this.Session["Contributors"];

            if ((!(obj == null)))

            {

                return ((DataTable)(obj));

            }
 

            System.Data.DataTable dt;

            dt = new DataTable();
 

            // Columns

            DataColumn ContributorID;

            DataColumn FirstName;

            DataColumn MiddleInitial;

            DataColumn LastName;

            DataColumn FullName;

            DataColumn Email;

            DataColumn DisplayName;

            DataColumn DisplayBookWebsite;

            DataColumn ContributorRole;

            DataColumn AddressLine1;

            DataColumn AddressLine2;

            DataColumn City;

            DataColumn State;

            DataColumn PostalCode;

            DataColumn Country;

            DataColumn CitizenShipType;

            DataColumn RoyaltyRecipient;

            DataColumn RoyaltyPayableTo;

            DataColumn SSNTaxID;

            DataColumn EFT;

            DataColumn W9;

            

            // Initialize the columns

            ContributorID = new DataColumn("ContributorID");

            FirstName = new DataColumn("FirstName");

            MiddleInitial = new DataColumn("MiddleInitial");

            LastName = new DataColumn("LastName");

            FullName = new DataColumn("FullName");

            Email = new DataColumn("Email");

            DisplayName = new DataColumn("DisplayName");

            DisplayBookWebsite = new DataColumn("DisplayBookWebsite");

            ContributorRole = new DataColumn("ContributorRole");

            AddressLine1 = new DataColumn("AddressLine1");

            AddressLine2 = new DataColumn("AddressLine2");

            City = new DataColumn("City");

            State = new DataColumn("State");

            PostalCode = new DataColumn("PostalCode");

            Country = new DataColumn("Country");

            CitizenShipType = new DataColumn("CitizenShipType");

            RoyaltyRecipient = new DataColumn("RoyaltyRecipient");

            RoyaltyPayableTo = new DataColumn("RoyaltyPayableTo");

            SSNTaxID = new DataColumn("SSNTaxID");

            EFT = new DataColumn("EFT");

            W9 = new DataColumn("W9");

            W9.DataType = System.Type.GetType("System.Boolean");
 

            // Add columns

            dt.Columns.Add(ContributorID);

            dt.Columns.Add(FirstName);

            dt.Columns.Add(MiddleInitial);

            dt.Columns.Add(LastName);

            dt.Columns.Add(FullName);

            dt.Columns.Add(Email);

            dt.Columns.Add(DisplayName);

            dt.Columns.Add(DisplayBookWebsite);

            dt.Columns.Add(ContributorRole);

            dt.Columns.Add(AddressLine1);

            dt.Columns.Add(AddressLine2);

            dt.Columns.Add(City);

            dt.Columns.Add(State);

            dt.Columns.Add(PostalCode);

            dt.Columns.Add(Country);

            dt.Columns.Add(CitizenShipType);

            dt.Columns.Add(RoyaltyRecipient);

            dt.Columns.Add(RoyaltyPayableTo);

            dt.Columns.Add(SSNTaxID);

            dt.Columns.Add(EFT);

            dt.Columns.Add(W9);
 

            //Add DataRows to DataTable

            DataRow myRow;

            myRow = dt.NewRow();

            myRow["ContributorID"] = 123;

            myRow["FirstName"] = "Sean";

            myRow["MiddleInitial"] = "";

            myRow["LastName"] = "Bornstein";

            myRow["FullName"] = "Sean Bornstein";

            myRow["Email"] = "sbornstein@authorhouse.com";

            myRow["DisplayName"] = "";

            myRow["DisplayBookWebsite"] = false;

            myRow["ContributorRole"] = "Contributor";

            myRow["AddressLine1"] = "12 Water Road";

            myRow["AddressLine2"] = "";

            myRow["City"] = "Scituate";

            myRow["State"] = "MA";

            myRow["PostalCode"] = "02066";

            myRow["Country"] = "USA";

            myRow["CitizenshipType"] = "US Citizen";

            myRow["RoyaltyRecipient"] = true;

            myRow["RoyaltyPayableTo"] = "Sean Bornstein";

            myRow["SSNTaxID"] = "337665482";

            myRow["EFT"] = false;

            myRow["W9"] = true;

            dt.Rows.Add(myRow);
 

            myRow = dt.NewRow();

            myRow["ContributorID"] = 234;

            myRow["FirstName"] = "Jack";

            myRow["MiddleInitial"] = "";

            myRow["LastName"] = "Flash";

            myRow["FullName"] = "Jack Flash";

            myRow["Email"] = "jflash@authorhouse.com";

            myRow["DisplayName"] = "";

            myRow["DisplayBookWebsite"] = true;

            myRow["ContributorRole"] = "Agent";

            myRow["AddressLine1"] = "5 Waterhill Lane";

            myRow["AddressLine2"] = "";

            myRow["City"] = "South Boston";

            myRow["State"] = "MA";

            myRow["PostalCode"] = "02127";

            myRow["Country"] = "USA";

            myRow["CitizenshipType"] = "US Citizen";

            myRow["RoyaltyRecipient"] = false;

            myRow["RoyaltyPayableTo"] = "";

            myRow["SSNTaxID"] = "";

            myRow["EFT"] = false;

            myRow["W9"] = true;

            dt.Rows.Add(myRow);
 

            myRow = dt.NewRow();

            myRow["ContributorID"] = 789;

            myRow["FirstName"] = "Leopold";

            myRow["MiddleInitial"] = "";

            myRow["LastName"] = "Butters";

            myRow["FullName"] = "Leopold Butters";

            myRow["Email"] = "butters@authorhouse.com";

            myRow["DisplayName"] = "";

            myRow["DisplayBookWebsite"] = true;

            myRow["ContributorRole"] = "Co-Author";

            myRow["AddressLine1"] = "33 South Park Rd";

            myRow["AddressLine2"] = "";

            myRow["City"] = "South Park";

            myRow["State"] = "IN";

            myRow["PostalCode"] = "47403";

            myRow["Country"] = "USA";

            myRow["CitizenshipType"] = "US Citizen";

            myRow["RoyaltyRecipient"] = true;

            myRow["RoyaltyPayableTo"] = "Butters";

            myRow["SSNTaxID"] = "222989876";

            myRow["EFT"] = true;

            myRow["W9"] = false;

            dt.Rows.Add(myRow);
 

            myRow = dt.NewRow();

            myRow["ContributorID"] = 908;

            myRow["FirstName"] = "Eric";

            myRow["MiddleInitial"] = "";

            myRow["LastName"] = "Cartman";

            myRow["FullName"] = "Eric Cartman";

            myRow["Email"] = "cartman@authorhouse.com";

            myRow["DisplayName"] = "respect authority";

            myRow["DisplayBookWebsite"] = true;

            myRow["ContributorRole"] = "Contributor";

            myRow["AddressLine1"] = "865 South Park Rd";

            myRow["AddressLine2"] = "";

            myRow["City"] = "South Park";

            myRow["State"] = "IN";

            myRow["PostalCode"] = "47403";

            myRow["Country"] = "USA";

            myRow["CitizenshipType"] = "US Citizen";

            myRow["RoyaltyRecipient"] = true;

            myRow["RoyaltyPayableTo"] = "Kitty";

            myRow["SSNTaxID"] = "111345678";

            myRow["EFT"] = false;

            myRow["W9"] = true;

            dt.Rows.Add(myRow);
 

            myRow = dt.NewRow();

            myRow["ContributorID"] = 227;

            myRow["FirstName"] = "Stan";

            myRow["MiddleInitial"] = "";

            myRow["LastName"] = "Marsh";

            myRow["FullName"] = "Stan Marsh";

            myRow["Email"] = "smarsh@authorhouse.com";

            myRow["DisplayName"] = "";

            myRow["DisplayBookWebsite"] = true;

            myRow["ContributorRole"] = "Contributor";

            myRow["AddressLine1"] = "211 South Park Rd";

            myRow["AddressLine2"] = "";

            myRow["City"] = "South Park";

            myRow["State"] = "IN";

            myRow["PostalCode"] = "47403";

            myRow["Country"] = "USA";

            myRow["CitizenshipType"] = "US Citizen";

            myRow["RoyaltyRecipient"] = false;

            myRow["RoyaltyPayableTo"] = "";

            myRow["SSNTaxID"] = "";

            myRow["EFT"] = false;

            myRow["W9"] = false;

            dt.Rows.Add(myRow);
 

            myRow = dt.NewRow();

            myRow["ContributorID"] = 999;

            myRow["FirstName"] = "Yoggi";

            myRow["MiddleInitial"] = "";

            myRow["LastName"] = "Bear";

            myRow["FullName"] = "Yoggi Bear";

            myRow["Email"] = "ybear@authorhouse.com";

            myRow["DisplayName"] = "";

            myRow["DisplayBookWebsite"] = false;

            myRow["ContributorRole"] = "Contributor";

            myRow["AddressLine1"] = "33 Yellowstonr Park";

            myRow["AddressLine2"] = "";

            myRow["City"] = "Yellowstone";

            myRow["State"] = "NH";

            myRow["PostalCode"] = "23223";

            myRow["Country"] = "USA";

            myRow["CitizenshipType"] = "US Citizen";

            myRow["RoyaltyRecipient"] = false;

            myRow["RoyaltyPayableTo"] = "";

            myRow["SSNTaxID"] = "";

            myRow["EFT"] = false;

            myRow["W9"] = false;

            dt.Rows.Add(myRow);
 

            this.Session["Contributors"] = dt;
 

            return dt;

        }

    }
 

ASCX Code:

<asp:CheckBox ID="cbxW9" Checked='<%# (DataBinder.Eval(Container, "DataItem.W9") is DBNull ?false:Eval("W9")) %>'  runat="server" />

Open in new window

0
Comment
Question by:sbornstein2
  • 2
4 Comments
 
LVL 14

Expert Comment

by:ajitha75
ID: 21799376
W9 column seems to be fine. The problem i think is with EFT and other columns which are declared as string datatype
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 21799377
Try this:
<asp:CheckBox ID="cbxW9" Checked='<%# (DataBinder.Eval(Container, "DataItem.W9")=DBNull.Value ?false:(bool)Eval("W9")) %>'  runat="server" />

Open in new window

0
 
LVL 22

Accepted Solution

by:
prairiedog earned 250 total points
ID: 21799384
Sorry, missed a "=".
<asp:CheckBox ID="cbxW9" Checked='<%# (DataBinder.Eval(Container, "DataItem.W9")==DBNull.Value ?false:(bool)Eval("W9")) %>'  runat="server" />

Open in new window

0
 

Author Closing Comment

by:sbornstein2
ID: 31467839
thanks perfect
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Replacing HTML tags in Textarea/Textbox 5 46
Why is "packages element is not declared"? 2 124
Problem to page 4 71
Asp.net mvc foreach 3 33
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now