0

whenever i click an update button to update values in a single column of a gridview, all the columnss gets updated with the same values.
eg. if a 1st column of a row with s.no. 1 is updated with values watch,50.,,,rest of the columns of the rows with s.no.2,3,4,5 etc. also gets updated with these same values.
m using this code..
will u plz help me to solve this problem.!!!!

void ItemsGrid_EditCommand(Object source,System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
  ItemsGrid.EditIndex = (int)e.Item.ItemIndex;
        BindGrid();
    }
public void ItemsGrid_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
        //string updatecmd = "update toys set type=@type,item=@item,quan=@quan,price=@price,tid=@tid";
        cmd = new SqlCommand("update toys set type=@type,item=@item,quan=@quan,price=@price,tid=@tid", con);
        cmd.Parameters.Add(new SqlParameter("@type", SqlDbType.NChar, 10));
        cmd.Parameters.Add("@item", SqlDbType.VarChar, 20);
        cmd.Parameters.Add("@quan", SqlDbType.Int);
        cmd.Parameters.Add("@price", SqlDbType.Int);
        cmd.Parameters.Add("@tid", SqlDbType.Int);

        cmd.Parameters["s_no"].Value = ItemsGrid.DataKeys[(int)e.Item.ItemIndex];
       // cmd.Parameters["@s_no"].Value = ((TextBox)e.Item.Cells[0].Controls[0]).Text;

        cmd.Parameters["@type"].Value = ((TextBox)e.Item.Cells[1].Controls[1]).Text;
        cmd.Parameters["@item"].Value = ((TextBox)e.Item.Cells[2].Controls[1]).Text;

        cmd.Parameters["@quan"].Value = ((TextBox)e.Item.Cells[3].Controls[1]).Text;
        cmd.Parameters["@price"].Value = ((TextBox)e.Item.Cells[4].Controls[1]).Text;
        cmd.Parameters["@tid"].Value = ((TextBox)e.Item.Cells[5].Controls[1]).Text;

        try
        {
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
                da.UpdateCommand.ExecuteNonQuery();
                lblmessage.Text="record updated";
                ItemsGrid.EditIndex= -1;
            }
            con.Close();
        }
        catch(SqlException exc)
        {
            lblmessage.Text =exc.ToString()+ "can not update";
        }
        BindGrid();
            
    }
3
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by sknake
0

whenever i click an update button to update values in a single column of a gridview, all the columnss gets updated with the same values.
eg. if a 1st column of a row with s.no. 1 is updated with values watch,50.,,,rest of the columns of the rows with s.no.2,3,4,5 etc. also gets updated with these same values.
m using this code..
will u plz help me to solve this problem.!!!!

void ItemsGrid_EditCommand(Object source,System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
  ItemsGrid.EditIndex = (int)e.Item.ItemIndex;
        BindGrid();
    }
public void ItemsGrid_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
        //string updatecmd = "update toys set type=@type,item=@item,quan=@quan,price=@price,tid=@tid";
        cmd = new SqlCommand("update toys set type=@type,item=@item,quan=@quan,price=@price,tid=@tid", con);
        cmd.Parameters.Add(new SqlParameter("@type", SqlDbType.NChar, 10));
        cmd.Parameters.Add("@item", SqlDbType.VarChar, 20);
        cmd.Parameters.Add("@quan", SqlDbType.Int);
        cmd.Parameters.Add("@price", SqlDbType.Int);
        cmd.Parameters.Add("@tid", SqlDbType.Int);

        cmd.Parameters["s_no"].Value = ItemsGrid.DataKeys[(int)e.Item.ItemIndex];
       // cmd.Parameters["@s_no"].Value = ((TextBox)e.Item.Cells[0].Controls[0]).Text;

        cmd.Parameters["@type"].Value = ((TextBox)e.Item.Cells[1].Controls[1]).Text;
        cmd.Parameters["@item"].Value = ((TextBox)e.Item.Cells[2].Controls[1]).Text;

        cmd.Parameters["@quan"].Value = ((TextBox)e.Item.Cells[3].Controls[1]).Text;
        cmd.Parameters["@price"].Value = ((TextBox)e.Item.Cells[4].Controls[1]).Text;
        cmd.Parameters["@tid"].Value = ((TextBox)e.Item.Cells[5].Controls[1]).Text;

        try
        {
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
                da.UpdateCommand.ExecuteNonQuery();
                lblmessage.Text="record updated";
                ItemsGrid.EditIndex= -1;
            }
            con.Close();
        }
        catch(SqlException exc)
        {
            lblmessage.Text =exc.ToString()+ "can not update";
        }
        BindGrid();
            
    }

in your query you must include the where clause, hope it works.

0
cmd = new SqlCommand("update toys set type=@type,item=@item,quan=@quan,price=@price,tid=@tid where s_no=@s_no", con);
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.