Solved

Update Gridview table from MySQL database in Visual Studio

Posted on 2015-02-20
2
567 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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 fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now