Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

C#
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Insert")
            {

                int i;
                for (i = 0; i < GridView1.Rows.Count; i++)
                {
                    GridViewRow row = GridView1.Rows[i];

                    string bookTitle = (row.FindControl("book") as Label).Text;
                    string date = (row.FindControl("date") as Label).Text;
                    string name = (row.FindControl("name") as Label).Text;
                    string memberID = (row.FindControl("memberID") as Label).Text;
                    string phone = (row.FindControl("phone") as Label).Text;

                    string Query = "insert into c_records (bookTitle, date, name, memberID, phone) values (@bookTitle, @date, @name, @memberID, @phone)";
                    MySqlCommand cmd = new MySqlCommand(Query, conn);
                    cmd.Parameters.AddWithValue("@bookTitle", bookTitle);
                    cmd.Parameters.AddWithValue("@date", date);
                    cmd.Parameters.AddWithValue("@name", name);
                    cmd.Parameters.AddWithValue("@memberID", memberID);
                    cmd.Parameters.AddWithValue("@phone", phone);

                    
                    cmd.ExecuteNonQuery();

                    BindData();
                    
                }
            }

        }


//The Front End

ASP.NET
<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>


Please don't hesitate to tell me if there are any other solution.
Posted
Updated 30-Jul-15 19:43pm
v5

1 solution

You seem to combine both queries in a single command. DOn't do that. Instead run bith separately. You also seem to set the values for the parameters twice. At least when having a quick look I see no reason for that.

Also note that some of the parameters had a mismatch in naming.

So instead of
C#
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);

You should have something like
C#
conn.Open();

string q4 = "insert into c_records (bookTitle, date, name, memberID, phone) values (@bookTitle, @date, @name, @memberID, @phone);";
MySqlCommand cmd = new MySqlCommand(q4, conn);
cmd.Parameters.AddWithValue("@bookTitle", bookTitle);
cmd.Parameters.AddWithValue("@date", date);
cmd.Parameters.AddWithValue("@name", name);
cmd.Parameters.AddWithValue("@memberID", memberID);
cmd.Parameters.AddWithValue("@phone", phone);
cmd.ExecuteNonQuery();

string q5 = "delete customers from customers where name = @name and memberID = @memberID;";
cmd = new MySqlCommand(q5, conn);
cmd.Parameters.AddWithValue("@name", name);
cmd.Parameters.AddWithValue("@memberID", memberID);
cmd.ExecuteNonQuery();
...
 
Share this answer
 
v2
Comments
Hayashi Narumi 31-Jul-15 1:30am    
Thanks...I'm gonna take note of this. It kinda helps but that's not the solution.
I believe that the problem has something to do with the commandname. It hits when i set the breakpoints but the data doesn't appear in mysql database.
Wendelius 31-Jul-15 4:03am    
I don't see what you mean with the command name. If you mean the variable cmd, it can be whatever you like. The name of the variable doesn't affect the execution.

If you place the breakpoint on the line cmd.ExecuteNonQuery and investigate the command text and the values of the parameters, is everything correctly set?
Hayashi Narumi 2-Aug-15 23:02pm    
Oh, what I meant was the GridView's CommandName Event.
Thanks for helping though.
I've solved the problem by adding a new button instead. It makes everything easier.
And since you've helped me with my positioning of query strings, I'll just accept your solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900