net.ado.net

 ADO.net 

form1.cs

using System;

using System.Data;

using System.Data.SqlClient;

using System.Windows.Forms;


namespace StudentsServiceDBApp

{

    public partial class Form1 : Form

    {

        // Connection string to the database

        string connectionString = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\USERS\ABISHEK\DOCUMENTS\VISUAL STUDIO 2012\PROJECTS\STUDENTSSERVICEDBAPP\STUDENTSSERVICEDBAPP\STUDENTS.MDF;Integrated Security=True";


        public Form1()

        {

            InitializeComponent();

            AppDomain.CurrentDomain.SetData("DataDirectory", @"C:\USERS\ABISHEK\DOCUMENTS\VISUAL STUDIO 2012\PROJECTS\STUDENTSSERVICEDBAPP\STUDENTSSERVICEDBAPP");

        }


        // Load data into the DataGridView when the form loads

        private void Form1_Load(object sender, EventArgs e)

        {

            LoadData();

        }


        // Insert a new record into the Students table

        private void btnInsert_Click(object sender, EventArgs e)

        {

            using (SqlConnection con = new SqlConnection(connectionString))

            {

                con.Open();

                string query = "INSERT INTO Students (Name, Age, Course) VALUES (@Name, @Age, @Course)";

                SqlCommand cmd = new SqlCommand(query, con);

                cmd.Parameters.AddWithValue("@Name", txtName.Text);

                cmd.Parameters.AddWithValue("@Age", Convert.ToInt32(txtAge.Text));

                cmd.Parameters.AddWithValue("@Course", txtCourse.Text);

                cmd.ExecuteNonQuery();

                MessageBox.Show("Record Inserted Successfully!");

                LoadData(); // Refresh the DataGridView

            }

        }


        // Update an existing record in the Students table

        private void btnUpdate_Click(object sender, EventArgs e)

        {

            using (SqlConnection con = new SqlConnection(connectionString))

            {

                con.Open();

                string query = "UPDATE Students SET Name=@Name, Age=@Age, Course=@Course WHERE StudentID=@ID";

                SqlCommand cmd = new SqlCommand(query, con);

                cmd.Parameters.AddWithValue("@ID", Convert.ToInt32(txtStudentID.Text));

                cmd.Parameters.AddWithValue("@Name", txtName.Text);

                cmd.Parameters.AddWithValue("@Age", Convert.ToInt32(txtAge.Text));

                cmd.Parameters.AddWithValue("@Course", txtCourse.Text);

                cmd.ExecuteNonQuery();

                MessageBox.Show("Record Updated Successfully!");

                LoadData(); // Refresh the DataGridView

            }

        }


        // Delete a record from the Students table

        private void btnDelete_Click(object sender, EventArgs e)

        {

            using (SqlConnection con = new SqlConnection(connectionString))

            {

                con.Open();

                string query = "DELETE FROM Students WHERE StudentID=@ID";

                SqlCommand cmd = new SqlCommand(query, con);

                cmd.Parameters.AddWithValue("@ID", Convert.ToInt32(txtStudentID.Text));

                cmd.ExecuteNonQuery();

                MessageBox.Show("Record Deleted Successfully!");

                LoadData(); // Refresh the DataGridView

            }

        }


        // Refresh the DataGridView to display the latest data

        private void btnRefresh_Click(object sender, EventArgs e)

        {

            LoadData();

        }


        // Load data from the Students table into the DataGridView

        private void LoadData()

        {

            try

            {

                using (SqlConnection con = new SqlConnection(connectionString))

                {

                    con.Open();

                    SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Students", con);

                    DataTable dt = new DataTable();

                    da.Fill(dt);

                    dataGridView1.DataSource = dt;

                }

            }

            catch (Exception ex)

            {

                MessageBox.Show("Error: " + ex.Message);

            }

        }


        // Populate textboxes when a row in the DataGridView is clicked

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)

