convert to C#, viewstate

Posted on 2006-05-26
Last Modified: 2008-03-03
Hi Expert,

I tried to convert VB.NET to C#, there is one line shown below gives error message when I build the project.  It is viewstate statement, this line works fine in VB.NET.

Cannot implicitly convert type 'object' to 'System.Data.DataTable'


DataTable tableSource;
DataTable tableTarget;

private void Page_Load(object sender, System.EventArgs e)
if (!Page.IsPostBack)
tableSource = ds.Tables["tblFromUsers"];
tableTarget = ds.Tables["tblToUsers"];

ViewState["tblSource"] = tableSource;
ViewState["tblTarget"] = tableTarget;                                          
tableSource = ViewState["tblSource"];   <------ Cannot implicitly convert type 'object' to 'System.Data.DataTable'
tableTarget = ViewState["tblTarget"];
Question by:5281
    LVL 25

    Accepted Solution


    tableSource = ViewState["tblSource"] as DataTable;  
    tableTarget = ViewState["tblTarget"] as DataTable;

    Author Comment


    You are great, that works.  However, I am a little bit confused, why sometimes it need define again, what need redefined, what doesn't need redefined, do we just remember we need define that if it is datatable, dataset?
    LVL 25

    Expert Comment

    You're not redefining it.  There is not an implicit cast from Object (which ViewState[] returns) to DataTable.  You have to explicitly state what type you want to cast it to.  The "as" keyword is saying "If you can cast it to a DataTable, do it, otherwise give me a null object):


    Author Comment


    I think I understand that now.  Thanks again.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    779 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

    13 Experts available now in Live!

    Get 1:1 Help Now