Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In grid view i have edit option,if i click on edit, particular row is going to edit screen and filling textboxes and updating as expected.When clicking on add new record button in grid view,it is going to add screen with empty textboxes as expected but if i add a new record,it is updating the previous record which was edited.
If i click on add new record button, after filling textboxes it should add new record but it is updating the old record which edited just before adding new record.
If i edit record and update it, it is correctly updating and coming back to gridview and highlighting that edited record but now if i try to add new record,it is updating the highlighted record.

What I have tried:

C#
protected void GV_PRIOR_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
    int pid = Convert.ToInt32(GV_PRIOR.DataKeys[e.NewSelectedIndex].Value);
    DataTable dt = bll.editprior(pid);
    foreach (DataRow dr in dt.Rows)
    {
        HiddenField1.Value = dr["P_ID"].ToString();
        tb_prioname.Text = dr["P_NAME"].ToString();
        chk_actprior.Checked = dr["P_ACTIVE"].ToString() == "Y";
    }
    btn_savprior.Text = "UPDATE";                
}

 protected void btn_savprior_Click(object sender, EventArgs e)
{
    if (HiddenField1.Value == "")
    {
        bll.savprior(0, tb_prioname.Text, Convert.ToBoolean(chk_actprior.Checked));
        Response.Write("<script>alert('Priority Saved Successfully')</script>");

    }
    else
    {
        bll.savprior(Convert.ToInt16(HiddenField1.Value), tb_prioname.Text, Convert.ToBoolean(chk_actprior.Checked));
        Response.Write("<script>alert('Priority Updated Successfully')</script>");
    }
    bindgrid();              
}


Stored Procedure:

SQL
  ALTER PROC [dbo].[SAVEPRIOR]
@P_ID int,
@P_NAME varchar(20),
@P_ACTIVE char(1)
AS
BEGIN
IF(@P_ID=0)
BEGIN
INSERT INTO PRIORITY(P_NAME) VALUES (@P_NAME)
END
ELSE
BEGIN
UPDATE PRIORITY SET P_NAME=@P_NAME,P_ACTIVE=@P_ACTIVE WHERE P_ID=@P_ID
END
END


DAL:

C#
public void savePRIOR(int id, string priorname, bool actv )
    {
        SqlCommand cmd = new SqlCommand("SAVEPRIOR", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@P_ID", id);
        cmd.Parameters.AddWithValue("@P_NAME", priorname);
        cmd.Parameters.AddWithValue("@P_ACTIVE", (actv) ? "Y" : "N");            
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
    }



BAL:

C#
public void savprior(int id, string priorname, bool actv )
   {
       dll.savePRIOR(id, priorname, actv);
   }
Posted
Updated 1-May-19 6:08am
v5

1 solution

The problem seems to be on the id you're passing along to SQL.

Since you created a new record, the id has not changed yet so in your case, you need to set
HiddenField1.Value = ""
before calling 'btn_savprior_Click()' in order to go into that 'if' (Also, I would change the alert as 'Created' instead of 'Saved'):
protected void btn_savprior_Click(object sender, EventArgs e)
{
    if (HiddenField1.Value == "")
    {
        bll.savprior(0, tb_prioname.Text, Convert.ToBoolean(chk_actprior.Checked));
        Response.Write("<script>alert('Priority Created Successfully')</script>");

    }

I can see that you are setting the value on the first function but, if the value is new, the 'P_ID' column is getting another number (maybe), therefore, the value will never be zero.

Please try to calculate the total and store it on a variable something like:
private int CountRows = 0

Then, you call your DataSet and assign a value to the variable
...
CountRows = GV_PRIOR.Rous.Count;
...


After that, you can modify your function like this:
protected void GV_PRIOR_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
    int pid = Convert.ToInt32(GV_PRIOR.DataKeys[e.NewSelectedIndex].Value);
    DataTable dt = bll.editprior(pid);
    foreach (DataRow dr in dt.Rows)
    {
        if (Convert.ToInt32(dr["P_ID"].ToString()) > CountRows)
        {
                HiddenField1.Value = "";
        }
        else
        {
                HiddenField1.Value = dr["P_ID"].ToString();
        }
        tb_prioname.Text = dr["P_NAME"].ToString();
        chk_actprior.Checked = dr["P_ACTIVE"].ToString() == "Y";
    }
    btn_savprior.Text = "UPDATE";                
}


Also, if you want to identify right away if will be a new record instead of an update then, this will be the final code for that function:
protected void GV_PRIOR_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
    int pid = Convert.ToInt32(GV_PRIOR.DataKeys[e.NewSelectedIndex].Value);
    DataTable dt = bll.editprior(pid);
    foreach (DataRow dr in dt.Rows)
    {
        if (Convert.ToInt32(dr["P_ID"].ToString()) > CountRows)
        {
            HiddenField1.Value = "";
        }
        else
        {
            HiddenField1.Value = dr["P_ID"].ToString();
        }
        tb_prioname.Text = dr["P_NAME"].ToString();
        chk_actprior.Checked = dr["P_ACTIVE"].ToString() == "Y";
    }
    if (HiddenField1.Value == "")
    {
        btn_savprior.Text = "CREATE";
    }
    else
    {
        btn_savprior.Text = "UPDATE";
    }
}
 
Share this answer
 
Comments
Member 14185275 3-May-19 6:51am    
Thank u so much for the reply and the solution is perfectly working.
thank u

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900