Hi akhter,
I have created a sample please take its reference.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
HTML
Name : <asp:TextBox ID="txtName" runat="server" /><br />
City : <asp:TextBox ID="txtCity" runat="server" /><br />
Country : <asp:TextBox ID="txtCountry" runat="server" /><br />
<asp:DropDownList runat="server" ID="ddlNumber">
<asp:ListItem Value="--Select--" Text="--Select--" />
<asp:ListItem Value="1" Text="1" />
<asp:ListItem Value="4" Text="4" />
<asp:ListItem Value="9" Text="9" />
</asp:DropDownList><br /><br />
<asp:Button Text="Insert" runat="server" ID="btnInsert" OnClick="OnInsert" /><br /><br />
<asp:GridView runat="server" ID="gvCustomers" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="CustomerID" HeaderText="ID" />
<asp:BoundField DataField="ContactName" HeaderText="Name" />
<asp:BoundField DataField="City" HeaderText="City" />
<asp:BoundField DataField="Country" HeaderText="Country" />
</Columns>
</asp:GridView>
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindCustomers();
}
}
private void BindCustomers()
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("SELECT CustomerID,ContactName,City,Country FROM Customers", con))
{
cmd.CommandType = CommandType.Text;
using (SqlDataAdapter sda = new SqlDataAdapter())
{
sda.SelectCommand = cmd;
DataTable dt = new DataTable();
sda.Fill(dt);
this.gvCustomers.DataSource = dt;
this.gvCustomers.DataBind();
}
}
}
}
protected void OnInsert(object sender, EventArgs e)
{
int number = Convert.ToInt32(ddlNumber.SelectedValue);
gvCustomers.AllowPaging = false;
BindCustomers();
int rowCount = gvCustomers.Rows.Count;
gvCustomers.AllowPaging = true;
BindCustomers();
if (number < rowCount)
{
string name = txtName.Text;
string city = txtCity.Text;
string country = txtCountry.Text;
this.Insert(name, city, country);
}
}
private void Insert(string name, string city, string country)
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO tblCustomers VALUES(Name,City,Country)", con))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@Name", name);
cmd.Parameters.AddWithValue("@City", city);
cmd.Parameters.AddWithValue("@City", country);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindCustomers()
End If
End Sub
Private Sub BindCustomers()
Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
Using cmd As SqlCommand = New SqlCommand("SELECT CustomerID,ContactName,City,Country FROM Customers", con)
cmd.CommandType = CommandType.Text
Using sda As SqlDataAdapter = New SqlDataAdapter()
sda.SelectCommand = cmd
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
Me.gvCustomers.DataSource = dt
Me.gvCustomers.DataBind()
End Using
End Using
End Using
End Sub
Protected Sub OnInsert(ByVal sender As Object, ByVal e As EventArgs)
Dim number As Integer = Convert.ToInt32(ddlNumber.SelectedValue)
gvCustomers.AllowPaging = False
BindCustomers()
Dim rowCount As Integer = gvCustomers.Rows.Count
gvCustomers.AllowPaging = True
BindCustomers()
If number < rowCount Then
Dim name As String = txtName.Text
Dim city As String = txtCity.Text
Dim country As String = txtCountry.Text
Me.Insert(name, city, country)
End If
End Sub
Private Sub Insert(ByVal name As String, ByVal city As String, ByVal country As String)
Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
Using cmd As SqlCommand = New SqlCommand("INSERT INTO tblCustomers VALUES(Name,City,Country)", con)
cmd.CommandType = CommandType.Text
cmd.Parameters.AddWithValue("@Name", name)
cmd.Parameters.AddWithValue("@City", city)
cmd.Parameters.AddWithValue("@City", country)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
End Sub