Insert into MySql using GridView imagebutton from labels C#

I want to fetch the data from labels in Gridview and insert them to MySql database. When users click the imagebutton, the data will be inserted to MySql DB. I can't seem to make it happen with these codes:

//The Backend
   private void Insert(string bookTitle, string name, string date, string memberID, string phone)
        {
            try
            {
                conn.Open();
                string q4 = "insert into c_records (bookTitle, date, name, memberID, phone) values (@bookTitle, @date, @name, @memberID, @phone);";
                string q5 = "delete customers from customers where name = @name and memberID = @memberID;";
                MySqlCommand cmd = new MySqlCommand(q4 + q5, conn);
                cmd.Parameters.AddWithValue("@date", date);
                cmd.Parameters.AddWithValue("@memberID", memberID);
                cmd.Parameters.AddWithValue("@phone", phone);
                cmd.Parameters.AddWithValue("@book", bookTitle);
                cmd.Parameters.AddWithValue("@name", name);
                cmd.Parameters["@book"].Value = bookTitle;
                cmd.Parameters["@date"].Value = date;
                cmd.Parameters["@name"].Value = name;
                cmd.Parameters["@memberID"].Value = memberID;
                cmd.Parameters["@phone"].Value = phone;
                MySqlDataAdapter adp = new MySqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                adp.Fill(ds);
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            conn.Close();
        }

        protected void GridView1_InsDel(object sender, GridViewCommandEventArgs e)
        {
            // If multiple buttons are used in a GridView control, use the
            // CommandName property to determine which button was clicked.
            if (e.CommandName == "Insert")
            {
                // Convert the row index stored in the CommandArgument
                // property to an Integer.
                //   int index = Convert.ToInt32(e.CommandArgument);

                // Retrieve the row that contains the button clicked 
                // by the user from the Rows collection.      
                //GridViewRow row = GridView1.Rows[index];
                int i;
                for (i = 0; i < GridView1.Rows.Count; i++)
                {
                    string bookTitle = GridView1.Rows[i].Cells[0].ToString();
                    string date = GridView1.Rows[i].Cells[1].ToString();
                    string name = GridView1.Rows[i].Cells[2].ToString();
                    string memberID = GridView1.Rows[i].Cells[3].ToString();
                    string phone = GridView1.Rows[i].Cells[4].ToString();

                    MySqlCommand cmd = new MySqlCommand("INSERT INTO c_records (bookTitle, date, name, memberID, phone) VALUES (@bookTitle, @date, @name, @memberID, @phone)", conn);
                    MySqlParameter book = new MySqlParameter("@bookTitle", bookTitle);
                    MySqlParameter dt = new MySqlParameter("@date", date);
                    MySqlParameter n = new MySqlParameter("@name", name);
                    MySqlParameter mb = new MySqlParameter("@memberID", memberID);
                    MySqlParameter p = new MySqlParameter("@phone", phone);

                    cmd.Parameters.Add(book);
                    cmd.Parameters.Add(dt);
                    cmd.Parameters.Add(n);
                    cmd.Parameters.Add(mb);
                    cmd.Parameters.Add(p);
                    

                    cmd.ExecuteNonQuery();


                    Insert(bookTitle, date, memberID, phone, name);
                    GridView1.EditIndex = -1;
                    BindData();

                }
            }

        }

Open in new window


//The Front End
 <asp:GridView ID="GridView1" runat="server" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" AutoGenerateColumns="False"  Width="721px" showfooter="true" OnRowUpdating="GridView1_RowUpdating" Height="411px" OnRowCommand="GridView1_InsDel">
            <Columns>
                <asp:TemplateField HeaderText="Id">
                    <ItemTemplate>
                        <asp:Label ID="Label5" runat="server" Text='<%# Eval("id") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Book Title">
                    <ItemTemplate>
                        <asp:Label ID="book" runat="server" Text='<%# Eval("bookTitle") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Date">
                    <ItemTemplate>
                        <asp:Label ID="date" runat="server" Text='<%# Eval("date") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Name">
                    <ItemTemplate>
                        <asp:Label ID="name" runat="server" Text='<%# Eval("name") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Member ID">
                    <ItemTemplate>
                        <asp:Label ID="memberID" runat="server" Text='<%# Eval("memberID") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Phone">
                    <ItemTemplate>
                        <asp:Label ID="phone" runat="server" Text='<%# Eval("phone") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
               
            
                <asp:TemplateField HeaderText="Sign Out">
                    <ItemTemplate>
                        <asp:ImageButton OnClientClick="return abc(this);" ID="signOut" CommandName="Insert" AlternateText="Available" ImageUrl="~/Images/unclick.png" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
               
            
                <asp:TemplateField>

                    <ItemTemplate>
                        <asp:LinkButton ID="submit" runat="server" Text="Submit" CommandName="Update"><img src="Images/save.jpg" /></asp:LinkButton>
                    </ItemTemplate>

                </asp:TemplateField>

Open in new window


Please don't hesitate to tell me if there are any other solution.
K AkemiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

srikanthreddyn143Commented:
You don't need dataadapter for insert and delete.

You can use executenonquery method on command object.

cmd.ExecuteNonQuery()

If you want to use dataadapter check below links. Examples are for sqlcommand but it should be same for mysql as well.

https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldataadapter.insertcommand(v=vs.110).aspx

http://csharp.net-informations.com/dataadapter/deletecommand-sqlserver.htm
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
K AkemiAuthor Commented:
Thank you for the solution. I just noticed about the executenonquery part.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.