Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is my code for updating and saving from grid view data directly.But it is not working.
Help me out to update record.thanks in advance.Where i am making mistake im not able to find out.
C#
protected void UpdateRecord(object sender, GridViewUpdateEventArgs e)
   {
       string id = gridRegistrationTableDetails.Rows[e.RowIndex].Cells[0].Text;
       string name=((TextBox)gridRegistrationTableDetails.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
       string address = ((TextBox)gridRegistrationTableDetails.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
       string dept = ((TextBox)gridRegistrationTableDetails.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
       string mail = ((TextBox)gridRegistrationTableDetails.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
       updaterecordMethod(name,address,dept,mail);
       gridRegistrationTableDetails.EditIndex = -1;
       BindData();
   }

   private void updaterecordMethod(string name,string address,string dept,string mail)
   {
       string UpdateQuery = "update SignUP set Employee_Name=@Empname,Employee_Address=@EmpAddress,Employee_Dept=@EmpDept,Employee_Mail=@EmpMail";
       try
       {
           SqlConObject.Open();
           SqlCommand cmd = new SqlCommand(UpdateQuery, SqlConObject);
           cmd.Parameters.AddWithValue("@Empname", name);
           cmd.Parameters.AddWithValue("@EmpAddress", address);
           cmd.Parameters.AddWithValue("@EmpDept", dept);
           cmd.Parameters.AddWithValue("@EmpMail", mail);
           cmd.CommandType = CommandType.Text;
           cmd.ExecuteNonQuery();
       }
       finally
       {
           SqlConObject.Close();
       }

   }
Posted
Updated 24-Jul-12 2:00am
v2
Comments
Prasad_Kulkarni 24-Jul-12 8:02am    
Is it giving any error?
Have you tried using debugger??
..and what is condition for updating records? which record this query will update?
_Amy 24-Jul-12 8:03am    
It is correct only.. What the error you are getting?

Please check out the Casting of the Gridview values. you are assigning values to string variable. I am sure you are getting error while building a page.

C#
protected void UpdateRecord(object sender, GridViewUpdateEventArgs e)
    {
        string id = gridRegistrationTableDetails.Rows[e.RowIndex].Cells[0].Text;
        Textbox name=((TextBox)gridRegistrationTableDetails.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
        Textbox address = ((TextBox)gridRegistrationTableDetails.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
        Textbox dept = ((TextBox)gridRegistrationTableDetails.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
        Textbox mail = ((TextBox)gridRegistrationTableDetails.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
        updaterecordMethod(name.Text.Trim(),address.Text.Trim(),dept.Text.Trim(),mail.Text.Trim());
        gridRegistrationTableDetails.EditIndex = -1;
        BindData();
    }



Try this


Thanks
Ashish
 
Share this answer
 
v2
this code is not working Ashish..!
 
Share this answer
 
i got it. this is the code....

C#
protected void UpdateRecord(object sender, GridViewUpdateEventArgs e)
    {
        string id = gridRegistrationTableDetails.Rows[e.RowIndex].Cells[2].Text;
        string name = ((TextBox)gridRegistrationTableDetails.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
        string address = ((TextBox)gridRegistrationTableDetails.Rows[e.RowIndex].Cells[4].Controls[0]).Text;
        string dept = ((TextBox)gridRegistrationTableDetails.Rows[e.RowIndex].Cells[5].Controls[0]).Text;
        string mail = ((TextBox)gridRegistrationTableDetails.Rows[e.RowIndex].Cells[6].Controls[0]).Text;
        updaterecordMethod(name, address, dept, mail);
        gridRegistrationTableDetails.EditIndex = -1;
        BindData();
    }
    private void updaterecordMethod(string name,string address,string dept,string mail)
    {
        string UpdateQuery = "update ISSSignUP set Employee_Name=@Empname,Employee_Address=@EmpAddress,Employee_Email=@EmpMail,Employee_Department=@EmpDept";
        try
        {
            SqlConObject.Open();
            SqlCommand cmd = new SqlCommand(UpdateQuery, SqlConObject);
            cmd.Parameters.AddWithValue("@Empname", name);
            cmd.Parameters.AddWithValue("@EmpAddress", address);
            cmd.Parameters.AddWithValue("@EmpMail", mail);
            cmd.Parameters.AddWithValue("@EmpDept", dept);
            cmd.CommandType = CommandType.Text;
            cmd.ExecuteNonQuery();
        }
        finally
        {
            SqlConObject.Close();
        }

    }
 
Share this answer
 

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