Hi IamAzhar,
Refer below sample.
HTML
Name :
<asp:TextBox runat="server" ID="txtName" />
<br />
Country :
<asp:TextBox runat="server" ID="txtCountry" />
<br />
<asp:Button Text="Insert" runat="server" OnClick="Insert" />
<br />
<asp:GridView runat="server" ID="gvCustomers" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="CustomerId" HeaderText="Id" />
<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.SqlClient
Imports System.Data
Imports System.Configuration
Code
C#
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
if (Session["Message"] != null)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('" + Session["Message"] + "');", true);
Session["Message"] = null;
}
BindGrid();
}
}
protected void Insert(object sender, EventArgs e)
{
Session["Message"] = "Insert Successfully.";
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO Customers(Name,Country) VALUES(@Name,@Country)", con))
{
cmd.Parameters.AddWithValue("@Name", txtName.Text);
cmd.Parameters.AddWithValue("@Country", txtCountry.Text);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
BindGrid();
Page.Response.Redirect(Page.Request.RawUrl, false);
}
public void BindGrid()
{
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT CustomerId,Name,Country FROM Customers", con))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
da.Fill(dt);
gvCustomers.DataSource = dt;
gvCustomers.DataBind();
}
}
}
}
VB.Net
Private constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
If Session("Message") IsNot Nothing Then
ScriptManager.RegisterStartupScript(Me, Me.[GetType](), "Message", "alert('" & Session("Message") & "');", True)
Session("Message") = Nothing
End If
BindGrid()
End If
End Sub
Protected Sub Insert(ByVal sender As Object, ByVal e As EventArgs)
Session("Message") = "Insert Successfully."
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("INSERT INTO Customers(Name,Country) VALUES(@Name,@Country)", con)
cmd.Parameters.AddWithValue("@Name", txtName.Text)
cmd.Parameters.AddWithValue("@Country", txtCountry.Text)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
BindGrid()
Page.Response.Redirect(Page.Request.RawUrl, False)
End Sub
Public Sub BindGrid()
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("SELECT CustomerId,Name,Country FROM Customers", con)
Using da As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
da.Fill(dt)
gvCustomers.DataSource = dt
gvCustomers.DataBind()
End Using
End Using
End Using
End Sub
Screenshot