First, I am selecting all records on page-load like this:
OleDbCommand objCmd = new OleDbCommand("SELECT * FROM MyTable", mycon);
objCmd.CommandType = CommandType.Text;
DataTable dtTemp = new DataTable();
OleDbDataReader rd = objCommand.ExecuteReader();
if (rd.HasRows)
{
dtTemp.Load(rd);
gvTemp.DataSource = dtTemp;
gvTemp.DataBind();
}
In GridView(gvTemp), I am passing MemID in CommandArgument like this:
<asp:TemplateField HeaderText="Action" ItemStyle-Width="80px">
<ItemTemplate>
<asp:LinkButton ID="lbtnStart" runat="server" CommandArgument='<%# Eval("MemID") %>' CommandName="Start">Start</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
So when I click on Start, gvTemp_RowCommand is fired and I want to get CommandArgument (MemID). So I am doing this to achieve it:
protected void gvTemp_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Start")
{
Response.Redirect("AddMem.aspx?MemID=" + e.CommandArgument.ToString());
}
}
This work fine. But problem is here.
When user search for a particular record, I reload gvTemp like this:
OleDbCommand objCmd = new OleDbCommand("SELECT * FROM MyTable WHERE MemID = ?", mycon);
objCmd.Parameters.Add(myParam);
objCmd.CommandType = CommandType.Text;
DataTable dtTemp = new DataTable();
OleDbDataReader rd = objCommand.ExecuteReader();
if (rd.HasRows)
{
dtTemp.Load(rd);
gvTemp.DataSource = dtTemp;
gvTemp.DataBind();
}
Executing this query, I am getting only one record. And when I click on Start, gvTemp_RowCommand is fired. But I am getting e.CommandArgument of old DataSource. (i.e. The top 1 MemID of SELECT * FROM MyTable). So before binding actual DataSource I clear the GridView like this:
gvTemp.DataSource = null;
gvTemp.DataBind();
gvTemp.DataSource = dtTemp;
gvTemp.DataBind();
But still I am getting e.CommandArgument of old DataSource. So what am I missing? Or is there any alternative method to achieve the same?
On every button click a postback occurs and the page load event occurs so in your case this might be the reason why previous data source is being loaded. so you must check IsPostBack condition.
Try something like this: