Note that there are some explanatory texts on larger screens.

plurals
  1. POUpdate Gridview using OnClick Event
    primarykey
    data
    text
    <p>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.</p> <p>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.</p> <pre><code>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 =&gt; m.ID == Int32.Parse(IDNumber.Text)); if (validationFlag) { // this part doesn't seem right SearchResultDataset.DataSource = results; SearchResultDataset.DataBind(); } } </code></pre> <p>Here's the gridview, though it's nothing special:</p> <pre><code>&lt;asp:GridView ID="SearchResultDataset" runat="server" AlternatingRowStyle-CssClass="alt" CssClass="search_results" GridLines="None" AutoGenerateColumns="true"&gt;&lt;/asp:GridView&gt; </code></pre> <p>Everything runs smoothly until I get to the DataBind() method and a NullReferenceException was thrown. Holy exceptions, Batman!</p> <p>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.</p> <p><strong>EDIT:</strong></p> <p>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.</p> <p>After this I then assigned a DataSource in the Page_Load method:</p> <pre><code>try { MyDataContext dbc = new MyDataContext(); SearchResultDataset.DataSource = dbc.MyTable; SearchResultDataset.DataBind(); } catch (Exception ex) { errText.InnerHtml = ex.ToString(); } </code></pre> <p>Came up with this exception:</p> <pre><code>System.Reflection.TargetInvocationException: Property accessor 'IsPostBack' on object 'MySite.MySite' threw the following exception:'Object reference not set to an instance of an object.' ---&gt; System.NullReferenceException: Object reference not set to an instance of an object. </code></pre> <p>at System.Web.UI.UserControl.get_IsPostBack()</p> <p>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.</p> <p><strong>UPDATE</strong></p> <p>Used SqlDataSource control to access database.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload