using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
namespace StudentInternship
{
public partial class WebForm1 : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\acer-pc\Desktop\StudentInternship\App_Data\Database1.mdf;Integrated Security=True;Connect Timeout=30");
protected void Page_Load(object sender, EventArgs e)
{
if(con.State==ConnectionState.Open)
{
con.Close();
}
con.Open;
disp_data();
}
protected void btnInsert_Click(object sender, EventArgs e)
{
SqlCommand sqlCommand = con.CreateCommand();
sqlCommand.CommandType = CommandType.Text;
sqlCommand.CommandText="insert into Students values('"+txtBoxfName.Text+"','"+txtBoxlName.Text+"','"+txtBoxStudentNo2+"')";
sqlCommand.ExecuteNonQuery();
txtBoxfName.Text = " ";
txtBoxlName.Text = " ";
txtBoxStudentNo2.Text = " ";
disp_data();
}
public void disp_data()
{
SqlCommand sqlCommand = con.CreateCommand();
sqlCommand.CommandType = CommandType.Text;
sqlCommand.CommandText = "select * from Students ";
sqlCommand.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(sqlCommand);
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}