0

I've been looking for a while, and didn't find a proper solution; I'm trying to search in a DataSet (which is a source of a gridView) which consists of usernames & details in an ASP.Net page.

If the user wants to find a specific row, he needs to insert the username, and press a button. Then, I want to change the dataset which is saved on the cache - I want it to contain that specific row the user was searching for.

However, when I change the dataset (set it to contain only one row) I get the following error:

A field or property with the name 'Username' was not found on the selected data source.

Am I doing anything wrong?

Here's the code:

protected void Search_Click(object 
        sender, EventArgs e)
    {
        DataRow myRow = findRow(((DataSet)Cache["Users"]).Tables[0], userSearched.Text);
        if (myRow == null)
        {
            ResponseLabel.ForeColor = System.Drawing.Color.Red;
            ResponseLabel.Text = "User not found.";
            return;
        }
        ResponseLabel.ForeColor = System.Drawing.Color.Green;
        ResponseLabel.Text = "There you go.";
        DataSet newDs = new DataSet();
        DataTable newDt = new DataTable();
        newDt.ImportRow(myRow);
        newDs.Tables.Add(newDt);
        Cache["Users"] = newDs;
        UpdateSource(); // Updating the source after each iteration
        }

findrow():

private DataRow findRow(DataTable View, string searchValue, int index = 0)
    {
        foreach (DataRow row in View.Rows)
        {
            if (row.ItemArray[index].Equals(searchValue))
            {
                return row;
            }
        }
        return null;
    }

Relevant part in the GridView:

<Columns>
        <asp:BoundField DataField="Username" HeaderText="Username" 
            SortExpression="Username" />
        <asp:BoundField DataField="TimeJoined" HeaderText="Joined" 
            SortExpression="TimeJoined" />
        <asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
        <asp:CheckBoxField DataField="Banned" HeaderText="Banned" 
            SortExpression="Banned" />
        <asp:CheckBoxField DataField="Admin" HeaderText="Admin" 
            SortExpression="Admin" />
        <asp:CommandField HeaderText="Options" ShowEditButton="True" 
            ShowHeader="True" />
    </Columns>

Page load:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Cache["Users"] == null)
            {
                UsersView.DataSource = DbManager.getUsersView(); // database stuff. It worked pretty well before.
                Cache["Users"] = UsersView.DataSource;
            }
            else
            {
                UsersView.DataSource = (DataSet)Cache["Users"];
            }
            UsersView.DataBind(); // I get the error here.
        }
    }

If you need anything else, let me know. Thanks in advance!

1 Answer 1

1

I believe you are losing the DataTable schema when you recreate it in Search_Click, try explicitly declaring the column names.

Btw, have you tried the DataTable Select method (https://msdn.microsoft.com/en-us/library/y06xa2h1.aspx) or a DataView (https://msdn.microsoft.com/en-us/library/bb669073%28v=vs.110%29.aspx) ?

Sign up to request clarification or add additional context in comments.

2 Comments

What do you mean by that? can you give an example (given the columns in the example)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.