Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8735959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:11:34+00:00 2026-06-13T10:11:34+00:00

I have a GridView with some data from my database. However, when hovering the

  • 0

I have a GridView with some data from my database.

However, when hovering the delete link, it links to the incorrect ID.

I want it to use the ID from the database.

This question is very much related to this one – but it never received an answer.

My GridView code looks like this:

<asp:GridView ID="GridView1" runat="server" OnRowDeleting="GridView1_RowDeleting1" AutoGenerateColumns="false" DataKeyNames="id">
    <Columns>
      <asp:BoundField HeaderText="Name" DataField="name" />
      <asp:BoundField HeaderText="Email" DataField="email" />
      <asp:BoundField HeaderText="Comment" DataField="comment" />
      <asp:CommandField ShowDeleteButton="True"  />
    </Columns>
</asp:GridView>

How do I make the DeleteButton link to the correct ID?

Update

Page-load code that puts the data in the datatable:

    protected void Page_Load(object sender, EventArgs e)
    {
        DataTable commentsTable = null;
        commentsTable = new DataTable("Comments");
        using (SqlDataReader reader = studentManager.getCommentsFromDB())
        {
            commentsTable.Load(reader);
            GridView1.DataSource = commentsTable;
            GridView1.DataBind();
        }
    }

getCommentsFromDB:

    public SqlDataReader getCommentsFromDB()
    {
        SqlConnection conn = dal.connectDatabase();
        conn.Open();
        cmd = new SqlCommand(@"SELECT id, name, email, comment FROM GuestBook", conn);
        rdr = cmd.ExecuteReader();
        return rdr;
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T10:11:35+00:00Added an answer on June 13, 2026 at 10:11 am

    There’s a number of ways you can go about doing this. I tend to use an ASP.NET Button and set the CommandName property to "Delete".

    <asp:Button ID="btnDelete" runat="server" CommandName="Delete" />
    

    or

    <asp:ImageButton ID="ibtnDelete" runat="server" ImageUrl="~/Images/Delete.gif" CommandName="Delete" />
    

    or

    <asp:CommandField ButtonType="Button" DeleteText="Delete" ShowDeleteButton="true" />
    

    Then it’s just a matter of wiring up the event that handles OnRowDeleting.

    protected void GridView1_RowDeleting1(object sender, GridViewDeleteEventArgs e) {
        // Get the id
        string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
    
        // Create the delete query
        string sql = @"delete from GuestBook where id = @id";
    
        using (SqlConnection conn = dal.connectDatabase()) {
    
            cmd = new SqlCommand(sql, conn);
            cmd.Parameters.Add("@id", SqlDbType.Int);
            cmd.Parameters["@id"].Value = id;
    
            try {
                conn.Open();
                cmd.ExecuteScalar();
            }
            catch (Exception ex) {
                Console.WriteLine(ex.Message);
            }
        }
    
        // Refresh the GridView1
        Bind_GridView1();
    }
    

    … and the method to call each time that you want to refresh the GridView. It’s much cleaner to have your GridView binding call in it’s own method that you can call from anywhere in the page (which means that you can call it from your page load event as well).

    public void Bind_GridView1() {
            DataTable commentsTable = null;
            commentsTable = new DataTable("Comments");
            using (SqlDataReader reader = studentManager.getCommentsFromDB())
            {
                commentsTable.Load(reader);
                GridView1.DataSource = commentsTable;
                GridView1.DataBind();
            }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a GridView which retrieves data from a database. Users have roles, some
I have a gridview which is pulling some data from a database. One of
I have Gridview and I want to retrieve some Information from data base(SQL). and
I have a Gridview within an UpdatePanel that shows some data from my Database.
I have a GridView that gets it's data from the SQL database . I
I have compared two queries which fetch some fairly large data from a database
So basically I have data being pulled from a database for which I want
So my question is this, I have a gridview that filters data from a
I have the following code to insert some data from a GridView to a
I have a page that uses GridView to display some data taken from a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.