Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
<pre><div class="container">
    <asp:GridView ID="GridView1" runat="server"  AutoGenerateColumns="False" Font-Size="Medium">

<asp:TemplateField HeaderText="Student ID">
  <ItemTemplate>
     <asp:Label ID="lblID" runat="server"   Width="80px"  Text='<%#Eval("studentID") %>'/>
 </ItemTemplate>


  <asp:TemplateField HeaderText="">
        <ItemTemplate>                                     
                  <asp:LinkButton runat="server"  OnCommand="LinkButton_Click" Text=" VoteCandidate"> </asp:LinkButton>     
         </ItemTemplate>



 </asp:GridView>

    <div>
        <asp:Label ID="Label1" runat="server" ></asp:Label>
    </div>
</div>


Here is my form to show the gridview.


What I have tried:

C#
protected void loadCandidate()
    {
        con.Open();
        MySqlCommand cmd = new MySqlCommand("select studentID from candidate where faculty='FOCS'", con);
        MySqlDataReader dr = cmd.ExecuteReader();
        if (dr.HasRows == true)
        {
            GridView1.DataSource = dr;
            GridView1.DataBind();
        }
    }


    protected void LinkButton_Click(Object sender, EventArgs e)
    {
        String MyConnection2 = "Server=localhost;database=ovs;Uid=root;password=; Convert Zero Datetime=True";
        foreach (GridViewRow g1 in GridView1.Rows)
        {
            MySqlConnection MyConn2 = new MySqlConnection(MyConnection2);
            
            String query = "insert into voting (studentID)values ('" + g1.Cells[0].Text + "')" ;
            MySqlCommand MyCommand2 = new MySqlCommand(query, MyConn2);
         
          MySqlDataReader MyReader2;
          MyConn2.Open();
          MyReader2 = MyCommand2.ExecuteReader();
          MyConn2.Close();
        }

    }


When I execute the sql insert command, no error occur, but I expect the studentID that displayed in the gridview being stored in the voting table, but the studentID on the voting table are empty
Posted
Updated 4-Aug-20 20:15pm
Comments

1 solution

For Insert, you need to try with ExecuteNonQuery on command and not ExecuteReader. ExecuteReader would not edit/update data in database.

Please read here about ADO.NET and try: Accessing data with ADO.NET[^]

Example:
C#
private void button1_Click(object sender, EventArgs e)
        {
            string connetionString = null;
            SqlConnection cnn ;
            SqlCommand cmd ;
            string sql = null;

            connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password";
            sql = "<Your INSERT SQL Statemnt Here>";

            cnn = new SqlConnection(connetionString);
            try
            {
                cnn.Open();
                cmd = new SqlCommand(sql, cnn);
                cmd.ExecuteNonQuery();
                cmd.Dispose();
                cnn.Close();
                MessageBox.Show (" ExecuteNonQuery in SqlCommand executed !!");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Can not open connection ! ");
            }
        }
 
Share this answer
 
v3

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