Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- App.Config
- <?xml version="1.0" encoding="utf-8" ?>
- <configuration>
- <startup>
- <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
- </startup>
- <connectionStrings>
- <add name="ConnectionString" connectionString="Data Source=instance_name;Initial Catalog=webform;Integrated Security=True" providerName="System.Data.SqlClient"/>
- </connectionStrings>
- </configuration>
- Form1.cs
- using System;
- using System.Configuration;
- using System.Data;
- using System.Data.SqlClient;
- using System.Windows.Forms;
- namespace WindowsFormsApp1
- {
- public partial class Form1 : Form
- {
- // Connection string
- //string connectionString = "Data Source=DESKTOP-6082S7Q;Initial Catalog=webform;Integrated Security=True";
- string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
- public Form1()
- {
- InitializeComponent();
- // Set background image
- this.BackgroundImage = Properties.Resources.wallpaper; // Assuming the image is added to your project's resources
- // Optionally, set background image layout
- this.BackgroundImageLayout = ImageLayout.Stretch; // Adjust to your preferred layout mode
- }
- private void btnDelete_Click(object sender, EventArgs e)
- {
- if (dataGridView1.SelectedRows.Count > 0)
- {
- int selectedRowIndex = dataGridView1.SelectedRows[0].Index;
- int studentId = Convert.ToInt32(dataGridView1.Rows[selectedRowIndex].Cells["StudentID"].Value);
- using (SqlConnection connection = new SqlConnection(connectionString))
- {
- string query = "DELETE FROM Student_Registration WHERE StudentID = @StudentID";
- SqlCommand command = new SqlCommand(query, connection);
- command.Parameters.AddWithValue("@StudentID", studentId);
- try
- {
- connection.Open();
- int rowsAffected = command.ExecuteNonQuery();
- if (rowsAffected > 0)
- {
- MessageBox.Show("Data deleted successfully.");
- // Refresh the DataGridView after deletion
- btnShow_Click(sender, e);
- }
- else
- {
- MessageBox.Show("No rows were affected. Data might not have been deleted.");
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show("Error: " + ex.Message);
- }
- }
- }
- else
- {
- MessageBox.Show("Please select a row to delete.");
- }
- }
- // Button click event for Insert
- private void btnInsert_Click(object sender, EventArgs e)
- {
- using (SqlConnection connection = new SqlConnection(connectionString))
- {
- string query = "INSERT INTO Student_Registration VALUES (@StudentID, @StudentName, @Address, @Class)";
- SqlCommand command = new SqlCommand(query, connection);
- command.Parameters.AddWithValue("@StudentID", Convert.ToInt32(txtStudentId.Text));
- command.Parameters.AddWithValue("@StudentName", txtStudentName.Text);
- command.Parameters.AddWithValue("@Address", txtAddress.Text);
- command.Parameters.AddWithValue("@Class", txtClass.Text);
- try
- {
- connection.Open();
- int rowsAffected = command.ExecuteNonQuery();
- if (rowsAffected > 0)
- {
- MessageBox.Show("Data inserted successfully.");
- // Clear textboxes after successful insertion
- txtStudentId.Text = "";
- txtStudentName.Text = "";
- txtAddress.Text = "";
- txtClass.Text = "";
- }
- else
- {
- MessageBox.Show("No rows were affected. Data might not have been inserted.");
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show("Error: " + ex.Message);
- }
- }
- }
- // Button click event for Search
- private void btnFind_Click(object sender, EventArgs e)
- {
- using (SqlConnection connection = new SqlConnection(connectionString))
- {
- string query = "SELECT * FROM Student_Registration WHERE StudentID = @StudentID";
- SqlCommand command = new SqlCommand(query, connection);
- command.Parameters.AddWithValue("@StudentID", Convert.ToInt32(txtSearch.Text));
- try
- {
- connection.Open();
- SqlDataReader reader = command.ExecuteReader();
- // Create a DataTable to hold the fetched data
- DataTable dt = new DataTable();
- dt.Load(reader);
- // Assign DataTable as the DataSource for DataGridView
- dataGridView1.DataSource = dt;
- // If no rows are returned, display a message
- if (dataGridView1.Rows.Count == 0)
- {
- MessageBox.Show("No matching student found.");
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show("Error: " + ex.Message);
- }
- }
- }
- private void btnExit_Click(object sender, EventArgs e)
- {
- Application.Exit();
- }
- private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
- {
- if (e.RowIndex >= 0)
- {
- DataGridViewRow selectedRow = dataGridView1.Rows[e.RowIndex];
- // Populate text fields with data from the selected row
- txtStudentId.Text = selectedRow.Cells["ID"].Value.ToString();
- txtStudentName.Text = selectedRow.Cells["StudentName"].Value.ToString();
- txtAddress.Text = selectedRow.Cells["Address"].Value.ToString();
- txtClass.Text = selectedRow.Cells["Class"].Value.ToString();
- }
- }
- private void btnShow_Click(object sender, EventArgs e)
- {
- using (SqlConnection connection = new SqlConnection(connectionString))
- {
- string query = "SELECT * FROM Student_Registration";
- SqlCommand command = new SqlCommand(query, connection);
- try
- {
- connection.Open();
- SqlDataReader reader = command.ExecuteReader();
- // Create a DataTable to hold the fetched data
- DataTable dt = new DataTable();
- dt.Load(reader);
- // Assign DataTable as the DataSource for DataGridView
- dataGridView1.DataSource = dt;
- // If no rows are returned, display a message
- if (dataGridView1.Rows.Count == 0)
- {
- MessageBox.Show("No data available.");
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show("Error: " + ex.Message);
- }
- }
- }
- private void btnUpdate_Click(object sender, EventArgs e)
- {
- // Check if all required fields are filled
- if (string.IsNullOrEmpty(txtStudentId.Text) || string.IsNullOrEmpty(txtStudentName.Text) ||
- string.IsNullOrEmpty(txtAddress.Text) || string.IsNullOrEmpty(txtClass.Text))
- {
- MessageBox.Show("Please fill in all fields.");
- return;
- }
- // Check if the student ID exists in the database
- int studentId;
- if (!int.TryParse(txtStudentId.Text, out studentId))
- {
- MessageBox.Show("Invalid student ID.");
- return;
- }
- // Check if the student ID exists in the database
- if (!StudentExists(studentId))
- {
- MessageBox.Show("Student with ID " + studentId + " does not exist.");
- return;
- }
- // Perform the update operation
- using (SqlConnection connection = new SqlConnection(connectionString))
- {
- string query = "UPDATE Student_Registration SET StudentName = @StudentName, Address = @Address, Class = @Class WHERE StudentID = @StudentID";
- SqlCommand command = new SqlCommand(query, connection);
- command.Parameters.AddWithValue("@StudentID", studentId);
- command.Parameters.AddWithValue("@StudentName", txtStudentName.Text);
- command.Parameters.AddWithValue("@Address", txtAddress.Text);
- command.Parameters.AddWithValue("@Class", txtClass.Text);
- try
- {
- connection.Open();
- int rowsAffected = command.ExecuteNonQuery();
- if (rowsAffected > 0)
- {
- MessageBox.Show("Data updated successfully.");
- }
- else
- {
- MessageBox.Show("No rows were affected. Data might not have been updated.");
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show("Error: " + ex.Message);
- }
- }
- }
- // Helper method to check if a student with the given ID exists in the database
- private bool StudentExists(int studentId)
- {
- using (SqlConnection connection = new SqlConnection(connectionString))
- {
- string query = "SELECT COUNT(*) FROM Student_Registration WHERE StudentID = @StudentID";
- SqlCommand command = new SqlCommand(query, connection);
- command.Parameters.AddWithValue("@StudentID", studentId);
- try
- {
- connection.Open();
- int count = (int)command.ExecuteScalar();
- return count > 0;
- }
- catch (Exception)
- {
- return false;
- }
- }
- }
- }
- }
- Form1.Designer.cs
- using System;
- using System.Data.SqlClient;
- using System.Windows.Forms;
- namespace WindowsFormsApp1
- {
- partial class Form1
- {
- /// <summary>
- /// Required designer variable.
- /// </summary>
- private System.ComponentModel.IContainer components = null;
- /// <summary>
- /// Clean up any resources being used.
- /// </summary>
- /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
- protected override void Dispose(bool disposing)
- {
- if (disposing && (components != null))
- {
- components.Dispose();
- }
- base.Dispose(disposing);
- }
- #region Windows Form Designer generated code
- /// <summary>
- /// Required method for Designer support - do not modify
- /// the contents of this method with the code editor.
- /// </summary>
- private void InitializeComponent()
- {
- this.lblStudentId = new System.Windows.Forms.Label();
- this.txtStudentId = new System.Windows.Forms.TextBox();
- this.lblStudentName = new System.Windows.Forms.Label();
- this.txtStudentName = new System.Windows.Forms.TextBox();
- this.lblAddress = new System.Windows.Forms.Label();
- this.txtAddress = new System.Windows.Forms.TextBox();
- this.lblClass = new System.Windows.Forms.Label();
- this.txtClass = 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.btnShow = new System.Windows.Forms.Button();
- this.btnExit = new System.Windows.Forms.Button();
- this.lblSearch = new System.Windows.Forms.Label();
- this.txtSearch = new System.Windows.Forms.TextBox();
- this.btnFind = new System.Windows.Forms.Button();
- this.dataGridView1 = new System.Windows.Forms.DataGridView();
- this.label1 = new System.Windows.Forms.Label();
- ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
- this.SuspendLayout();
- //
- // lblStudentId
- //
- this.lblStudentId.BackColor = System.Drawing.Color.Transparent;
- this.lblStudentId.Font = new System.Drawing.Font("Times New Roman", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
- this.lblStudentId.ForeColor = System.Drawing.Color.WhiteSmoke;
- this.lblStudentId.Location = new System.Drawing.Point(20, 71);
- this.lblStudentId.Name = "lblStudentId";
- this.lblStudentId.Size = new System.Drawing.Size(100, 20);
- this.lblStudentId.TabIndex = 0;
- this.lblStudentId.Text = "Student ID:";
- //
- // txtStudentId
- //
- this.txtStudentId.Location = new System.Drawing.Point(153, 71);
- this.txtStudentId.Name = "txtStudentId";
- this.txtStudentId.Size = new System.Drawing.Size(200, 20);
- this.txtStudentId.TabIndex = 1;
- //
- // lblStudentName
- //
- this.lblStudentName.BackColor = System.Drawing.Color.Transparent;
- this.lblStudentName.Font = new System.Drawing.Font("Times New Roman", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
- this.lblStudentName.ForeColor = System.Drawing.Color.WhiteSmoke;
- this.lblStudentName.Location = new System.Drawing.Point(20, 111);
- this.lblStudentName.Name = "lblStudentName";
- this.lblStudentName.Size = new System.Drawing.Size(100, 20);
- this.lblStudentName.TabIndex = 2;
- this.lblStudentName.Text = "Student Name:";
- //
- // txtStudentName
- //
- this.txtStudentName.Location = new System.Drawing.Point(153, 111);
- this.txtStudentName.Name = "txtStudentName";
- this.txtStudentName.Size = new System.Drawing.Size(200, 20);
- this.txtStudentName.TabIndex = 3;
- //
- // lblAddress
- //
- this.lblAddress.BackColor = System.Drawing.Color.Transparent;
- this.lblAddress.Font = new System.Drawing.Font("Times New Roman", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
- this.lblAddress.ForeColor = System.Drawing.Color.WhiteSmoke;
- this.lblAddress.Location = new System.Drawing.Point(20, 155);
- this.lblAddress.Name = "lblAddress";
- this.lblAddress.Size = new System.Drawing.Size(100, 20);
- this.lblAddress.TabIndex = 4;
- this.lblAddress.Text = "Address:";
- //
- // txtAddress
- //
- this.txtAddress.Location = new System.Drawing.Point(153, 155);
- this.txtAddress.Name = "txtAddress";
- this.txtAddress.Size = new System.Drawing.Size(200, 20);
- this.txtAddress.TabIndex = 5;
- //
- // lblClass
- //
- this.lblClass.BackColor = System.Drawing.Color.Transparent;
- this.lblClass.Font = new System.Drawing.Font("Times New Roman", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
- this.lblClass.ForeColor = System.Drawing.Color.WhiteSmoke;
- this.lblClass.Location = new System.Drawing.Point(20, 196);
- this.lblClass.Name = "lblClass";
- this.lblClass.Size = new System.Drawing.Size(100, 20);
- this.lblClass.TabIndex = 6;
- this.lblClass.Text = "Class:";
- //
- // txtClass
- //
- this.txtClass.Location = new System.Drawing.Point(153, 196);
- this.txtClass.Name = "txtClass";
- this.txtClass.Size = new System.Drawing.Size(200, 20);
- this.txtClass.TabIndex = 7;
- //
- // btnInsert
- //
- this.btnInsert.BackColor = System.Drawing.Color.Black;
- this.btnInsert.FlatAppearance.BorderColor = System.Drawing.Color.White;
- this.btnInsert.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
- this.btnInsert.ForeColor = System.Drawing.Color.White;
- this.btnInsert.Location = new System.Drawing.Point(23, 279);
- this.btnInsert.Name = "btnInsert";
- this.btnInsert.Size = new System.Drawing.Size(75, 23);
- this.btnInsert.TabIndex = 8;
- this.btnInsert.Text = "Insert";
- this.btnInsert.UseVisualStyleBackColor = false;
- this.btnInsert.Click += new System.EventHandler(this.btnInsert_Click);
- //
- // btnUpdate
- //
- this.btnUpdate.BackColor = System.Drawing.Color.Black;
- this.btnUpdate.FlatAppearance.BorderColor = System.Drawing.Color.White;
- this.btnUpdate.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
- this.btnUpdate.ForeColor = System.Drawing.Color.White;
- this.btnUpdate.Location = new System.Drawing.Point(153, 279);
- this.btnUpdate.Name = "btnUpdate";
- this.btnUpdate.Size = new System.Drawing.Size(75, 23);
- this.btnUpdate.TabIndex = 9;
- this.btnUpdate.Text = "Update";
- this.btnUpdate.UseVisualStyleBackColor = false;
- this.btnUpdate.Click += new System.EventHandler(this.btnUpdate_Click);
- //
- // btnDelete
- //
- this.btnDelete.BackColor = System.Drawing.Color.Black;
- this.btnDelete.FlatAppearance.BorderColor = System.Drawing.Color.White;
- this.btnDelete.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
- this.btnDelete.ForeColor = System.Drawing.Color.White;
- this.btnDelete.Location = new System.Drawing.Point(278, 279);
- this.btnDelete.Name = "btnDelete";
- this.btnDelete.Size = new System.Drawing.Size(75, 23);
- this.btnDelete.TabIndex = 10;
- this.btnDelete.Text = "Delete";
- this.btnDelete.UseVisualStyleBackColor = false;
- this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
- //
- // btnShow
- //
- this.btnShow.BackColor = System.Drawing.Color.Black;
- this.btnShow.FlatAppearance.BorderColor = System.Drawing.Color.White;
- this.btnShow.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
- this.btnShow.ForeColor = System.Drawing.Color.White;
- this.btnShow.Location = new System.Drawing.Point(87, 339);
- this.btnShow.Name = "btnShow";
- this.btnShow.Size = new System.Drawing.Size(75, 23);
- this.btnShow.TabIndex = 11;
- this.btnShow.Text = "Show";
- this.btnShow.UseVisualStyleBackColor = false;
- this.btnShow.Click += new System.EventHandler(this.btnShow_Click);
- //
- // btnExit
- //
- this.btnExit.BackColor = System.Drawing.Color.Black;
- this.btnExit.FlatAppearance.BorderColor = System.Drawing.Color.White;
- this.btnExit.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
- this.btnExit.ForeColor = System.Drawing.Color.White;
- this.btnExit.Location = new System.Drawing.Point(219, 339);
- this.btnExit.Name = "btnExit";
- this.btnExit.Size = new System.Drawing.Size(75, 23);
- this.btnExit.TabIndex = 12;
- this.btnExit.Text = "Exit";
- this.btnExit.UseVisualStyleBackColor = false;
- this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
- //
- // lblSearch
- //
- this.lblSearch.BackColor = System.Drawing.Color.Transparent;
- this.lblSearch.Font = new System.Drawing.Font("Times New Roman", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
- this.lblSearch.ForeColor = System.Drawing.Color.WhiteSmoke;
- this.lblSearch.Location = new System.Drawing.Point(394, 71);
- this.lblSearch.Name = "lblSearch";
- this.lblSearch.Size = new System.Drawing.Size(55, 20);
- this.lblSearch.TabIndex = 13;
- this.lblSearch.Text = "Search:";
- //
- // txtSearch
- //
- this.txtSearch.Location = new System.Drawing.Point(455, 68);
- this.txtSearch.Name = "txtSearch";
- this.txtSearch.Size = new System.Drawing.Size(258, 20);
- this.txtSearch.TabIndex = 14;
- //
- // btnFind
- //
- this.btnFind.Location = new System.Drawing.Point(719, 66);
- this.btnFind.Name = "btnFind";
- this.btnFind.Size = new System.Drawing.Size(50, 23);
- this.btnFind.TabIndex = 15;
- this.btnFind.Text = "Find";
- this.btnFind.Click += new System.EventHandler(this.btnFind_Click);
- //
- // dataGridView1
- //
- this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
- this.dataGridView1.Location = new System.Drawing.Point(397, 111);
- this.dataGridView1.Name = "dataGridView1";
- this.dataGridView1.Size = new System.Drawing.Size(391, 210);
- this.dataGridView1.TabIndex = 16;
- this.dataGridView1.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellContentClick);
- //
- // label1
- //
- this.label1.AutoSize = true;
- this.label1.BackColor = System.Drawing.Color.Transparent;
- this.label1.Font = new System.Drawing.Font("Times New Roman", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
- this.label1.ForeColor = System.Drawing.Color.WhiteSmoke;
- this.label1.ImageAlign = System.Drawing.ContentAlignment.TopCenter;
- this.label1.Location = new System.Drawing.Point(229, 9);
- this.label1.Name = "label1";
- this.label1.Size = new System.Drawing.Size(345, 31);
- this.label1.TabIndex = 17;
- this.label1.Text = "Student Registration System";
- this.label1.TextAlign = System.Drawing.ContentAlignment.TopCenter;
- //
- // Form1
- //
- this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
- this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
- this.ClientSize = new System.Drawing.Size(800, 450);
- this.Controls.Add(this.label1);
- this.Controls.Add(this.dataGridView1);
- this.Controls.Add(this.lblStudentId);
- this.Controls.Add(this.txtStudentId);
- this.Controls.Add(this.lblStudentName);
- this.Controls.Add(this.txtStudentName);
- this.Controls.Add(this.lblAddress);
- this.Controls.Add(this.txtAddress);
- this.Controls.Add(this.lblClass);
- this.Controls.Add(this.txtClass);
- this.Controls.Add(this.btnInsert);
- this.Controls.Add(this.btnUpdate);
- this.Controls.Add(this.btnDelete);
- this.Controls.Add(this.btnShow);
- this.Controls.Add(this.btnExit);
- this.Controls.Add(this.lblSearch);
- this.Controls.Add(this.txtSearch);
- this.Controls.Add(this.btnFind);
- this.Name = "Form1";
- this.Text = "Student Registration Form";
- ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
- this.ResumeLayout(false);
- this.PerformLayout();
- }
- #endregion
- private System.Windows.Forms.Label lblStudentId;
- private System.Windows.Forms.TextBox txtStudentId;
- private System.Windows.Forms.Label lblStudentName;
- private System.Windows.Forms.TextBox txtStudentName;
- private System.Windows.Forms.Label lblAddress;
- private System.Windows.Forms.TextBox txtAddress;
- private System.Windows.Forms.Label lblClass;
- private System.Windows.Forms.TextBox txtClass;
- private System.Windows.Forms.Button btnUpdate;
- private System.Windows.Forms.Button btnDelete;
- private System.Windows.Forms.Button btnShow;
- private System.Windows.Forms.Button btnExit;
- private System.Windows.Forms.Label lblSearch;
- private System.Windows.Forms.TextBox txtSearch;
- private System.Windows.Forms.Button btnFind;
- private Button btnInsert;
- private DataGridView dataGridView1;
- private Label label1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement