Visual Studio C# 2019 (Service-Based Database)
Visual Studio C# 2019 (Service-Based Database)
cs
using System.Data.SqlClient;
using System.Windows.Forms;
namespace WindowsFormsApp1.connection
{
class connection
{
}
Function > function.cs
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
namespace WindowsFormsApp1.function
{
class function : Form1
{
}
catch (Exception ex)
{
Console.Write(ex);
}
}
}
}
SUBMIT-BUTTON
try
{
connection.connection.DB();
string gen = "Insert into stud(studid, studname)values(" + txtstudid.Text + ",'" + txtstudname.Text + "')";
SqlCommand command = new SqlCommand(gen, connection.connection.conn);
command.ExecuteNonQuery();
MessageBox.Show("Successfully Saved");
Form1_Load(sender, e);
Clear();
connection.connection.conn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public void Clear()
{
txtstudid.Clear();
txtstudname.Clear();
}
EDIT-BUTTON
string q = "update stud SET studname = '" + txtstudname.Text + "' where studid = " +
txtstudid.Text + "";
SqlCommand comm = new SqlCommand(q, connection.connection.conn);
comm.ExecuteNonQuery();
MessageBox.Show("Edited Successfully!");
Form1_Load(sender, e);
Clear();
connection.connection.conn.Close();
}
catch (Exception x)
{
MessageBox.Show(x.Message);
}
}
DELETE-BUTTON
}
catch (Exception x)
{
MessageBox.Show(x.Message);
}
}
TXTSEARCH_TEXTCHANGED
function.function.dataReader = comm.ExecuteReader();
if (function.function.dataReader.Read())
{
txtstudid.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
txtstudname.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
FORM1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
try
{
connection.connection.DB();
string gen = "Insert into stud(studid, studname)values(" + txtstudid.Text + ",'" + txtstudname.Text + "')";
SqlCommand command = new SqlCommand(gen, connection.connection.conn);
command.ExecuteNonQuery();
MessageBox.Show("Successfully Saved");
Form1_Load(sender, e);
Clear();
connection.connection.conn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public void Clear()
{
txtstudid.Clear();
txtstudname.Clear();
}
string q = "update stud SET studname = '" + txtstudname.Text + "' where studid = " +
txtstudid.Text + "";
SqlCommand comm = new SqlCommand(q, connection.connection.conn);
comm.ExecuteNonQuery();
MessageBox.Show("Edited Successfully!");
Form1_Load(sender, e);
Clear();
connection.connection.conn.Close();
}
catch (Exception x)
{
MessageBox.Show(x.Message);
}
}
catch (Exception x)
{
MessageBox.Show(x.Message);
}
}
private ‘void Txtsearch_TextChanged(object sender, EventArgs e)
{
try
{
connection.connection.DB();
string a = "Select * from stud where studid LIKE " + "'%" + txtsearch.Text + "%' or studname LIKE '%" + txtsearch.Text +
"%'";
function.function.datagridfill(a, dataGridView1);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
function.function.dataReader = comm.ExecuteReader();
if (function.function.dataReader.Read())
{
txtstudid.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
txtstudname.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}