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();
            
    }

Recommended Answers

All 2 Replies

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.

cmd = new SqlCommand("update toys set type=@type,item=@item,quan=@quan,price=@price,tid=@tid where s_no=@s_no", con);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.