Unable to save back to MySQL from Gridview

I've created an ASP.NET page.  I have put the whole page into Edit mode, but when I click the button which calls the Update SQL statement, the parameters are (I think) being overwritten with the original values in the database.

I think I need to put an IsPostBack statement in there, but I can't seem to do that without losing the GridView altogether.

Help please!!

Thanks
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Odbc;
using System.Collections;
using System.Configuration;
using System.Web.Security;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace AT
{
    public class BulkEditGridView : System.Web.UI.WebControls.GridView
    {
        protected override GridViewRow CreateRow(int rowIndex, int dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState)
        {
            return base.CreateRow(rowIndex, dataSourceIndex, rowType, rowState | DataControlRowState.Edit);
        }
    }
    public partial class Default : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {

            // if (!Page.IsPostBack)
            {
                BindData();
            }

        }

        private void BindData()
        {
            DataSet ds = new DataSet();

            using (OdbcConnection conn = new
          OdbcConnection("DRIVER={MySQL ODBC 5.1 Driver};database=actiontracker;server=localhost;uid=root;pwd=tracker1;Port=3306;"))
            {
                OdbcCommand cmd = new OdbcCommand
                    ("(SELECT manddataid,mandactionid,action,yes,no,notapp,comment,MAX(lastupdated),tocid FROM mandatory, mandatoryqndata WHERE (mandatory.mandid = mandatoryqndata.mandid) GROUP BY mandatoryqndata.tocid, mandatory.mandactionid HAVING (mandatoryqndata.tocid = ?))", conn);

                cmd.Parameters.Add("?tocid", SqlDbType.Int);
                cmd.Parameters["?tocid"].Value = DropDownList1.SelectedValue;
                OdbcDataAdapter da = new OdbcDataAdapter(cmd);
                da.Fill(ds);

                GridView2.DataSource = ds;
                GridView2.DataBind();
            }
        }
                     
        public void newbuttonUpdate_Click(object sender, EventArgs e)
        {
            using (OdbcConnection conn = new OdbcConnection("DRIVER={MySQL ODBC 5.1 Driver};database=actiontracker;server=localhost;uid=root;pwd=tracker1;Port=3306;")) ;
        
            UpdateInfo();
        }

        public void UpdateInfo()
        {
            using (OdbcConnection conn = new OdbcConnection("DRIVER={MySQL ODBC 5.1 Driver};database=actiontracker;server=localhost;uid=root;pwd=tracker1;Port=3306;"))

            using (OdbcCommand cmd = new OdbcCommand("UPDATE mandatoryqndata SET yes=?, no=?, notapp=? WHERE tocid=? and manddataid=?;", conn))
            {
                for (int i = 0; i < GridView2.Rows.Count; i++)
                {
                    cmd.Parameters.Add("?yes", OdbcType.Int).Value = (((RadioButton)GridView2.Rows[i].FindControl("rdYes")).Checked);
                    cmd.Parameters.Add("?no", OdbcType.Int).Value = (((RadioButton)GridView2.Rows[i].FindControl("edNo")).Checked);
                    cmd.Parameters.Add("?notapp", OdbcType.Int).Value = (((RadioButton)GridView2.Rows[i].FindControl("edNotApp")).Checked);
                    cmd.Parameters.Add("?", OdbcType.Int).Value = Convert.ToInt32(((TextBox)GridView2.Rows[i].FindControl("tocidtb")).Text);
                    cmd.Parameters.Add("?", OdbcType.Int).Value = Convert.ToInt32(((TextBox)GridView2.Rows[i].FindControl("manddataidtb")).Text);
                }


                conn.Open();
                cmd.ExecuteNonQuery();





            }


        }

        protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            using (OdbcConnection conn = new OdbcConnection("DRIVER={MySQL ODBC 5.1 Driver};database=actiontracker;server=localhost;uid=root;pwd=tracker1;Port=3306;"))

            using (OdbcCommand cmd = new OdbcCommand("UPDATE mandatoryqndata SET yes=?yes, no=?no, notapp=?notapp WHERE tocid=? and manddataid=?;", conn))
            {
                for (int i = 0; i < GridView2.Rows.Count; i++)
                {
                    cmd.Parameters.Add("?yes", OdbcType.Int).Value = (((RadioButton)GridView2.Rows[i].FindControl("rdYes")).Checked);
                    cmd.Parameters.Add("?no", OdbcType.Int).Value = (((RadioButton)GridView2.Rows[i].FindControl("edNo")).Checked);
                    cmd.Parameters.Add("?notapp", OdbcType.Int).Value = (((RadioButton)GridView2.Rows[i].FindControl("edNotApp")).Checked);
                    cmd.Parameters.Add("?tocid", OdbcType.Int).Value = Convert.ToInt32(((TextBox)GridView2.Rows[i].FindControl("tocidtb")).Text);
                    cmd.Parameters.Add("?manddataid", OdbcType.Int).Value = Convert.ToInt32(((TextBox)GridView2.Rows[i].FindControl("manddataidtb")).Text);
                }


                conn.Open();
                cmd.ExecuteNonQuery();
            }

        }

    }
}

Open in new window

k1ss0ffAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

princeatapiCommented:
Use Your BindData() inside if (!Page.IsPostBack)

After Updates you can Cll the BindData() function to ensure the DB changes
0
k1ss0ffAuthor Commented:
That's what I tried to do in Line 32, I had to comment it out, as if I didn't, then I just get to see my dropdown list.

If I then choose my variable from the drop down list, it doesn't bring the GridView up?
0
princeatapiCommented:
k try replace with this

protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindData();
        }

 You have written Nothing inside the SelectedInedxChanged ??
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

RameshSCommented:
1. Put  the  BindData() method inside if (!Page.IsPostBack)
2.  You are iterating all the rows of GridView in GridView2_RowUpdating. This event is used to update a specific row and not to update all rows.
0
RameshSCommented:
Replace the following code in GridView2_RowUpdating event and try it.


    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        using (OdbcConnection conn = new OdbcConnection("DRIVER={MySQL ODBC 5.1 Driver};database=actiontracker;server=localhost;uid=root;pwd=tracker1;Port=3306;"))
        {
            using (OdbcCommand cmd = new OdbcCommand("UPDATE mandatoryqndata SET yes=?yes, no=?no, notapp=?notapp WHERE tocid=? and manddataid=?;", conn))
            {
                int i = e.RowIndex;

                cmd.Parameters.Add("?yes", OdbcType.Int).Value = (((RadioButton)GridView2.Rows[i].FindControl("rdYes")).Checked);
                cmd.Parameters.Add("?no", OdbcType.Int).Value = (((RadioButton)GridView2.Rows[i].FindControl("edNo")).Checked);
                cmd.Parameters.Add("?notapp", OdbcType.Int).Value = (((RadioButton)GridView2.Rows[i].FindControl("edNotApp")).Checked);
                cmd.Parameters.Add("?tocid", OdbcType.Int).Value = Convert.ToInt32(((TextBox)GridView2.Rows[i].FindControl("tocidtb")).Text);
                cmd.Parameters.Add("?manddataid", OdbcType.Int).Value = Convert.ToInt32(((TextBox)GridView2.Rows[i].FindControl("manddataidtb")).Text);

                conn.Open();
                cmd.ExecuteNonQuery();
            }

        }
    }

Open in new window

0
codingbeaverCommented:
Is newbuttonUpdate defined outside the gridview?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.