?
Solved

Unable to save back to MySQL from Gridview

Posted on 2010-01-12
6
Medium Priority
?
256 Views
Last Modified: 2013-11-27
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

0
Comment
Question by:k1ss0ff
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 11

Expert Comment

by:princeatapi
ID: 26295427
Use Your BindData() inside if (!Page.IsPostBack)

After Updates you can Cll the BindData() function to ensure the DB changes
0
 

Author Comment

by:k1ss0ff
ID: 26295534
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
 
LVL 11

Accepted Solution

by:
princeatapi earned 2000 total points
ID: 26295567
k try replace with this

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

 You have written Nothing inside the SelectedInedxChanged ??
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:RameshS
ID: 26295614
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
 
LVL 10

Expert Comment

by:RameshS
ID: 26295699
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
 
LVL 10

Expert Comment

by:codingbeaver
ID: 26296199
Is newbuttonUpdate defined outside the gridview?
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question