Can I delete a record from gridview based on another column name besides Id column which is primary key?

Donald Symmons 3,066 Reputation points
2022-11-29T10:54:55.663+00:00

I am displaying my records in gridview based on a column named ("Name"), but my primary key is the column ("Id"). May I please ask if it is possible to delete a gridview record based on the column "Name"? If yes, may I please know how to do this?
And will it have effect o the primary key (which in my case, is the column ("Id").

I don't know how to do this but here is how my Stored Procedure looks like and my gridview display code. Any assistance please?

Srored Procedure

CREATE PROCEDURE [dbo].[UserData]  
 @Name NVARCHAR(MAX)  
 ,@PageIndex INT  
 ,@PageSize INT  
 ,@RecordCount INT OUT  
AS  
BEGIN  
    SELECT ROW_NUMBER() OVER(ORDER BY Name) RowNumber  
 ,Id  
         ,UserName    
             ,email  
 ,RegisteredDate  
  
    INTO #Temp   
    FROM UserTable  
 WHERE Name = @Name  
  
 SELECT @RecordCount = COUNT(*) FROM #Temp   
  
 SELECT * FROM #Temp   
    WHERE (RowNumber BETWEEN ((@PageIndex-1) * @PageSize) + 1 AND (@PageIndex * @PageSize)) OR @PageIndex = - 1   
     
    DROP TABLE #Temp   
  
 END  
RETURN 0  

**Gridview **

    private int PageSize = 5;  
    protected void Page_Load(object sender, EventArgs e)  
    {  
        if (!this.IsPostBack)  
        {  
            this.GetCustomersPageWise(1);  
        }  
    }  
  
    private void GetCustomersPageWise(int pageIndex)  
    {  
        using (SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\Dataregister.mdf;Integrated Security = True"))  
        {  
            using (SqlCommand cmd = new SqlCommand("UserData ", con))  
            {  
                cmd.CommandType = CommandType.StoredProcedure;  
                cmd.Parameters.AddWithValue("@Name", Textboxname.Text.Trim());  
                cmd.Parameters.AddWithValue("@PageIndex", pageIndex);  
                cmd.Parameters.AddWithValue("@PageSize", PageSize);  
                cmd.Parameters.Add("@RecordCount", SqlDbType.Int, 4);  
                cmd.Parameters["@RecordCount"].Direction = ParameterDirection.Output;  
                con.Open();  
                using (SqlDataAdapter sda = new SqlDataAdapter())  
                {  
                    cmd.Connection = con;  
                    sda.SelectCommand = cmd;  
                    using (DataTable dt = new DataTable())  
                    {  
                        sda.Fill(dt);  
                        GridView1.DataSource = dt;  
                        GridView1.DataBind();  
                        con.Close();  
                        int recordCount = Convert.ToInt32(cmd.Parameters["@RecordCount"].Value);  
                        this.PopulatePager(recordCount, pageIndex);  
                    }  
                }  
            }  
        }  
    }  
  
Developer technologies ASP.NET Other
Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. QiYou-MSFT 4,326 Reputation points Microsoft External Staff
    2022-11-30T08:21:12.227+00:00

    Hi @Donald Symmons ,
    When we build a database, we need to specify a primary key for each table, the so-called primary key is a property or attribute group that can uniquely identify a row in the table, a table can only have one primary key, but can have multiple candidate indexes. Because the primary key can uniquely identify a row of records, it can ensure that there will be no errors when performing data updates and deletions. Of course, other fields can help us eliminate sharing conflicts when performing these operations, but they are not discussed here. In addition to the above functions, primary keys often form referential integrity constraints with foreign keys to prevent data inconsistencies. Therefore, when designing a database, the primary key plays an important role.
    We can delete records through other columns, using code in the database:

    DELETE from Table where xxx='xxx'  
    

    265643-pic1.png
    265580-pic2.png
    From the figure above, you can see that records can be deleted by other columns. However, because the primary key is unique and the others are not, one situation may occur where the positioning is inaccurate.
    As for the data represented by the GridView, it is based on the data of the database, just a read, as long as the data of the database is obtained.
    Best Regrads
    Qi You


1 additional answer

Sort by: Most helpful
  1. Jonathon Kindred 411 Reputation points
    2022-11-29T11:08:17.043+00:00

    The data in GridView is based on the data from your data source. So, if you want to delete a row from it then you'll have to delete the data from the data source.

    If you are using a database for populating your GridView then you need to delete the data from the database and rebind your GridView with the updated data source to reflect the changes.

    Deleting from the source will be as simple as:

    DELETE FROM your_table_name WHERE name = "your_value";  
      
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.