Hi,@Farshad Valizade . Welcome Microsoft Q&A.
For the three layers winform project, you could try the following example.
Form1.cs:
using WinFormsApp1.BLL;
using WinFormsApp1.DAL;
namespace WinFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnAdd_Click(object sender, EventArgs e)
{
DocumentHandler documentH = new DocumentHandler();
if (documentH.InsertDocument(new DAL.Document() { DocRev = int.Parse(txtDocRev.Text), DocTitle = txtDocTitle.Text, DocID= int.Parse(txtDocId.Text) }))
{
MessageBox.Show("Add Success");
dataGridView2.DataSource = documentH.GetDocuments();
}
}
private void dataGridView2_SelectionChanged(object sender, EventArgs e)
{
if (dataGridView2.SelectedRows.Count > 0) // make sure user select at least 1 row
{
string Id = dataGridView2.SelectedRows[0].Cells[0].Value + string.Empty;
string Rev = dataGridView2.SelectedRows[0].Cells[1].Value + string.Empty;
string Title = dataGridView2.SelectedRows[0].Cells[2].Value + string.Empty;
txtDocId.Text = Id;
txtDocTitle.Text = Title;
txtDocRev.Text = Rev;
}
}
private void btnEdit_Click(object sender, EventArgs e)
{
DocumentHandler documentH = new DocumentHandler();
if (documentH.UpdateDocument(new DAL.Document() { DocRev = int.Parse(txtDocRev.Text), DocTitle = txtDocTitle.Text,
DocID = int.Parse(txtDocId.Text)
}))
{
MessageBox.Show("Add Success");
dataGridView2.DataSource = documentH.GetDocuments();
}
}
private void btnSelect_Click(object sender, EventArgs e)
{
try
{
DocumentHandler d = new DocumentHandler();
this.dataGridView2.DataSource = d.GetDocuments(Convert.ToInt16(this.txtID.Text));
}
catch
{
MessageBox.Show("Error Occurred");
}
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
DocumentHandler d = new DocumentHandler();
this.dataGridView2.DataSource = d.GetDocuments();
}
catch
{
MessageBox.Show("Error Occurred");
}
}
}
}
BLL:
using System.Data;
using WinFormsApp1.DAL;
using Document = WinFormsApp1.DAL.Document;
namespace WinFormsApp1.BLL
{
public class DocumentHandler
{
public DataTable GetDocuments()
{
try
{
DocumentDAL objdal = new DocumentDAL();
return objdal.Read();
}
catch
{
throw;
}
}
public DataTable GetDocuments(Int16 ID)
{
try
{
DocumentDAL objdal = new DocumentDAL();
return objdal.Read(ID);
}
catch
{
throw;
}
}
public bool InsertDocument(Document document)
{
if (DocumentDAL.Validate(document))
{
DocumentDAL.Add(document);
return true;
}
return false;
}
public bool UpdateDocument(Document document)
{
if (DocumentDAL.Validate(document))
{
DocumentDAL.Update(document);
return true;
}
return false;
}
}
}
DAL:
using System.Data;
using System.Data.SqlClient;
namespace WinFormsApp1.DAL
{
public class Document
{
public int DocID { get; set; }
public int DocRev { get; set; }
public string DocTitle { get; set; }
}
internal class DocumentDAL
{
public static string ConString = "constr";
static SqlConnection conn = new SqlConnection(ConString);
DataTable dt = new DataTable();
public DataTable Read()
{
if (ConnectionState.Closed == conn.State)
conn.Open();
SqlCommand cmd = new SqlCommand("select * from [dbo].[Document]", conn);
try
{
SqlDataReader rd = cmd.ExecuteReader();
dt.Load(rd);
return dt;
}
catch
{
throw;
}
}
public DataTable Read(Int16 Id)
{
if (ConnectionState.Closed == conn.State)
conn.Open();
SqlCommand cmd = new SqlCommand("select * from Person where ID= " + Id + "", conn);
try
{
SqlDataReader rd = cmd.ExecuteReader();
dt.Load(rd);
return dt;
}
catch
{
throw;
}
}
public DataTable Select()
{
if (ConnectionState.Closed == conn.State)
conn.Open();
string sqlcommand = "select * from [dbo].[Document]";
SqlDataAdapter da = new SqlDataAdapter(sqlcommand, conn);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public static void Add(Document d)
{
if (ConnectionState.Closed == conn.State)
conn.Open();
string query = "insert into [dbo].[Document]([DocID],[DocTitle],[DocRev]) values(@docid, @doctitle,@docrev)";
SqlCommand cmd = new SqlCommand(query, conn);
cmd.Parameters.AddWithValue("@docid", d.DocID);
cmd.Parameters.AddWithValue("@doctitle", d.DocTitle);
cmd.Parameters.AddWithValue("@docrev", d.DocRev);
cmd.ExecuteNonQuery();
}
public static void Update(Document d)
{
if (ConnectionState.Closed == conn.State)
conn.Open();
string query = "update [dbo].[Document] set DocTitle = @doctitle,DocRev=@docrev where DocID = @docid";
SqlCommand cmd = new SqlCommand(query, conn);
cmd.Parameters.AddWithValue("@docid", d.DocID);
cmd.Parameters.AddWithValue("@doctitle", d.DocTitle);
cmd.Parameters.AddWithValue("@docrev", d.DocRev);
cmd.ExecuteNonQuery();
}
internal static bool Validate(Document d)
{
return true;
// some validations before insert
}
}
}
The result:
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.