Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Unable to save back to MySQL from Gridview

Posted on 2010-01-12
6
Medium Priority
?
261 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
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

578 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