Hi Akhter,
Please refer below sample.
Database
I have made use of the following table Customers with the schema as follows.
I have already inserted few records in the table.
You can download the database table SQL by clicking the download link below.
Download SQL file
Procedure
CREATE PROCEDURE [dbo].[Customers_InsertCustomer]
@Name VARCHAR (100),
@Country VARCHAR (100)
AS
BEGIN
IF NOT EXISTS(SELECT CustomerId FROM Customers WHERE Name = @Name AND Country = @Country)
BEGIN
INSERT INTO Customers (Name, Country)
VALUES (@Name, @Country)
END
END
HTML
<table>
<tr>
<td>Name:</td>
<td><asp:TextBox runat="server" ID="txtName" /></td>
</tr>
<tr>
<td>Country:</td>
<td><asp:TextBox runat="server" ID="txtCountry" /></td>
</tr>
<tr>
<td><asp:Button ID="btnAdd" Text="Add" runat="server" OnClick="Add" /></td>
</tr>
</table>
<br />
<asp:GridView runat="server" ID="gvCustomers" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Country" HeaderText="Country" />
</Columns>
</asp:GridView>
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
}
protected void Add(object sender, EventArgs e)
{
int recordsInserted = 0;
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("Customers_InsertCustomer"))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = con;
cmd.Parameters.AddWithValue("@Name", txtName.Text);
cmd.Parameters.AddWithValue("@Country", txtCountry.Text);
con.Open();
recordsInserted = cmd.ExecuteNonQuery();
con.Close();
}
}
this.BindGrid();
if (recordsInserted == -1)
{
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Data already exists.');", true);
}
}
private void BindGrid()
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("SELECT Name, Country FROM Customers", con))
{
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
gvCustomers.DataSource = dt;
gvCustomers.DataBind();
}
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindGrid()
End If
End Sub
Protected Sub Add(ByVal sender As Object, ByVal e As EventArgs)
Dim recordsInserted As Integer = 0
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand("Customers_InsertCustomer")
cmd.CommandType = CommandType.StoredProcedure
cmd.Connection = con
cmd.Parameters.AddWithValue("@Name", txtName.Text)
cmd.Parameters.AddWithValue("@Country", txtCountry.Text)
con.Open()
recordsInserted = cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
Me.BindGrid()
If recordsInserted = -1 Then
ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('Data already exists.');", True)
End If
End Sub
Private Sub BindGrid()
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand("SELECT Name, Country FROM Customers", con)
Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
Using dt As DataTable = New DataTable()
sda.Fill(dt)
gvCustomers.DataSource = dt
gvCustomers.DataBind()
End Using
End Using
End Using
End Using
End Sub
Screenshot