        {

            if (e.RowIndex >= 0)

            {

                DataGridViewRow row = dataGridView1.Rows[e.RowIndex];

                txtStudentID.Text = row.Cells["StudentID"].Value.ToString();

                txtName.Text = row.Cells["Name"].Value.ToString();

                txtAge.Text = row.Cells["Age"].Value.ToString();

                txtCourse.Text = row.Cells["Course"].Value.ToString();

            }

        }


        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)

        {

            // Optional: Handle cell content click events if needed

        }

    }

}

form1.designer.cs

namespace StudentsServiceDBApp

{

    partial class Form1

    {

        private System.ComponentModel.IContainer components = null;

        private System.Windows.Forms.TextBox txtStudentID;

        private System.Windows.Forms.TextBox txtName;

        private System.Windows.Forms.TextBox txtAge;

        private System.Windows.Forms.TextBox txtCourse;

        private System.Windows.Forms.Button btnInsert;

        private System.Windows.Forms.Button btnUpdate;

        private System.Windows.Forms.Button btnDelete;

        private System.Windows.Forms.Button btnRefresh;

        private System.Windows.Forms.DataGridView dataGridView1;


        protected override void Dispose(bool disposing)

        {

            if (disposing && (components != null))

            {

                components.Dispose();

            }

            base.Dispose(disposing);

        }


        private void InitializeComponent()

