Hi guys. I have an ASP.Net assignment to do and need some assistance if possible.
I previously read a post from a person with the same assignment but this is for ASP.Net LINQ to SQL not windows form. The following is my code forthe batting average.

Code-Behind

private void btnBattingAverage_Click(object sender, EventArgs e)
        {
            var Players =
                from player in database.Players
                where player.BattingAverage >= decimal.Parse(txtMin.Text) && player.BattingAverage <= decimal.Parse(txtMax.Text)
                select player;
            List<Player> FindBy = Players.ToList();
            playerDataGridView.DataSource = FindBy;
            
        }

For some reason is not working. The code seems fine to me. Is there another way of doing this problem?

Thanks I appreciate for your assistance.

playerDataGridView.DaaBind() method is missing.

private void btnBattingAverage_Click(object sender, EventArgs e)
        {
            var Players =
                from player in database.Players
                where player.BattingAverage >= decimal.Parse(txtMin.Text) && player.BattingAverage <= decimal.Parse(txtMax.Text)
                select player;
            List<Player> FindBy = Players.ToList();
            playerDataGridView.DataSource = FindBy;
            playerDataGridView.DataBind();
            
        }

Thanks I was missing the DataBind() method. I tried what you suggested and now i am getting this error.

Both DataSource and DataSourceID are defined on 'playersGridView'. Remove one definition. Thanks.

I appreciate for your assistance.

bravo659,

Are you using the tag DataSourceID = "something here" in your asp markup? If so, try removing that.

As long as the btnBattingAverage_Click is triggered prior to when you want it viewed (page load, after a button click etc) you should be okay. Posting your asp mark up would help trouble shoot the problem as well.

Hi croker10,
I appreciate you guiding to the right direction I did removed the DataSourceID="LinqDataSource1" is what i had in its place. I removed it from the source code in the asp and it worked. But when the page loads it does not load the gridview. Do I have to code the DataSourceID in the Page_Load?

Thanks.

What I would recommend is that you create a function in your code that contians all the code required to collect and bind your data. I normally call it BindDataGrid() or something similar. You can then choose to bind the datasource to the data grid when ever you need to.

If you need it when the page is first loaded, you can put that call inside the Page_Load event, you can also make calls to it during button click events or any other event. Keep in mind that unless you want the databound to the control everytime something is clicked, it is a good idea to nest it inside an if(!Page.isPostBack) statement.

Hope that helps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.