Hi nauna,
Please refer below sample.
HTML
<asp:Button Text="Save" ID="btnSave" OnClick="Save" runat="server" />
Namespaces
C#
using System.Data;
VB.Net
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.GetDataTable();
}
}
protected void Save(object sender, EventArgs e)
{
int id = 2;
string name = "Mudassar Khan";
string country = "India";
DataTable dt = GetDataTable();
var duplicate = from d in dt.AsEnumerable()
where d.Field<int>("Id") == id
&& d.Field<string>("Name") == name
&& d.Field<string>("Country") == country
select d;
if (duplicate.Count() > 0)
{
ClientScript.RegisterStartupScript(this.GetType(), "message", "alert('Record already exist')", true);
}
else
{
dt.Rows.Add(id, name, country);
}
}
private DataTable GetDataTable()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] {
new DataColumn("Id", typeof(int)),
new DataColumn("Name", typeof(string)),
new DataColumn("Country",typeof(string))});
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(4, "Robert Schidner", "Russia");
return dt;
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.GetDataTable()
End If
End Sub
Protected Sub Save(ByVal sender As Object, ByVal e As EventArgs)
Dim id As Integer = 2
Dim name As String = "Mudassar Khan"
Dim country As String = "India"
Dim dt As DataTable = GetDataTable()
Dim duplicate = From d In dt.AsEnumerable() Where d.Field(Of Integer)("Id") = id AndAlso d.Field(Of String)("Name") = name AndAlso d.Field(Of String)("Country") = country Select d
If duplicate.Count() > 0 Then
ClientScript.RegisterStartupScript(Me.GetType(), "message", "alert('Record already exist')", True)
Else
dt.Rows.Add(id, name, country)
End If
End Sub
Private Function GetDataTable() As DataTable
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id", GetType(Integer)), New DataColumn("Name", GetType(String)), New DataColumn("Country", GetType(String))})
dt.Rows.Add(1, "John Hammond", "United States")
dt.Rows.Add(2, "Mudassar Khan", "India")
dt.Rows.Add(3, "Suzanne Mathews", "France")
dt.Rows.Add(4, "Robert Schidner", "Russia")
Return dt
End Function