        {

            this.components = new System.ComponentModel.Container();

            this.txtStudentID = new System.Windows.Forms.TextBox();

            this.txtName = new System.Windows.Forms.TextBox();

            this.txtAge = new System.Windows.Forms.TextBox();

            this.txtCourse = new System.Windows.Forms.TextBox();

            this.btnInsert = new System.Windows.Forms.Button();

            this.btnUpdate = new System.Windows.Forms.Button();

            this.btnDelete = new System.Windows.Forms.Button();

            this.btnRefresh = new System.Windows.Forms.Button();

            this.dataGridView1 = new System.Windows.Forms.DataGridView();

            this.database1DataSet = new StudentsServiceDBApp.Database1DataSet();

            this.database1DataSetBindingSource3 = new System.Windows.Forms.BindingSource(this.components);

            this.database1DataSetBindingSource = new System.Windows.Forms.BindingSource(this.components);

            this.database1DataSetBindingSource1 = new System.Windows.Forms.BindingSource(this.components);

            this.database1DataSetBindingSource2 = new System.Windows.Forms.BindingSource(this.components);

            this.database1DataSetBindingSource4 = new System.Windows.Forms.BindingSource(this.components);

            this.studentsEntitiesBindingSource1 = new System.Windows.Forms.BindingSource(this.components);

            this.studentsEntitiesBindingSource = new System.Windows.Forms.BindingSource(this.components);

            this.database1DataSetBindingSource5 = new System.Windows.Forms.BindingSource(this.components);

            this.database1DataSetBindingSource6 = new System.Windows.Forms.BindingSource(this.components);

            this.form1BindingSource = new System.Windows.Forms.BindingSource(this.components);

            this.programBindingSource = new System.Windows.Forms.BindingSource(this.components);

            this.label1 = new System.Windows.Forms.Label();

            this.label2 = new System.Windows.Forms.Label();

            this.label3 = new System.Windows.Forms.Label();

            this.label4 = new System.Windows.Forms.Label();

            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();

            ((System.ComponentModel.ISupportInitialize)(this.database1DataSet)).BeginInit();

            ((System.ComponentModel.ISupportInitialize)(this.database1DataSetBindingSource3)).BeginInit();

            ((System.ComponentModel.ISupportInitialize)(this.database1DataSetBindingSource)).BeginInit();

            ((System.ComponentModel.ISupportInitialize)(this.database1DataSetBindingSource1)).BeginInit();

            ((System.ComponentModel.ISupportInitialize)(this.database1DataSetBindingSource2)).BeginInit();

            ((System.ComponentModel.ISupportInitialize)(this.database1DataSetBindingSource4)).BeginInit();

            ((System.ComponentModel.ISupportInitialize)(this.studentsEntitiesBindingSource1)).BeginInit();

            ((System.ComponentModel.ISupportInitialize)(this.studentsEntitiesBindingSource)).BeginInit();

            ((System.ComponentModel.ISupportInitialize)(this.database1DataSetBindingSource5)).BeginInit();

            ((System.ComponentModel.ISupportInitialize)(this.database1DataSetBindingSource6)).BeginInit();

            ((System.ComponentModel.ISupportInitialize)(this.form1BindingSource)).BeginInit();

            ((System.ComponentModel.ISupportInitialize)(this.programBindingSource)).BeginInit();

            this.SuspendLayout();

            // 

            // txtStudentID

            // 

            this.txtStudentID.Location = new System.Drawing.Point(82, 50);

            this.txtStudentID.Name = "txtStudentID";

            this.txtStudentID.Size = new System.Drawing.Size(100, 20);

            this.txtStudentID.TabIndex = 0;

            // 

            // txtName

            // 

            this.txtName.Location = new System.Drawing.Point(82, 82);

            this.txtName.Name = "txtName";

            this.txtName.Size = new System.Drawing.Size(100, 20);

            this.txtName.TabIndex = 1;

            // 

            // txtAge

            // 

            this.txtAge.Location = new System.Drawing.Point(82, 110);

            this.txtAge.Name = "txtAge";

            this.txtAge.Size = new System.Drawing.Size(100, 20);

            this.txtAge.TabIndex = 2;

            // 

            // txtCourse

            // 

            this.txtCourse.Location = new System.Drawing.Point(82, 142);

            this.txtCourse.Name = "txtCourse";

            this.txtCourse.Size = new System.Drawing.Size(100, 20);

            this.txtCourse.TabIndex = 3;

            // 

            // btnInsert

            // 

            this.btnInsert.Location = new System.Drawing.Point(200, 50);

            this.btnInsert.Name = "btnInsert";

            this.btnInsert.Size = new System.Drawing.Size(75, 23);

            this.btnInsert.TabIndex = 4;

            this.btnInsert.Text = "Insert";

            this.btnInsert.UseVisualStyleBackColor = true;

            this.btnInsert.Click += new System.EventHandler(this.btnInsert_Click);

            // 

            // btnUpdate

            // 

            this.btnUpdate.Location = new System.Drawing.Point(200, 80);

            this.btnUpdate.Name = "btnUpdate";

            this.btnUpdate.Size = new System.Drawing.Size(75, 23);

            this.btnUpdate.TabIndex = 5;

            this.btnUpdate.Text = "Update";

            this.btnUpdate.UseVisualStyleBackColor = true;

            this.btnUpdate.Click += new System.EventHandler(this.btnUpdate_Click);

            // 

            // btnDelete

            // 

            this.btnDelete.Location = new System.Drawing.Point(200, 110);

            this.btnDelete.Name = "btnDelete";

            this.btnDelete.Size = new System.Drawing.Size(75, 23);

            this.btnDelete.TabIndex = 6;

            this.btnDelete.Text = "Delete";

            this.btnDelete.UseVisualStyleBackColor = true;

            this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);

            // 

            // btnRefresh

            // 

            this.btnRefresh.Location = new System.Drawing.Point(200, 140);

            this.btnRefresh.Name = "btnRefresh";

            this.btnRefresh.Size = new System.Drawing.Size(75, 23);

            this.btnRefresh.TabIndex = 7;

            this.btnRefresh.Text = "Refresh";

            this.btnRefresh.UseVisualStyleBackColor = true;

            this.btnRefresh.Click += new System.EventHandler(this.btnRefresh_Click);

            // 

            // dataGridView1

            // 

            this.dataGridView1.AllowUserToOrderColumns = true;

            this.dataGridView1.AutoGenerateColumns = false;

            this.dataGridView1.DataSource = this.database1DataSet;

            this.dataGridView1.Location = new System.Drawing.Point(38, 169);

            this.dataGridView1.Name = "dataGridView1";

            this.dataGridView1.Size = new System.Drawing.Size(400, 200);

