I would like to explain about inserting values into textbox and that will be displayed in grid view
so for that
In visual studio click on file -->New project-->Add weapplication-> add new item-->
add new web form
In .aspx page:
<table>
<tr><td>Name:</td><td>
<asp:TextBox ID="txt_name" runat="server"></asp:TextBox></td></tr>
<tr><td>Description:</td><td>
<asp:TextBox ID="txt_description" runat="server"></asp:TextBox></td></tr>
<tr><td>Department:</td><td>
<asp:TextBox ID="txt_depart" runat="server"></asp:TextBox></td></tr>
<tr><td colspan="2" align="center">
<asp:Button ID="Button1" runat="server" Text="Submit" onclick="Button1_Click" /></td></tr>
</table>
<asp:label: id="1abel1" runat="server" >
The page will be display like this
code in .aspx.cs page:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class insert : System.Web.UI.Page
{
string strcon = ConfigurationManager.ConnectionStrings["sqlconn"].ConnectionString;
// you have to add configuration in name space .
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
}
code in button click event
protected void Button1_Click(object sender, EventArgs e)
{
try
{
SqlConnection con = new SqlConnection(strcon);
SqlCommand cmd = new SqlCommand("insert_sp_emp", con);
//here i passed stored procedure instead ofinsert query.
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Name", txt_name.Text);
cmd.Parameters.AddWithValue("@Description", txt_description.Text);
cmd.Parameters.AddWithValue("@Department", txt_depart.Text);
con.Open();
cmd.ExecuteNonQuery();
label1.text="Record Inserted Succesfully;
con.Close();
}
catch(Exception,ex)
{
label1.Text=ex.message;
}
}
}
}
in sql server
crete table
and write procedure for insert query i.e,
create procedure insert_sp_emp(@Name nvarchar(50),@Description nvarchar(50),@Department nvarchr(50))
AS
BEGIN
insert into tablename(Name,Description,Department) values(@Name,@Description,@Department)
END
if you set an identity true no need to mention id as parameter-(@Id int)
Web Config file:
<configuration>
<connectionStrings>
<add name="sqlconn" connectionString="Data Source=SYS-2;Initial Catalog=Emp;Integrated Security=True"/>
</connectionStrings>
So build your application and start debugging then it shown your output like this
then you have to display the complete record into gridview using asp.net:
in .aspx page:
drag and drop gridview from datbound controls
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Description" HeaderText="Description" />
<asp:BoundField DataField="Department" HeaderText="Department" />
</Columns>
</asp:GridView>
code in .aspx.cs page:
public void binddata()
{
SqlConnection con = new SqlConnection(strcon);
SqlCommand cmd = new SqlCommand("select* from Emp", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
In button click event you have to mention
bindata(); before con.close();//connection close
then build the and debug the code once
then it will be displayed liked this
so it is useful for beginners who are learning asp.net newly
I hope you understand raise a query you have any doubt about this