Solved

Update Gridview table from MySQL database in Visual Studio

Posted on 2015-02-20
2
605 Views
Last Modified: 2015-04-02
I am attempting to update a Gridview but can't seem to get the proper syntax. I tested this in the data source and it works
SelectCommand="SELECT Employee.* FROM Employee WHERE (empLName = @empLName)"

UpdateCommand="UPDATE Employee SET userName = @userName, password = @password, empPermissions = @empPermissions, empFName = @empFName, empLName = @empLName, empPhone = @empPhone, empEmail = @empEmail WHERE (empID = @empID)">

Open in new window


But I want to use C# to update the Gridview rows. Here is the update method:
public static bool updateEmpManage(System.Web.UI.WebControls.GridView gvEmployee, string empLName)
    {




        MySqlConnection connection = new MySqlConnection("server=;user id=;database=;;persistsecurityinfo=True;Allow User Variables=True");
        connection.Open();

       using( MySqlCommand cmd = new MySqlCommand(String.Format("UPDATE Employee SET userName = @userName, password = @password, empPermissions = @empPermissions, empFName = @empFName, empLName = @empLName, empPhone = @empPhone, empEmail = @empEmail WHERE empLName = '{0}'", empLName), connection))
        //using (MySqlCommand cmd = new MySqlCommand(query, connection);
        {



            cmd.Parameters.AddWithValue("@userName", userName);
            cmd.Parameters.AddWithValue("@password", password);
            cmd.Parameters.AddWithValue("@empPermissions", empPermissions);
            cmd.Parameters.AddWithValue("@empFName", empFName);
            cmd.Parameters.AddWithValue("@empLName", empLName);
            cmd.Parameters.AddWithValue("@empPhone", empPhone);
            cmd.Parameters.AddWithValue("@empEmail", empEmail);
            cmd.ExecuteNonQuery();
        }

       

        return true;



    }

Open in new window


Here is the method calling my method calling the update:
 protected void gvEmployee_RowUpdating(object sender, GridViewUpdateEventArgs e)
       {
           clsDataLayer.updateEmpManage(gvEmployee, empLName.Text );
        }

Open in new window


There seems to be several ways to get this to work but I just can't figure it out. The goal is to be able to update and/or delete the row. Any help will be greatly appreciated. I've been stuck trying to get this working for hours.
0
Comment
Question by:Robin Harris
2 Comments
 
LVL 32

Expert Comment

by:Robberbaron (robr)
ID: 40625403
seems strange that you are pass the entire gridview object to the update routine.
you need to pass just the row being acted upon.  its in the GridViewUpdateEventArgs  e.Row

or better still, extract the data from the row in the RowUpdating method and pass strongly typed objects to the update method,

e.Row.Cells[xx]  will give you the items for each of the update parametes i suspect.
0
 
LVL 10

Accepted Solution

by:
Jini Jose earned 500 total points
ID: 40630144
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

776 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