0

My code is properly work in desktop application show data asncrounsly in data gridview but when I use same code in web form it throws exception GridView1 was null. at GridView1.DataSource = table;.While debugging data is completely working just want to put that data into gridview.What changes should i made to acheive my task.

    public partial class _Default : System.Web.UI.Page
    {
        public class NameAndScore
        {
            public string Name { get; set; }
            public string Score { get; set; }
        }
     public _Default()
     {

        InitTable();
     }
        DataTable table;
        HtmlWeb web = new HtmlWeb();

        private async Task<List<NameAndScore>> GameRankingsFromPage(int pagenum)
        {
            string url = "https://www.ebay.com/sch/i.html?_nkw=xbox+one&_in_kw=1&_ex_kw=&_sacat=0&LH_Complete=1&_udlo=&_udhi=&_ftrt=901&_ftrv=1&_sabdlo=&_sabdhi=&_samilow=&_samihi=&_sadis=15&_stpos=&_sargn=-1%26saslc%3D1&_salic=1&_sop=12&_dmd=1&_ipg=50&_fosrp=1";
            //    string url = "https://www.gamerankings.com/browse.html";

            if (pagenum != 0)
                url = "https://www.ebay.com/sch/i.html?_sacat=0&LH_Complete=1&_udlo=&_udhi=&_ftrt=901&_ftrv=1&_sabdlo=&_sabdhi=&_samilow=&_samihi=&_sadis=15&_stpos=&_sop=12&_dmd=1&_fosrp=1&_nkw=xbox+one&_pgn=" + pagenum.ToString() + "&_skc=50&rt=nc";

            var doc = await Task.Factory.StartNew(() => web.Load(url));

            var namenodes = doc.DocumentNode.SelectNodes("//*[contains(@id,'item')]/h3/a");
            var scorenodes = doc.DocumentNode.SelectNodes("//*[contains(@id,'item')]/ul[1]/li[1]/span");
            var names = namenodes.Select(node => node.InnerText.Trim('\r', '\n', '\t'));
            var scores = scorenodes.Select(node => node.InnerText.Trim('\r', '\n', '\t'));


            if (namenodes == null || scorenodes == null)
                return new List<NameAndScore>();

            return names.Zip(scores, (name, score) => new NameAndScore() { Name = name.ToString(), Score = score.ToString() }).ToList();
        }
        protected async void Page_Load(object sender, EventArgs e)
        {
            int pageNume = 0;
            int id = 0;
            var rankings = await GameRankingsFromPage(0);
            while (rankings.Count > 0)
            {
                foreach (var ranking in rankings)
                     table.Rows.Add(id++, ranking.Name, ranking.Score);
                GridView1.DataSource = table;
                GridView1.DataBind();
                pageNume++;
                rankings = await GameRankingsFromPage(pageNume);
            }

        }
        private void InitTable()
        {
            table = new DataTable("Xbox Prices");
            table.Columns.Add("ID", typeof(string));
            table.Columns.Add("Name", typeof(string));
            table.Columns.Add("Score", typeof(string));
            GridView1.DataSource = table;
            GridView1.DataBind();

        }
    }
Adil Yar
  • 45
  • 1
  • 10
  • You are not calling `InitTable` method anywhere that's why `table` is null – Chetan Dec 23 '18 at 14:48
  • @ChetanRanpariya I have edited my question i added some code which is exactly in desktop application but i forget to put in this now error show at second last line GridView1.DataSource = table; as 'Object reference not set to an instance of an object.' GridView1 was null. – Adil Yar Dec 23 '18 at 15:00
  • @Adriani6 the link you mention is about for java while i am asking about .net secondly this null exception show at gridview. – Adil Yar Dec 23 '18 at 15:04
  • @AdilYar That's right. Answer would be quite universal though. I should of marked it as a duplicate of this though: https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it. – Adrian Dec 23 '18 at 15:06
  • @Adriani6 I have edited my question now, I searched this problem on google before posting there but could not get any ans even duplicated to that so I posted there thanks. – Adil Yar Dec 23 '18 at 15:12
  • @AdilYar you need to understand the difference between Windows applications and web applications and how they work. Did you debug the code and check what is null when you get the exception? – Chetan Dec 23 '18 at 22:47
  • @ChetanRanpariya dude I have worked in webform and windows application gridview but problem is that first time I used async/await method that why I am confuse. Code is working perfectly but exception is thrown at GridView1.DataSource = table; gridview is null.I think to put data asynchronously in web form gridview is different from the desktop application.It would be nice if you tell me changes so that it works. – Adil Yar Dec 24 '18 at 06:43

0 Answers0