Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

jQuery Delete Row from Gridview and Database in asp.net using C#, VB.NET

Dec 1, 2014
Introduction

Here I will explain how to delete row from
gridview and database using jQuery in asp.net in c# , vb.net with confirmation message or remove gridview rows from database with confirmation box using jQuery in asp.net with c# , vb.net.

Description:
  
In previous articles I explained Delete rows from gridview with checkbox selection in asp.net, Get selected row cell value from gridview in asp.net, jQuery remove previous row details in table,
export gridview data to pdf in asp.net using c# and many articles relating to asp.net, c#,vb.net and jQuery. Now I will explain how to delete row from gridview and database using jQuery in asp.net in c# , vb.net with confirmation message.

Before implement this example first design one table UserDetails in your database as shown below

Column Name
Data Type
Allow Nulls
UserId
Int(IDENTITY=TRUE)
Yes
UserName
varchar(50)
Yes
FirstName
varchar(50)
Yes
LastName
varchar(50)
Yes
Once table created in database enter some dummy data to test application after that write the following code in your aspx page 


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>jQuery delete gridview row from database in asp.net</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script type="text/javascript">
function Confirmationbox(obj) {
var result = confirm('Are you sure you want to delete selected User(s)?');
if (result) {
var row = $(obj).closest("tr");
var userid = parseInt(row.find("[id*=hdnUserId]").val());
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "DeleteGridviewRows.aspx/DeleteGridviewData",
data: "{'userid':'" + userid + "'}",
dataType: "json",
success: function(data) {
if (data.d == 'true') {
location.reload();
}
}
});
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div align="center" style="margin-top:200px">
<asp:GridView ID="gvDetails" AutoGenerateColumns="false" CellPadding="5" runat="server">
<Columns>
<asp:BoundField HeaderText="UserId" DataField="UserId" />
<asp:BoundField HeaderText="UserName" DataField="UserName" />
<asp:BoundField HeaderText="FirstName" DataField="FirstName" />
<asp:BoundField HeaderText="LastName" DataField="LastName" />
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField ID="hdnUserId" runat="server" Value='<%#Eval("UserId") %>' />
<asp:LinkButton ID="btnDelete" Text="Delete Record" runat="server" OnClientClick="Confirmationbox(this);" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
</asp:GridView>
</div>
</form>
</body>
</html>
Now open code behind file and write the following code

C# Code


using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.Services;

public partial class DeleteGridviewRows : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindUserDetails();
}
}
protected void BindUserDetails()
{
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection("Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB"))
{
con.Open();
SqlCommand cmd = new SqlCommand("Select * from UserDetails", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
con.Close();
}
gvDetails.DataSource = dt;
gvDetails.DataBind();
}
[WebMethod]
public static string DeleteGridviewData(int userid)
{
string msg = string.Empty;
using (SqlConnection con = new SqlConnection("Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB"))
{
using (SqlCommand cmd = new SqlCommand("delete from UserDetails where UserId= @userid", con))
{
con.Open();
cmd.Parameters.AddWithValue("@userid", userid);
int i = cmd.ExecuteNonQuery();
con.Close();
if (i == 1)
{
msg = "true";
}
else
{
msg = "false";
}
}
}
return msg;
}
}
VB.NET


Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Services
Partial Class VBCode
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
BindUserDetails()
End If
End Sub
Protected Sub BindUserDetails()
Dim dt As New DataTable()
Using con As New SqlConnection("Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB")
con.Open()
Dim cmd As New SqlCommand("Select * from UserDetails", con)
Dim da As New SqlDataAdapter(cmd)
da.Fill(dt)
con.Close()
End Using
gvDetails.DataSource = dt
gvDetails.DataBind()
End Sub
<WebMethod()> _
Public Shared Function DeleteGridviewData(ByVal userid As Integer) As String
Dim msg As String = String.Empty
Using con As New SqlConnection("Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB")
Using cmd As New SqlCommand("delete from UserDetails where UserId= @userid", con)
con.Open()
cmd.Parameters.AddWithValue("@userid", userid)
Dim i As Integer = cmd.ExecuteNonQuery()
con.Close()
If i = 1 Then
msg = "true"
Else
msg = "false"
End If
End Using
End Using
Return msg
End Function
End Class
Demo

delete row from gridview and database using jquery in asp.net

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

1 comments :

Bhupendra Singh Parihar said...

one question always in my mind,:
suppose i use jquery in html and use functions, but if someone inspect element then they will be able to see all of our functions and code, and we also use webmethods by jquery, so anyone can see our code in html by view-source on browser,
Is there any way to secure our functions and methods? so that our code will be secure.

please reply.

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.