Design:
<div>
<table>
<tr>
<td>
CustomerId:
</td>
<td>
<asp:TextBox ID="txtid" runat="server" />
</td>
</tr>
<tr>
<td>
Name:
</td>
<td>
<asp:TextBox ID="txtname" runat="server" />
</td>
</tr>
<tr>
<td>
Country:
</td>
<td>
<asp:TextBox ID="txtcountry" runat="server" />
</td>
</tr>
<tr>
<td align="center">
<asp:Button ID="btnAdd" Text="ADD" runat="server" OnClick="btnAdd_Click" />
</td>
<td align="center">
<asp:Button ID="btnInsert" Text="Insert" runat="server" OnClick="Save" />
</td>
</tr>
</table>
<asp:GridView runat="server" ID="gvResult" AutoGenerateColumns="false" EmptyDataText="No Record To Display !">
<Columns>
<asp:BoundField DataField="CustomerId" HeaderText="Id" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Country" HeaderText="Country" />
</Columns>
</asp:GridView>
</div>
SOURCE CODE:
using System;
using System.Data;
using System.Data.SqlClient;
namespace WebApplication2
{
public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("Data Source=ADMIN-PC\\SQLEXPRESS;Initial Catalog=sanjay;Integrated Security=True");
SqlCommand cmd;
DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
private void BindGrid()
{
cmd = new SqlCommand("SELECT * FROM Customers", con);
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable(); da.Fill(dt);
gvResult.DataSource = dt;
gvResult.DataBind();
con.Close();
}
protected void Save(object sender, EventArgs e)
{
con.Open();
cmd = new SqlCommand("insert into Customers values('" + txtid.Text + "','" + txtname.Text + "','" + txtcountry.Text + "')", con);
cmd.ExecuteNonQuery();
con.Close();
BindGrid();
Response.Write("Details are inserted");
}
protected void btnAdd_Click(object sender, EventArgs e)
{
}
}
}
step:1 onclick evenet add button
protected void btnAdd_Click(object sender, EventArgs e)
{
}
griddisplay Id Name Country
1 muthu india
SQL SERVER
ID NAME COUNTRY
STEP2: ONCLICK EVENET INSERT BUTTON
grid display
id Name country
1 muthu india
sql server display
ID NAME CONTRY
1 MUTHU INDIA