Solved

Update Gridview table from MySQL database in Visual Studio

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

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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.

740 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