Solved

Cannot implicitly convert type 'void' to 'object'

Posted on 2008-06-21
8
7,776 Views
Last Modified: 2013-11-26
See code attached, when i run the code in debug the  'public static void GetNews()' does bring data back but the error (Cannot implicitly convert type 'void' to 'object') is when i try and put the data to a datagrid

Any Ideas??
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
 
namespace News
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            GridView1.DataSource = GetNews();
            GridView1.DataBind();
            //GetNews();
        }
 
        public static void GetNews()
        {
 
            NEwsDataContext db = new NEwsDataContext();
 
            var gnews = from x in db.News
                        orderby x.PostDT ascending
                        select x;
 
        }
    }
}

Open in new window

0
Comment
Question by:martgriff
8 Comments
 
LVL 22

Accepted Solution

by:
p_davis earned 500 total points
ID: 21837943
GetNews is of type void and therefore isn't supose to return any data, from running the method alone so.

GridView1.DataSource = GetNews();

is illegal because datasource needs an object GetNews is void and doesn't return an object.
0
 

Author Comment

by:martgriff
ID: 21837952
could you recomend a solution?
0
 

Author Comment

by:martgriff
ID: 21838005
Thanks for your help, I have worked it out see below:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
 
namespace News
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            GridView1.DataSource = GetNews();
            GridView1.DataBind();
            //GetNews();
        }
 
        public static Object GetNews()
        {
 
            NEwsDataContext db = new NEwsDataContext();
 
            var gnews = from x in db.News
                        orderby x.PostDT ascending
                        select x;
            return gnews;
 
        }
    }
}

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:coldboy
ID: 21839445
p_davis should be marked as answer with full award of points
0
 
LVL 22

Expert Comment

by:p_davis
ID: 21855446
how in the world does my answer deserve a grade of C?
0
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 21855551
Apparently, martgriff is trying to acquire a terrible reputation....

First, by attempting to hold back points to himself (apparently, he felt he deserved them for making some progress on his own problem, but he doesn't seem to realize that is EXPECTED of people who have a problem).

Then, by minimizing the number of points awarded with no benefit to himself at all.

Martgriff, somebody tried to help you - and for no reason other than maybe that it took him longer than 20 minutes to answer your followup (after he had ALREADY satisfactorily answered your question and set you straight), you awarded him only one third of the points that he COULD have gotten.

Do you really think this will encourage people to help you in the future?
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
There’s a good reason for why it’s called a homepage – it closely resembles that of a physical house and the only real difference is that it’s online. Your website’s homepage is where people come to visit you. It’s the family room of your website wh…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

733 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