Click here to Skip to main content
16,021,211 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have table in sqlserver i want to delete rows through gridview using sqlserver in vb.net thnx in advance
Posted
Comments
__TR__ 30-Nov-12 1:59am    
This is not a good question. Use "Improve question" widget to update your question and provide more details on what is it you need help with.

1 solution

Use the following steps.

1) assign ID's (Primary Key) to Gridview
2) Use Gridview Row Deleting event.(using Command name)
3) Get the ID of row clicked by user to Delete.
4) Use ID in delete sql query & execute sql command.
5) Bind Gridview after successful deletion of row.

Use the following code.
C#
protected void gvAcceptanceStatus_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string ObvID =gvAcceptanceStatus.Rows[e.RowIndex].Cells[3].Text.ToString();
//OR
               string ObvID=gvAcceptanceStatus.DataKeys[index].Values["ObservationID"].ToString();
            conn.Open();
            SqlCommand cmd = new SqlCommand("sp_DeleteInfo", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("ID",ObvID);
        
            
            try
            {

                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw new Exception("Exception adding account. " + ex.Message);
            }
            finally
            {
                conn.Close();
            }
            BindgvAcceptanceStatus();
            
        }

In aspx page:-
ASP.NET
<asp:gridview id="gvAcceptanceStatus" runat="server" autogeneratecolumns="False" datakeynames="ObservationID" onrowdeleting="gvAcceptanceStatus_RowDeleting">

Convert it to VB.NET.

Hope this helps. :)
 
Share this answer
 
v4

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