using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;
public partial class stinfo : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;
AttachDbFilename=D:\WebSite\WebSite1\App_Data\personaldetails.mdf;
Integrated Security=True;User Instance=True");
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
form_load();
}
}
private void form_load()
{
con.Open();
SqlCommand cmd = new SqlCommand("select count(*) from student ", con);
int res = Convert.ToInt32(cmd.ExecuteScalar());
Label1.Text = "total no of students :" + res.ToString();
con.Close();
}
protected void btnInsert_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand("insert into student (sno,firstname,
lastname,phone) values( @sno, @firstname,@lastname,@phone)", con);
cmd.Parameters.Add("@sno", SqlDbType.Int).Value = txtSno.Text;
cmd.Parameters.Add("@firstname", SqlDbType.VarChar).Value = txtFirstname.Text;
cmd.Parameters.Add("@lastname", SqlDbType.VarChar).Value = txtLastname.Text;
cmd.Parameters.Add("@phone", SqlDbType.Int).Value = txtphoneno.Text;
int res= cmd.ExecuteNonQuery();
con.Close();
if (res == 1)
{
string str = "select IsNull(Max(sno),1000)+1 from student";
cmd.CommandText = str;
con.Open();
txtSno.Text = cmd.ExecuteScalar().ToString();
con.Close();
txtSno.Focus();
MessageBox.Show("record successfully inserted");
txtSno.Text = "";
txtFirstname.Text = "";
txtLastname.Text = "";
txtphoneno.Text = "";
form_load();
}
else
{
MessageBox.Show("record not inserted");
}
}
protected void btnDelete_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd=new SqlCommand("delete from student where sno="+txtSno.Text,con);
int res=Convert.ToInt32( cmd.ExecuteNonQuery());
con.Close();
if (res == 1)
{
txtSno.Text = "";
MessageBox.Show("record successfully deleted");
form_load();
}
else
{
MessageBox.Show("record not deleted");
}
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand("update student set
firstname='" + txtFirstname.Text +"',lastname='"
+ txtLastname.Text + "',phone='" + txtphoneno.Text + "'
where sno =" +txtSno.Text, con);
int res=Convert.ToInt32( cmd.ExecuteNonQuery());
con.Close();
if (res == 1)
{
txtSno.Text = "";
txtFirstname.Text = "";
txtLastname.Text = "";
txtphoneno.Text = "";
MessageBox.Show("record successfuly updated");
form_load();
}
else
{
MessageBox.Show("record not updated");
}
}
}
No comments:
Post a Comment