Move and delete checked box checked rows in database table using EntityFramework in ASP.Net

Last Reply 13 days ago By pandeyism

Posted 14 days ago

Thanks to Pandeyism for helping me we these codes

https://www.aspforums.net/Threads/748564/Move-and-delete-multiple-rows-in-database-table-using-EntityFramework-in-ASPNet/?s=2#Replies

But i whant to a a dynamic checkbox to the view and move and delete it based on the checked rows

what am i doing wrong here

 

 

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not IsPostBack Then
        Me.BindGrid()
    End If
	
	 Dim checkBoxColumn As New DataGridViewCheckBoxColumn()
        checkBoxColumn.HeaderText = ""
        checkBoxColumn.Width = 30
        checkBoxColumn.Name = "check"
        DataGridView1.Columns.Insert(0, checkBoxColumn)
        DataGridView1.AllowUserToAddRows = False
End Sub
 
Private Sub BindGrid()
    Dim test As TestEntities = New TestEntities()
    gvMembsers.DataSource = From m In test.members Select m
    gvMembsers.DataBind()
End Sub
 
Protected Sub btnInsert_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim test As TestEntities = New TestEntities()
	
   Dim isSelected As Boolean = Convert.ToBoolean(row.Cells("check").Value)
   
    Dim memberOldRecord = (From o In test.members Where isSelected = True Select o).ToList()
		If isSelected Then
	
			For j As Integer = 0 To memberOldRecord.Count() - 1
				Using test1 As TestEntities = New TestEntities()
					Dim ol As Oldmember = New Oldmember With {
						.name = memberOldRecord(j).name,
						.age = memberOldRecord(j).age,
						.dob = memberOldRecord(j).dob,
						.location = memberOldRecord(j).location
						}
					test1.AddToOldmembers(ol)
				test1.SaveChanges()
				End Using
			Next
		Me.BindGrid()
		End if 
 
    Using test2 As TestEntities = New TestEntities()
        Dim mem = (From c In test2.members Where c.age >= 20 Select c).ToList()
        For i As Integer = 0 To mem.Count() - 1
            Dim member As member = mem(i)
            test2.DeleteObject(member)
            test2.SaveChanges()
        Next
        gvOldmembers.DataSource = mem
        gvOldmembers.DataBind()
        Me.BindGrid()
    End Using
End Sub

will appriciate if the logics is explain please

 

rickabbam has viewed this question 10 days ago.
Posted 14 days ago

I am working on it, I will get back to you soon.


Posted 14 days ago

Hi rickabbam,

Please refer below sample and article

Entity Framework: Call Stored Procedure with Parameters using C# and VB.Net

SQL

CREATE PROCEDURE MemberInsertDelete 20
@ID INT=NULL
AS
BEGIN
    INSERT INTO oldMembers (name,age,location)
    SELECT name,age,location FROM Members WHERE id=@ID
	DELETE FROM Members WHERE id=@ID
END

HTML

<div>
    Member Table Gridview
    <asp:GridView ID="gvMembsers" runat="server" AutoGenerateColumns="false" OnRowDataBound="gvMembsers_RowDataBound">
        <Columns>
            <asp:BoundField DataField="id" HeaderText="id" />
            <asp:BoundField DataField="name" HeaderText="name" />
            <asp:BoundField DataField="age" HeaderText="age" />
            <asp:BoundField DataField="dob" HeaderText="dob" />
            <asp:BoundField DataField="location" HeaderText="location" />
        </Columns>
    </asp:GridView>
    <asp:Button ID="btnInsert" runat="server" Text="Insert" Visible="false" OnClick="btnInsert_Click" />
    <br />
    <br />
    Old Member Tabel GridView
    <asp:GridView ID="gvOldmembers" runat="server" AutoGenerateColumns="false">
        <Columns>
            <asp:BoundField DataField="id" HeaderText="id" />
            <asp:BoundField DataField="name" HeaderText="name" />
            <asp:BoundField DataField="age" HeaderText="age" />
            <asp:BoundField DataField="dob" HeaderText="dob" />
            <asp:BoundField DataField="location" HeaderText="location" />
        </Columns>
    </asp:GridView>
</div>

Namespaces

C#

using TestModel;
using System.Web.UI.WebControls;

Vb.Net

Imports TestModel

Code

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        this.Bind();
    }
}

private void Bind()
{
    TestEntities test = new TestEntities();
    gvMembsers.DataSource = from m in test.members
                            select m;
    gvMembsers.DataBind();
}
    
protected void gvMembsers_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        CheckBox chk = new CheckBox();
        chk.EnableViewState = true;
        chk.Enabled = true;
        chk.ID = "chkb";
        chk.AutoPostBack = true;
        chk.CheckedChanged += new EventHandler(CheckBox_CheckedChanged);
        e.Row.Cells[0].Controls.Add(chk);
        e.Row.TableSection = TableRowSection.TableBody;
    }
}

