Click here to Skip to main content
15,892,965 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how can i update and delete a row in gridview. i have four fields namely empid,empname,salary,city.
when i click on update button error shows that update command is not specified.what code can i use on gridview1_rowupdating event of gridview.
Posted
Updated 5-Jun-13 21:36pm
v2
Comments
Rockstar_ 6-Jun-13 3:20am    
Have u given update command in the design code of Gridview?

 
Share this answer
 
This Worked for me:


C#
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
            GridViewRow row = GridView1.Rows[e.RowIndex];
            string constr = ConfigurationManager.ConnectionStrings["connstr"].ToString();
            SqlConnection con = new SqlConnection(constr);

            string id = (((Label)row.FindControl("lblid")).Text).ToString();

            string comments = (((TextBox)row.FindControl("txtcomments")).Text).ToString();
            con.Open();
            SqlCommand CmdSql = new SqlCommand("Update leave set admin_comments='" + comments + "', granted='" + true + "' where id=" + id + " ", con);
                    CmdSql.ExecuteNonQuery();
                    GridView1.EditIndex = -1;
                    con.Close();
                    gridpop();

}
 
Share this answer
 
v2
Follow this code..
C#
public partial class DataTrialForm : Form
    {
        private String connectionString = null;
        private SqlConnection sqlConnection = null;
        private SqlDataAdapter sqlDataAdapter = null;
        private SqlCommandBuilder sqlCommandBuilder = null;
        private DataTable dataTable = null;
        private BindingSource bindingSource = null;
        private String selectQueryString = null;

        public DataTrialForm()
        {
            InitializeComponent();
        }

        private void DataTraiForm_Load(object sender, EventArgs e)
        {
          sqlConnection = new SqlConnection("data source=SEZ-WS-137\\SQLEXPRESS2008;initial catalog=AttendanceData;user id=sa;password=pass");

            selectQueryString = "select top 20 * from Attendance  order by punchtime asc";

            sqlConnection.Open();

            sqlDataAdapter = new SqlDataAdapter(selectQueryString, sqlConnection);
            sqlCommandBuilder = new SqlCommandBuilder(sqlDataAdapter);

            dataTable = new DataTable();
            sqlDataAdapter.Fill(dataTable);

            bindingSource = new BindingSource();
            bindingSource.DataSource = dataTable;

            dataGridViewTrial.DataSource = bindingSource;

           // to hide Identity column
            dataGridViewTrial.Columns[0].Visible = false;
        }

        private void addUpadateButton_Click(object sender, EventArgs e)
        {
            try
            {
                sqlDataAdapter.Update(dataTable);
            }
            catch (Exception exceptionObj)
            {
                MessageBox.Show(exceptionObj.Message.ToString());
            }
        }

       private void deleteButton_Click(object sender, EventArgs e)
        {
            try
            {
               dataGridViewTrial.Rows.RemoveAt(dataGridViewTrial.CurrentRow.Index);
               sqlDataAdapter.Update(dataTable);
            }
            catch (Exception exceptionObj)
            {
               MessageBox.Show(exceptionObj.Message.ToString());
            }
        }
    }

Also see it..
http://www.mindstick.com/Articles/9422cfc8-c2ed-4ec1-9fab-589eb850a863/?Insert%20Delete%20Update%20in%20DataGridView%20with%20DataTable%20in%20C[^]
 
Share this answer
 
v2
 
Share this answer
 
Comments
Rambo_Raja 6-Jun-13 4:17am    
no i want this to be done without using sqldatasource.
uspatel 6-Jun-13 4:22am    
can you share your code you are using

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