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

  • Home
  • SEARCH
  • 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 6836461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:21:30+00:00 2026-05-26T23:21:30+00:00

I am using VS2005. Currently I have a delete linkButton in my gridview, and

  • 0

I am using VS2005. Currently I have a delete linkButton in my gridview, and if I presses on it to delete a row, the GridView goes empty, and I will have to click on the link again to access the page.

Below is my codefor .aspx:

<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" ondatabound="gv_DataBound" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
        <Columns>
                         <asp:TemplateField>
                            <ItemTemplate>

                              <asp:CheckBox ID="UserSelection" OnCheckedChanged="UserSelector_CheckedChanged" runat="server" />
                                 <asp:LinkButton ID="lnkDelete" runat="server" onclick="lnkDelete_Click" Text="Delete" CommandArgument='<%# Eval("Employee") %>' ></asp:LinkButton>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
    </asp:GridView>

Below is my code for .cs

protected void lnkDelete_Click(object sender, EventArgs e)
    {
        LinkButton lnk = (LinkButton)sender;
        string stid = lnk.CommandArgument.ToString();
        SqlDataSource1.SelectCommand = "DELETE FROM [UserDB]where Employee like '%"+stid+"%'";
        SqlDataSource1.DataBind();

    }

I have tried assigning my delete query to SqlDataSource1 and select query to source2, but if that’s the case, my delete query would not work.

I have also tried using IsPostBack on my PageLoad method, but the GridView goes empty as well after clicking on the delete button.

Currently both of my queries are assigned to SqlDataSource1, and once the query is deleted, the page just go blank, although the query is deleted.

May I know how can I refresh the page or reload the GridView table after the delete query is ran?

Thank you.


Thanks to the help from you guys, the problem is solved. Currently my working code is as follows:

LinkButton lnk = (LinkButton)sender; 
    string stid = lnk.CommandArgument.ToString();
    SqlConnection conn = new SqlConnection("DATA-SOURCE");
    string sql = string.Format("DELETE FROM [UserDB] where Employee like '%{0}%'",stid);
    SqlCommand cmd = new SqlCommand(sql,conn);
    conn.Open();
    cmd.ExecuteNonQuery();
    conn.Close();
    SqlDataSource1.SelectCommand = "SELECT * FROM [UserDB]";
    SqlDataSource1.DataBind();
    Response.Redirect("/Project/UserList.aspx");

I used a sql connection string for the delete query, and sqldatasource for the select query afterwards, finally refreshed the page by using response.redirect. Hope it helped newbies like me who met this error too.

Great thanks to all who provided help

  • 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-05-26T23:21:31+00:00Added an answer on May 26, 2026 at 11:21 pm

    Try like this

    protected void lnkDelete_Click(object sender, EventArgs e)  
    { 
            LinkButton lnk = (LinkButton)sender; 
            string stid = lnk.CommandArgument.ToString(); 
            SqlConnection conn = new SqlConnection(<put your connectionstring here>);
            string sql = string.Format("DELETE FROM [UserDB] where Employee like '%{0}%'",stid);
            SqlCommand cmd = new SqlCommand(sql,conn);
            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();   
            BindTheGridView();
    
                //or you can use SelectCommand of your SqlDataSource1 and can bind again simply.
    } 
    

    EDIT:

    public void BindTheGridView()
        {
            SqlConnection conn = new SqlConnection(Connstring);
            string sql = "Select * from [UserDB]";
            SqlCommand cmd = new SqlCommand(sql, conn);
            conn.Open();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dtb = new DataTable();
            da.Fill(dtb);
            conn.Close();
            SqlDataSource1.DataSource = dtb;
            SqlDataSource1.DataBind();
        }
    

    And you can call this for any time to bind the values.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using VS2005 C#. Currently I have a GridView, and I have changed
I have written a DLL in C# using VS2005. Currently the DLL is showing
I have to build some C source files using Visual Studio (currently using VS2008)
I'm currently using VS2005 Profesional and .NET 2.0, and since our project is rather
I'm currently using default implementation of STL for VS2005 and I'm not really satisfied
I have a small website developed using VS2005 and mySQl, it's just 2 webforms
I have a Windows 7 machine on which I am using VS2005. .Net 2.0
I am using VS2005 .NET 2.0 C#. I am currently managing my user roles
I am using VS2005 .NET 2.0 C#. I am currently referencing to a website
I have 2 questions I am new to C# and I am using VS2005.

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.