protected void CheckBox_CheckedChanged(object sender, EventArgs e)
{
    GridViewRow row = ((sender as CheckBox).NamingContainer) as GridViewRow;
    string id = row.Cells[0].Text;
    TestEntities test = new TestEntities();
    test.InsertDelete(Convert.ToInt32(id));
    this.Bind();
}

Vb.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not IsPostBack Then
        Me.Bind()
    End If
End Sub

Private Sub Bind()
    Dim test As TestEntities = New TestEntities()
    gvMembsers.DataSource = From m In test.members Select m
    gvMembsers.DataBind()
End Sub

Protected Sub gvMembsers_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
    If e.Row.RowType = DataControlRowType.DataRow Then
        Dim chk As CheckBox = New CheckBox()
        chk.EnableViewState = True
        chk.Enabled = True
        chk.ID = "chkb"
        chk.AutoPostBack = True
        chk.CheckedChanged += New EventHandler(AddressOf CheckBox_CheckedChanged)
        e.Row.Cells(0).Controls.Add(chk)
        e.Row.TableSection = TableRowSection.TableBody
    End If
End Sub

Protected Sub CheckBox_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
    Dim row As GridViewRow = TryCast(((TryCast(sender, CheckBox)).NamingContainer), GridViewRow)
    Dim id As String = row.Cells(0).Text
    Dim test As TestEntities = New TestEntities()
    test.InsertDelete(Convert.ToInt32(id))
    Me.Bind()
End Sub

Posted 14 days ago

hello Pandeyism

thanks but i think i didnt explain myself well

i dont want it automatically removed when it is checked

with this table

checkbox name       age       location
  rita     19 kl
  fred      19 Gl
  grace   18 fh
  amos      17 gh

assuming row 1 and 5 are thicked, with a button click using entityframework the rows are moved. 


Posted 13 days ago

Hi rickabbam,

Please refer below sample.

HTML

<asp:GridView ID="gvMembsers" runat="server" AutoGenerateColumns="false" OnRowDataBound="gvMembsers_RowDataBound">
    <Columns>
        <asp:BoundField DataField="id" HeaderText="id" />
        <asp:BoundField DataField="name" HeaderText="name" />
        <asp:BoundField DataField="age" HeaderText="age" />
        <asp:BoundField DataField="dob" HeaderText="dob" />
        <asp:BoundField DataField="location" HeaderText="location" />
    </Columns>
</asp:GridView>
<asp:Button ID="btnInsert" runat="server" Text="Insert" OnClick="btnInsert_Click" />

Namespaces

C#

using TestModel;

Vb.Net

Imports TestModel

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        this.Bind();
    }
}

private void Bind()
{
    TestEntities test = new TestEntities();
    gvMembsers.DataSource = from m in test.members
                            select m;
    gvMembsers.DataBind();

}

protected void btnInsert_Click(object sender, EventArgs e)
{

    foreach (GridViewRow row in gvMembsers.Rows)
    {
        CheckBox cb = (CheckBox)row.Cells[0].FindControl("chkbox");
        if (cb.Checked)
        {
            string id = row.Cells[0].Text;
            TestEntities test = new TestEntities();

            test.ImsertDelete(Convert.ToInt32(id));
        }
    }
    this.Bind();
}
protected void gvMembsers_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        CheckBox cb = new CheckBox();
        cb.ID = "chkbox";
        e.Row.Cells[0].Controls.Add(cb);
        Label lbl = new Label();
        lbl.ID = "lblId";
        lbl.Text = e.Row.Cells[0].Text;
        e.Row.Cells[0].Controls.Add(lbl);
    }
}

Vb.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Me.Bind()
    End If
End Sub

Private Sub Bind()
    Dim test As TestEntities = New TestEntities()
    gvMembsers.DataSource = From m In test.members Select m
    gvMembsers.DataBind()
End Sub

Protected Sub btnInsert_Click(ByVal sender As Object, ByVal e As EventArgs)
    For Each row As GridViewRow In gvMembsers.Rows
        Dim cb As CheckBox = CType(row.Cells(0).FindControl("chkbox"), CheckBox)
        If cb.Checked Then
            Dim id As String = row.Cells(0).Text
            Dim test As TestEntities = New TestEntities()
            test.ImsertDelete(Convert.ToInt32(id))
        End If
    Next
    Me.Bind()
End Sub

Protected Sub gvMembsers_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
    If e.Row.RowType = DataControlRowType.DataRow Then
        Dim cb As CheckBox = New CheckBox()
        cb.ID = "chkbox"
        e.Row.Cells(0).Controls.Add(cb)
        Dim lbl As Label = New Label()
        lbl.ID = "lblId"
        lbl.Text = e.Row.Cells(0).Text
        e.Row.Cells(0).Controls.Add(lbl)
    End If
End Sub