gridview rowupdating using parametrized query.

  • 9 years ago

    Hi

    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.
    Thanks

     protected void gv_RowUpdating1(object sender, GridViewUpdateEventArgs e)
        {
            connectAccessDb();
            int i = e.RowIndex;
            //par = cmd.Parameters;
            //cmd.Parameters.Clear();
            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;

            cmd.ExecuteNonQuery();
            cmd.ExecuteNonQuery();
            //gv.EditIndex = -1;
            cn.Close();
            bindData();

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

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

  • 9 years ago

    Hi,

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

     

    Pramod

     

     

  • 9 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).

Contribute

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.

“A computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are, in short, a perfect match” - Bill Bryson