Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4014786
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:32:20+00:00 2026-05-20T09:32:20+00:00

I’m developing a basic web form that allows users to search certain columns in

  • 0

I’m developing a basic web form that allows users to search certain columns in a database and display those records in a GridView. I have several TextBox controls that allow users to search for a name, id, etc. I am using LinqToSql for my data access layer. It’s your basic search form.

The trouble comes with the GridView I am attempting to use display the data. I have the query itself in the OnClick event method of a Button control.

protected void SearchSpecific_Click(object sender, EventArgs e)
    {
    MyDataContext dbContext = new MyDataContext();
    var results = from row in dbContext.MyTable
                  select row;

    if(TextBoxIsValid(IDNumber))
        results = results.Where(m => m.ID == Int32.Parse(IDNumber.Text));

    if (validationFlag)
    {
        // this part doesn't seem right
        SearchResultDataset.DataSource = results;
        SearchResultDataset.DataBind();
    }
}

Here’s the gridview, though it’s nothing special:

<asp:GridView ID="SearchResultDataset" runat="server"
    AlternatingRowStyle-CssClass="alt"
    CssClass="search_results"
    GridLines="None"
    AutoGenerateColumns="true"></asp:GridView>

Everything runs smoothly until I get to the DataBind() method and a NullReferenceException was thrown. Holy exceptions, Batman!

The debugger tells me that results has the correct values, but I have a feeling it has to do with binding datasources on postback. I feel like I’m missed something… Any suggestions? I can provide a stack trace if necessary.

EDIT:

There’s a much larger problem at hand. I’ve tried adding both a LinqDataSource and an ObjectDataSource control to my page and both throw a NullReferenceException. I constructed both using the GUI and they look perfect.

After this I then assigned a DataSource in the Page_Load method:

try
{
    MyDataContext dbc = new MyDataContext();
    SearchResultDataset.DataSource = dbc.MyTable;
    SearchResultDataset.DataBind();
}
catch (Exception ex)
{
    errText.InnerHtml = ex.ToString();
}

Came up with this exception:

System.Reflection.TargetInvocationException: Property accessor 'IsPostBack' on object 'MySite.MySite' threw the following exception:'Object reference not set to an instance of an object.' ---> System.NullReferenceException: Object reference not set to an instance of an object.

at System.Web.UI.UserControl.get_IsPostBack()

The exception is definitely being thrown in the DataBind() method. This raises the question WTF is going on? I can’t seem to be able to bind a linqtosql source to my GridView. I can’t seem to find the page, but I read somewhere that GridView has difficulty binding to anything that isn’t a DataSet object.

UPDATE

Used SqlDataSource control to access database.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T09:32:21+00:00Added an answer on May 20, 2026 at 9:32 am

    You problem appears to be that you are trying to bind your dataview to an anonymous type. As MSDN says:

    Creating a DataView from a query that
    returns anonymous types is not
    supported.

    Solution: use the .ToList() function to cast that var into a datatype that the dataview understands and can work with.

    Like this:

    SearchResultDataset.DataSource = results.ToList();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
In my XML file chapters tag has more chapter tag.i need to display chapters

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.