using System.Data.SqlClient;
----------database connection ------------------
cn = new SqlConnection();
cn.ConnectionString = "Data Source=sirsuspect-PC;Initial Catalog=StudentDB;Integrated Security=True";
cn.Open();
----------saving record--------------------------
cm = new SqlCommand("insert into tblstudent(studentno, lname, fname, mname, program, section, gender)values(@studentno, @lname, @fname, @mname, @program, @section, @gender)", cn);
cm.Parameters.AddWithValue("@studentno", txtSno.Text);
cm.Parameters.AddWithValue("@lname", txtLname.Text);
cm.Parameters.AddWithValue("@fname", txtFname.Text);
cm.Parameters.AddWithValue("@mname", txtMname.Text);
cm.Parameters.AddWithValue("@program", cboProgram.Text);
cm.Parameters.AddWithValue("@section", txtSection.Text);
if (rdoFemale.Checked == true)
{
cm.Parameters.AddWithValue("@gender", rdoFemale.Text);
}
else if (rdoMale.Checked == true)
{
cm.Parameters.AddWithValue("@gender", rdoMale.Text);
}
else
{
cm.Parameters.AddWithValue("@gender", "");
}
cm.ExecuteNonQuery();
MessageBox.Show("Record successfully saved.", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
-----------updating record-----------------------
cm = new SqlCommand("update tblstudent set lname = @lname, fname=@fname, mname=@mname, program=@program, section=@section, gender=@gender where studentno like @studentno", cn);
cm.Parameters.AddWithValue("@studentno", txtSno.Text);
cm.Parameters.AddWithValue("@lname", txtLname.Text);
cm.Parameters.AddWithValue("@fname", txtFname.Text);
cm.Parameters.AddWithValue("@mname", txtMname.Text);
cm.Parameters.AddWithValue("@program", cboProgram.Text);
cm.Parameters.AddWithValue("@section", txtSection.Text);
if (rdoFemale.Checked == true)
{
cm.Parameters.AddWithValue("@gender", rdoFemale.Text);
}
else if (rdoMale.Checked == true)
{
cm.Parameters.AddWithValue("@gender", rdoMale.Text);
}
else
{
cm.Parameters.AddWithValue("@gender", "");
}
cm.ExecuteNonQuery();
MessageBox.Show("Record successfully updated.", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
-------------deleting records-------------------------
if (MessageBox.Show("Do you want to delete this record?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
cm = new SqlCommand("delete from tblstudent where studentno like '" + _sno + "'", cn);
cm.ExecuteNonQuery();
MessageBox.Show("Record successfully deleted.", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
Clear();
LoadRecord();
}
Programming Language: C#
IDE: Visual Studio 2010
Download link