            this.dataGridView1.TabIndex = 8;

            this.dataGridView1.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellClick);

            this.dataGridView1.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellContentClick);

            // 

            // database1DataSet

            // 

            this.database1DataSet.DataSetName = "Database1DataSet";

            this.database1DataSet.SchemaSerializationMode = System.Data.SchemaSerializationMode.IncludeSchema;

            // 

            // database1DataSetBindingSource3

            // 

            this.database1DataSetBindingSource3.DataSource = this.database1DataSet;

            this.database1DataSetBindingSource3.Position = 0;

            // 

            // database1DataSetBindingSource

            // 

            this.database1DataSetBindingSource.DataSource = this.database1DataSet;

            this.database1DataSetBindingSource.Position = 0;

            // 

            // database1DataSetBindingSource1

            // 

            this.database1DataSetBindingSource1.DataSource = this.database1DataSet;

            this.database1DataSetBindingSource1.Position = 0;

            // 

            // database1DataSetBindingSource2

            // 

            this.database1DataSetBindingSource2.DataSource = this.database1DataSet;

            this.database1DataSetBindingSource2.Position = 0;

            // 

            // database1DataSetBindingSource4

            // 

            this.database1DataSetBindingSource4.DataSource = this.database1DataSet;

            this.database1DataSetBindingSource4.Position = 0;

            // 

            // studentsEntitiesBindingSource1

            // 

            this.studentsEntitiesBindingSource1.DataSource = typeof(StudentsServiceDBApp.StudentsEntities);

            // 

            // studentsEntitiesBindingSource

            // 

            this.studentsEntitiesBindingSource.DataSource = typeof(StudentsServiceDBApp.StudentsEntities);

            // 

            // database1DataSetBindingSource5

            // 

            this.database1DataSetBindingSource5.DataSource = this.database1DataSet;

            this.database1DataSetBindingSource5.Position = 0;

            // 

            // database1DataSetBindingSource6

            // 

            this.database1DataSetBindingSource6.DataSource = typeof(StudentsServiceDBApp.Database1DataSet);

            this.database1DataSetBindingSource6.Position = 0;

            // 

            // form1BindingSource

            // 

            this.form1BindingSource.DataSource = typeof(StudentsServiceDBApp.Form1);

            // 

            // programBindingSource

            // 

            this.programBindingSource.DataSource = typeof(StudentsServiceDBApp.Program);

            // 

            // label1

            // 

            this.label1.AutoSize = true;

            this.label1.Location = new System.Drawing.Point(23, 53);

            this.label1.Name = "label1";

            this.label1.Size = new System.Drawing.Size(53, 13);

            this.label1.TabIndex = 9;

            this.label1.Text = "student id";

            // 

            // label2

            // 

            this.label2.AutoSize = true;

            this.label2.Location = new System.Drawing.Point(35, 85);

            this.label2.Name = "label2";

            this.label2.Size = new System.Drawing.Size(35, 13);

            this.label2.TabIndex = 10;

            this.label2.Text = "Name";

            // 

            // label3

            // 

            this.label3.AutoSize = true;

            this.label3.Location = new System.Drawing.Point(35, 117);

            this.label3.Name = "label3";

            this.label3.Size = new System.Drawing.Size(26, 13);

            this.label3.TabIndex = 11;

            this.label3.Text = "Age";

            this.label3.Click += new System.EventHandler(this.label3_Click);

            // 

            // label4

            // 

            this.label4.AutoSize = true;

            this.label4.Location = new System.Drawing.Point(35, 145);

            this.label4.Name = "label4";

            this.label4.Size = new System.Drawing.Size(40, 13);

            this.label4.TabIndex = 12;

            this.label4.Text = "Course";

            // 

            // Form1

            // 

            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);

            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

            this.ClientSize = new System.Drawing.Size(500, 400);

            this.Controls.Add(this.label4);

            this.Controls.Add(this.label3);

            this.Controls.Add(this.label2);

            this.Controls.Add(this.label1);

            this.Controls.Add(this.dataGridView1);

            this.Controls.Add(this.btnRefresh);

            this.Controls.Add(this.btnDelete);

            this.Controls.Add(this.btnUpdate);

            this.Controls.Add(this.btnInsert);

            this.Controls.Add(this.txtCourse);

            this.Controls.Add(this.txtAge);

            this.Controls.Add(this.txtName);

            this.Controls.Add(this.txtStudentID);

            this.Name = "Form1";

            this.Text = "Student Management System";

            this.Load += new System.EventHandler(this.Form1_Load);

            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();

            ((System.ComponentModel.ISupportInitialize)(this.database1DataSet)).EndInit();

            ((System.ComponentModel.ISupportInitialize)(this.database1DataSetBindingSource3)).EndInit();

            ((System.ComponentModel.ISupportInitialize)(this.database1DataSetBindingSource)).EndInit();

            ((System.ComponentModel.ISupportInitialize)(this.database1DataSetBindingSource1)).EndInit();

            ((System.ComponentModel.ISupportInitialize)(this.database1DataSetBindingSource2)).EndInit();

            ((System.ComponentModel.ISupportInitialize)(this.database1DataSetBindingSource4)).EndInit();

            ((System.ComponentModel.ISupportInitialize)(this.studentsEntitiesBindingSource1)).EndInit();

            ((System.ComponentModel.ISupportInitialize)(this.studentsEntitiesBindingSource)).EndInit();

            ((System.ComponentModel.ISupportInitialize)(this.database1DataSetBindingSource5)).EndInit();

            ((System.ComponentModel.ISupportInitialize)(this.database1DataSetBindingSource6)).EndInit();

            ((System.ComponentModel.ISupportInitialize)(this.form1BindingSource)).EndInit();

            ((System.ComponentModel.ISupportInitialize)(this.programBindingSource)).EndInit();

            this.ResumeLayout(false);

            this.PerformLayout();


        }


        private System.Windows.Forms.BindingSource database1DataSetBindingSource;

        private Database1DataSet database1DataSet;

        private System.Windows.Forms.BindingSource database1DataSetBindingSource3;

        private System.Windows.Forms.BindingSource database1DataSetBindingSource1;

        private System.Windows.Forms.BindingSource database1DataSetBindingSource2;

        private System.Windows.Forms.BindingSource database1DataSetBindingSource4;

        private System.Windows.Forms.BindingSource studentsEntitiesBindingSource1;

        private System.Windows.Forms.BindingSource studentsEntitiesBindingSource;

        private System.Windows.Forms.BindingSource database1DataSetBindingSource5;

        private System.Windows.Forms.BindingSource database1DataSetBindingSource6;

        private System.Windows.Forms.BindingSource form1BindingSource;

        private System.Windows.Forms.BindingSource programBindingSource;

        private System.Windows.Forms.Label label1;

        private System.Windows.Forms.Label label2;

        private System.Windows.Forms.Label label3;

        private System.Windows.Forms.Label label4;

    }

}

app.config

<?xml version="1.0" encoding="utf-8"?>

<configuration>

  <configSections>

    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />

  </configSections>

  <connectionStrings>

    <add name="StudentsServiceDBApp.Properties.Settings.StudentDBConnectionString" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\STUDENTS.MDF;Integrated Security=True" providerName="System.Data.SqlClient" />

  </connectionStrings>

  <startup>

    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />

  </startup>

  <entityFramework>

    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">

      <parameters>

        <parameter value="v11.0" />

      </parameters>

    </defaultConnectionFactory>

  </entityFramework>

</configuration>

SQL

CREATE TABLE [dbo].[Students] (
    [StudentID] INT            IDENTITY (1, 1) NOT NULL,
    [Name]      NVARCHAR (50)  NULL,
    [Age]       INT            NULL,
    [Course]    NVARCHAR (MAX) NULL,
    PRIMARY KEY CLUSTERED ([StudentID] ASC)
);

Comments

Popular posts from this blog

RDBMS

.Net

1-5