Click here to Skip to main content
15,908,674 members
Please Sign up or sign in to vote.
1.70/5 (3 votes)
See more:
i hav gridview in my aspx page ....i already bind values from mysql to grid view ...i want to update the values in gridview and my updated values are get updated in mysql...i tried of many times but my values did't get updated in mysql..this is my aspx and aspx.cs coding pleaselet me know wat mistake i hav done.....

C++
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using MySql.Data.MySqlClient;
using System.IO;


public partial class _Default : System.Web.UI.Page
{
    MySqlConnection con = new MySqlConnection("user id=root; password=admin; database=qms; server=localhost");

   protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            bind();
    }
   public void bind()
   {

       con.Open();
       string q = "select * from exam";
       MySqlDataAdapter da = new MySqlDataAdapter(q, con);
       DataSet ds = new DataSet();
       da.Fill(ds, "emp");
       GridView1.DataSource = ds;
       GridView1.DataBind();
       //con.Close();
   }

    protected void Button1_Click(object sender, EventArgs e)
    {
       bind();
    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        bind();
    }

    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        bind();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
        int id = Int32.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
        TextBox tname = (TextBox)row.FindControl("nam");
        TextBox tques = (TextBox)row.FindControl("que");
        MySqlCommand cmd = new MySqlCommand("update exam set name1=@name,ques=@ques where id = @id", con);
        cmd.Parameters.Add("@id", MySqlDbType.Int16).Value = id;
        cmd.Parameters.Add("@name", MySqlDbType.VarChar, 30).Value = tname.Text.Trim();
        cmd.Parameters.Add("@ques", MySqlDbType.VarChar,40).Value = tques.Text.Trim();
        con.Open();
        cmd.ExecuteNonQuery();
        GridView1.EditIndex = -1;
        bind();
    }
Posted
Updated 12-Oct-17 19:11pm
Comments
m@dhu 2-Mar-11 7:30am    
Are you getting any error? And always close the connection whenever you open it.
shanthikalai 7-Mar-11 4:42am    
Callbacks are not supported on TemplateField because some controls cannot update properly in a callback. Turn callbacks off on 'GridView1'.

i got this error
AKS94 17-May-14 8:54am    
this is not working.
it gaves an error that-index is 0.
AKS94 17-May-14 8:57am    
i got an error
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

Hi,

Are debugge your code?

Put a trace point at
C#
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)



Also verify that you have assign DataKey in gridview
 
Share this answer
 
Hello,

Please use this code in row updating event below..

C#
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
        int id = Int32.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
        string tname = ((TextBox)row.Cells[0].Controls[0]).Text;
        string tques = ((TextBox)row.Cells[1].Controls[0]).Text;
        MySqlCommand cmd = new MySqlCommand("update exam set name1=@name,ques=@ques where id = @id", con);
        cmd.Parameters.Add("@id", MySqlDbType.Int16).Value = id;
        cmd.Parameters.Add("@name", MySqlDbType.VarChar, 30).Value = tname.Text.Trim();
        cmd.Parameters.Add("@ques", MySqlDbType.VarChar, 40).Value = tques.Text.Trim();
        con.Open();
        cmd.ExecuteNonQuery();
        GridView1.EditIndex = -1;
        bind();
}


if you get result then pls give vote for answer..
 
Share this answer
 
Comments
Rahul Dihora RV 11-Sep-13 1:20am    
i get error like "object reference not set to an instance of an object" at third step in parameters like "cmd.Parameters.Add("@name", MySqlDbType.VarChar, 30).Value = tname.Text.Trim();
" this point so what is solution??????
Label lablid = GridView1.Rows[e.RowIndex].FindControl("lblid") as Label;
TextBox txtdes = GridView1.Rows[e.RowIndex].FindControl("txtdescription") as TextBox;
TextBox txtpric = GridView1.Rows[e.RowIndex].FindControl("txtprice") as TextBox;
TextBox txtproduct = GridView1.Rows[e.RowIndex].FindControl("txtproductname") as TextBox;
TextBox txtquant = GridView1.Rows[e.RowIndex].FindControl("txtquantity") as TextBox;
TextBox txttype = GridView1.Rows[e.RowIndex].FindControl("txttype") as TextBox;
 
Share this answer
 
Comments
Member 10429356 2-Dec-13 0:28am    
Row Updated Event in GridView in ASP.NET Example
Refer - [MSDN] GridView.EnableSortingAndPagingCallbacks Property[^].
Quote:
All columns in the Columns collection must support callbacks for this feature to work. If the Columns collection contains a column that does not support callbacks, such as TemplateField, a NotSupportedException exception is raised.
This is the problem.

So, put EnableSortingAndPagingCallbacks = false.
It will solve this issue.
 
Share this answer
 
 
Share this answer
 
v2

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