Solved

Update Gridview table from MySQL database in Visual Studio

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…

752 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