gridview rowupdating using parametrized query.

  • 12 years ago


    I have gridview and I am using gridview1_rowupdating event to update the gridview, but when I am using the code given below its not working. I do not know why it is not updating, it is not even showing error. please help me resolve this problem.

     protected void gv_RowUpdating1(object sender, GridViewUpdateEventArgs e)
            int i = e.RowIndex;
            //par = cmd.Parameters;
            string id = (gv.Rows[e.RowIndex].FindControl("lblCID") as Label).Text;
            string comp = (gv.Rows[e.RowIndex].FindControl("txtCName") as TextBox).Text;
            string address = (gv.Rows[e.RowIndex].FindControl("txtCAddr") as TextBox).Text;
            string city = (gv.Rows[e.RowIndex].FindControl("txtCcity") as TextBox).Text;
            //string strQuery="UPDATE companydetail SET companyname='" + comp + "',companyaddress='" + address + "',companycity='" + city + "' where id=" + Convert.ToInt32(id);
            //dCmd = new OleDbCommand(strQuery, cn);
            //dCmd = new OleDbCommand("UPDATE companydetail SET companyname=@comp, companyaddress=@address, companycity=@city where id=@id", cn);
            string strQuery = "UPDATE companydetail SET companyname=@comp, companyaddress=@address, companycity=@city where id=@id";
            cmd = new OleDbCommand(strQuery, cn);
            cmd.Parameters.Add("@id", OleDbType.Integer).Value = Convert.ToInt32(id);
            cmd.Parameters.Add("@comp", OleDbType.VarChar).Value = comp;
            cmd.Parameters.Add("@address", OleDbType.VarChar).Value = address;
            cmd.Parameters.Add("@city", OleDbType.VarChar).Value = city;

            //gv.EditIndex = -1;

     private void bindData()
            da = new OleDbDataAdapter("SELECT id,companyname,companyaddress,companycity  from companydetail order by companyname", cn);       
            da.Fill(ds, "companydetail");
            gv.DataSource = ds;
    private void connectAccessDb()

            cn = new OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=" + strRootDir + "App_Data\\newsdb.mdb");

  • 12 years ago


    Did you check that the event is getting fired by setting the break point?





  • 12 years ago

     ya the event is getting fired and every line is working fine, but it is not updating.

    Thanks for your reply.

Post a reply

Enter your message below

Sign in or Join us (it's free).


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“XML is like violence - if it's not working for you, you're not using enough of it.”