开发者

Update Gridview using OnClick Event

开发者 https://www.devze.com 2023-02-14 05:02 出处:网络
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

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.


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

精彩评论

暂无评论...
验证码 换一张
取 消