Hello there,
Case
I have web form that shows some record from database using a gridview.
1. I have given search facility in form using a textbox and button control. When user enter something in textbox and click on button the gridview updates with search results.
2. My gridview is inside a update panel.
3. I have given edit and delete record in using gridview link buttons.When user click on edit link
the page redirect to edit form with record id using query string.
4. The Gridview paging is enabled.
5. I have used dataset to fill the gridview.
While showing all records everything (Search, Edit, Delete) is working fine. But when user search for some record and click on edit link the rowcommand gets command argument of gridview having all record i.e previous state before search and final it redirects to edit form with wrong id.
Code:
Search Button Click Event:
protected void Button1_Click(object sender, EventArgs e)
{
refreshbothdd();
string SearchData;
int SearchIn;
SearchData = TextBox1.Text;
SearchIn = DropDownList3.SelectedIndex;
switch(SearchIn)
{
case 1:
FillGrid(4,SearchData );
break;
case 2 :
FillGrid(5,SearchData );
break;
}
Method to fill the grid according to Different Queries:
public void FillGrid(int temp, string tempData)
{
switch (temp)
{
case 0:
da = new SqlDataAdapter("Select SRNo, DepositeType, PartyName, AccOf, CreditOf, DepositeDt,BankName,BranchName,ChequeNo,Amount,DepositeBy from ChTable order by SRNo desc ", conn);
break;
case 1:
da = new SqlDataAdapter("Select SRNo, DepositeType,PartyName,AccOf,CreditOf,DepositeDt,BankName,BranchName,ChequeNo,Amount,DepositeBy from ChTable where DepositeBy='" + tempData + "' order by SRNo desc", conn);
break;
case 2:
da = new SqlDataAdapter("Select SRNo, DepositeType,PartyName,AccOf,CreditOf,DepositeDt,BankName,BranchName,ChequeNo,Amount,DepositeBy from ChTable where AccOf='" + tempData + "' order by SRNo desc", conn);
break;
case 3:
da = new SqlDataAdapter("Select SRNo, DepositeType,PartyName,AccOf,CreditOf,DepositeDt,BankName,BranchName,ChequeNo,Amount,DepositeBy from ChTable where DepositeType='" + tempData + "' order by SRNo desc", conn);
break;
case 4:
da = new SqlDataAdapter("Select SRNo, DepositeType,PartyName,AccOf,CreditOf,DepositeDt,BankName,BranchName,ChequeNo,Amount,DepositeBy from ChTable where ChequeNo like '%" + tempData + "%' order by SRNo desc", conn);
break;
case 5:
da = new SqlDataAdapter("Select SRNo, DepositeType,PartyName,AccOf,CreditOf,DepositeDt,BankName,BranchName,ChequeNo,Amount,DepositeBy from ChTable where PartyName like '%" + tempData + "%' order by SRNo desc", conn);
break;
case 6:
da = new SqlDataAdapter("Select SRNo, DepositeType,PartyName,AccOf,CreditOf,DepositeDt,BankName,BranchName,ChequeNo,Amount,DepositeBy from ChTable where DepositeDt between '" + fromdt.Text + "' and '" + todt.Text + "' order by SRNo desc", conn);
break;
}
GridView1.DataSource = null;
GridView1.DataBind();
ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}
The GridView Rowcommand For Edit:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Edit")
{
string SRID = e.CommandArgument.ToString();
Response.Redirect("EditTransact.aspx/?Transaction=" + SRID + "");
}
}
[Edit] Code posted in the comment is added here for better understanding[/Edit]