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!