Solved

Update Gridview table from MySQL database in Visual Studio

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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# Json POSt to Rest API 4 31
Server Error 11 47
Receiving a string from a WebService Push 21 31
Counting connections to SQL Server through C# 3 23
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!
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.
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 …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

932 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

11 Experts available now in Live!

Get 1:1 Help Now