Dear Readers,
Today I Publish an article i.e.., Windows Forms Application with DataGridView using layered Architecture With Validations in C#.Net
In order to accomplish this task we should follow the following steps :
1.Create a Solution and Name it as WindowsApp
2.Create a Two Folders Named it as BAL,DAL
3.Prepare Database with provided semantic of the table
4.Implement a code related App.config
5.Create Two ClassLibraries named as AppBAL.cs under BAL folder AppDAL.cs under DAL folder and implement necessary code
6.Design windows Form and Implement necessary Code
7. Run windows application
1.Create a Solution and Name it as WindowsApp
2.Create a Two Folders Named it as BAL,DAL
3.Prepare Database with provided semantic of the table
USE [Test]
GO
/****** Object: Table [dbo].[Product] Script Date: 10/11/2016 19:23:28 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Product](
[SNO] [int] IDENTITY(1,1) NOT NULL,
[PID] [nvarchar](20) NULL,
[PName] [nvarchar](20) NULL,
[ItemCount] [int] NULL,
[SDate] [datetime] NULL
) ON [PRIMARY]
GO
SET IDENTITY_INSERT [dbo].[Product] ON
INSERT [dbo].[Product] ([SNO], [PID], [PName], [ItemCount], [SDate]) VALUES (1, N'PNV001', N'Mastering with C#', -1, CAST(0x0000A4C700BDAA69 AS DateTime))
INSERT [dbo].[Product] ([SNO], [PID], [PName], [ItemCount], [SDate]) VALUES (2, N'PNV002', N'Mastering with ASP', 5, CAST(0x0000A4C700BDAA6A AS DateTime))
INSERT [dbo].[Product] ([SNO], [PID], [PName], [ItemCount], [SDate]) VALUES (3, N'PNV003', N'WebServices/WCF', 4, CAST(0x0000A4C700BDAA6B AS DateTime))
INSERT [dbo].[Product] ([SNO], [PID], [PName], [ItemCount], [SDate]) VALUES (4, N'PNV004', N'JQuery', 10, NULL)
INSERT [dbo].[Product] ([SNO], [PID], [PName], [ItemCount], [SDate]) VALUES (6, N'PNV005', N'MVC', 15, CAST(0x0000A52700B55D87 AS DateTime))
INSERT [dbo].[Product] ([SNO], [PID], [PName], [ItemCount], [SDate]) VALUES (8, N'PNV006', N'JDBC With Sql Server', 5, CAST(0x0000A65200B03D30 AS DateTime))
INSERT [dbo].[Product] ([SNO], [PID], [PName], [ItemCount], [SDate]) VALUES (9, N'PNV007', N'JSP With Sql Server', 5, CAST(0x0000A65200B06F94 AS DateTime))
INSERT [dbo].[Product] ([SNO], [PID], [PName], [ItemCount], [SDate]) VALUES (10, N'PNV008', N'JQuery', 14, CAST(0x0000A69C010C8C0C AS DateTime))
INSERT [dbo].[Product] ([SNO], [PID], [PName], [ItemCount], [SDate]) VALUES (11, N'PNV009', N'MVC', 230, CAST(0x0000A69C010D559C AS DateTime))
INSERT [dbo].[Product] ([SNO], [PID], [PName], [ItemCount], [SDate]) VALUES (12, N'PNV010', N'Mastering with ASP', 123, CAST(0x0000A69C0134CB2C AS DateTime))
SET IDENTITY_INSERT [dbo].[Product] OFF
/****** Object: Default [DF__Product__SDate__00551192] Script Date: 10/11/2016 19:23:28 ******/
ALTER TABLE [dbo].[Product] ADD DEFAULT (getdate()) FOR [SDate]
GO
Today I Publish an article i.e.., Windows Forms Application with DataGridView using layered Architecture With Validations in C#.Net
In order to accomplish this task we should follow the following steps :
1.Create a Solution and Name it as WindowsApp
2.Create a Two Folders Named it as BAL,DAL
3.Prepare Database with provided semantic of the table
4.Implement a code related App.config
5.Create Two ClassLibraries named as AppBAL.cs under BAL folder AppDAL.cs under DAL folder and implement necessary code
6.Design windows Form and Implement necessary Code
7. Run windows application
1.Create a Solution and Name it as WindowsApp
2.Create a Two Folders Named it as BAL,DAL
3.Prepare Database with provided semantic of the table
USE [Test]
GO
/****** Object: Table [dbo].[Product] Script Date: 10/11/2016 19:23:28 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Product](
[SNO] [int] IDENTITY(1,1) NOT NULL,
[PID] [nvarchar](20) NULL,
[PName] [nvarchar](20) NULL,
[ItemCount] [int] NULL,
[SDate] [datetime] NULL
) ON [PRIMARY]
GO
SET IDENTITY_INSERT [dbo].[Product] ON
INSERT [dbo].[Product] ([SNO], [PID], [PName], [ItemCount], [SDate]) VALUES (1, N'PNV001', N'Mastering with C#', -1, CAST(0x0000A4C700BDAA69 AS DateTime))
INSERT [dbo].[Product] ([SNO], [PID], [PName], [ItemCount], [SDate]) VALUES (2, N'PNV002', N'Mastering with ASP', 5, CAST(0x0000A4C700BDAA6A AS DateTime))
INSERT [dbo].[Product] ([SNO], [PID], [PName], [ItemCount], [SDate]) VALUES (3, N'PNV003', N'WebServices/WCF', 4, CAST(0x0000A4C700BDAA6B AS DateTime))
INSERT [dbo].[Product] ([SNO], [PID], [PName], [ItemCount], [SDate]) VALUES (4, N'PNV004', N'JQuery', 10, NULL)
INSERT [dbo].[Product] ([SNO], [PID], [PName], [ItemCount], [SDate]) VALUES (6, N'PNV005', N'MVC', 15, CAST(0x0000A52700B55D87 AS DateTime))
INSERT [dbo].[Product] ([SNO], [PID], [PName], [ItemCount], [SDate]) VALUES (8, N'PNV006', N'JDBC With Sql Server', 5, CAST(0x0000A65200B03D30 AS DateTime))
INSERT [dbo].[Product] ([SNO], [PID], [PName], [ItemCount], [SDate]) VALUES (9, N'PNV007', N'JSP With Sql Server', 5, CAST(0x0000A65200B06F94 AS DateTime))
INSERT [dbo].[Product] ([SNO], [PID], [PName], [ItemCount], [SDate]) VALUES (10, N'PNV008', N'JQuery', 14, CAST(0x0000A69C010C8C0C AS DateTime))
INSERT [dbo].[Product] ([SNO], [PID], [PName], [ItemCount], [SDate]) VALUES (11, N'PNV009', N'MVC', 230, CAST(0x0000A69C010D559C AS DateTime))
INSERT [dbo].[Product] ([SNO], [PID], [PName], [ItemCount], [SDate]) VALUES (12, N'PNV010', N'Mastering with ASP', 123, CAST(0x0000A69C0134CB2C AS DateTime))
SET IDENTITY_INSERT [dbo].[Product] OFF
/****** Object: Default [DF__Product__SDate__00551192] Script Date: 10/11/2016 19:23:28 ******/
ALTER TABLE [dbo].[Product] ADD DEFAULT (getdate()) FOR [SDate]
GO
4.Implement a code related App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" />
</startup>
<connectionStrings>
<add name="prod" connectionString="Server=.;IntegratedSecurity=true;Database=Test"/>
</connectionStrings>
</configuration>
5.Create Two ClassLibraries named as AppBAL.cs under BAL
folder AppDAL.cs under DAL folder and implement necessary code
AppBAL.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WindowsApp.DAL;
namespace WindowsApp.BAL
{
class AppBAL
{
AppDAL obj = new AppDAL();
public int InsertData(string Pid, string PName, int ItemCount)
{
return obj.InsertData(Pid,PName,ItemCount);
}
public DataSet getProductNames()
{
return obj.getProductNames();
}
public DataSet getgridData()
{
return obj.getgridData();
}
public int UpdateData(string Pid, string PName, int ItemCount)
{
return obj.UpdateData(Pid, PName, ItemCount);
}
}
}
APPDAL.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace WindowsApp.DAL
{
public class AppDAL
{
public string ConString = "Data Source=.;Initial Catalog=test;Integrated Security=True";
SqlConnection con = new SqlConnection();
DataTable dt = new DataTable();
public AppDAL()
{
}
public int InsertData(string Pid,string PName,int ItemCount)
{
int i = 0;
using (SqlConnection con = new SqlConnection(ConString))
{
con.Open();
SqlCommand cmd = new SqlCommand("insert into product (PID,PName,ItemCount,SDate) values('" + Pid + "','" + PName + "'," + ItemCount + ",'" + System.DateTime.Now.ToString() + "')", con);
cmd.CommandType = CommandType.Text;
i = cmd.ExecuteNonQuery();
return i;
}
}
public DataSet getProductNames()
{
SqlConnection con = new SqlConnection(ConString);
SqlDataAdapter da = new SqlDataAdapter("Select distinct PName from product", con);
DataSet ds = new DataSet();
da.Fill(ds, "X");
return ds;
}
public DataSet getgridData()
{
SqlConnection con = new SqlConnection(ConString);
SqlDataAdapter da = new SqlDataAdapter("Select PID,PName,ItemCount from Product order by 1 desc", con);
DataSet ds = new DataSet();
da.Fill(ds, "X");
return ds;
}
public int UpdateData(string Pid, string PName, int ItemCount)
{
int i = 0;
using (SqlConnection con = new SqlConnection(ConString))
{
con.Open();
using (SqlCommand cmd = new SqlCommand("update product set PName ='"+PName+"',ItemCount = "+ItemCount+" where PID='"+Pid+"' ", con))
{
cmd.CommandType = CommandType.Text;
i = cmd.ExecuteNonQuery();
return i;
}
}
}
}
}
6.Design windows Form and Implement necessary Code
Form1.cs[Design] Page :
Form1.cs Page Code :
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 WindowsApp.BAL;
namespace WindowsApp
{
public partial class Form1 : Form
{
AppBAL obj = new AppBAL();
public Form1()
{
InitializeComponent();
}
private void btnSubmit_Click(object sender, EventArgs e)
{
int ValidateCount = 0;
foreach (Control c1 in this.Controls)
{
if (c1 is TextBox)
{
if (string.IsNullOrEmpty(c1.Text))
{
MessageBox.Show("Text Box Field cannnot be blank");
c1.BackColor = Color.Red;
ValidateCount = ValidateCount + 1;
c1.Focus();
}
else
c1.BackColor = Color.White;
}
if (c1 is ComboBox)
{
if (string.IsNullOrEmpty(c1.Text))
{
MessageBox.Show("ComboBox cann't be blank");
c1.BackColor = Color.Red;
ValidateCount = ValidateCount + 1;
c1.Focus();
}
else
c1.BackColor = Color.White;
}
}
if (ValidateCount == 0)
{
if (btnSubmit.Text.Equals("Submit", StringComparison.InvariantCultureIgnoreCase))
{
int res = obj.InsertData(txtPID.Text, cmbPName.SelectedItem.ToString(), Convert.ToInt32(txtCount.Text));
if (res > 0)
{
MessageBox.Show("Data Inserted Sucessfully", "Alert");
LoadData();
}
else
{
MessageBox.Show("Data Not Inserted", "Alert");
}
}
else if (btnSubmit.Text.Equals("Update", StringComparison.InvariantCultureIgnoreCase))
{
int res = obj.UpdateData(txtPID.Text, cmbPName.SelectedItem.ToString(), Convert.ToInt32(txtCount.Text));
if (res > 0)
{
MessageBox.Show("Data Update Sucessfully", "Alert");
LoadData();
btnSubmit.Text = "Submit";
}
else
{
MessageBox.Show("Data Not Update ", "Alert");
}
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
DataSet ds = obj.getProductNames();
if (!object.ReferenceEquals(ds, null) && ds.Tables[0].Rows.Count > 0)
{
for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
{
cmbPName.Items.Add(ds.Tables[0].Rows[i][0].ToString());
}
}
LoadData();
this.Text = "Windows DataGridView Events With CheckBox including with Validation From PNV Technical Hub";
}
private void LoadData()
{
DataGridViewCheckBoxColumn checkboxColumn = new DataGridViewCheckBoxColumn();
checkboxColumn.Width = 30;
checkboxColumn.HeaderText = "Edit";
checkboxColumn.Name = "checkBoxColumn";
checkboxColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
dataGridView1.Columns.Insert(0, checkboxColumn);
DataSet gridDatads = obj.getgridData();
if (!object.ReferenceEquals(gridDatads, null) && gridDatads.Tables[0].Rows.Count > 0)
{
for (int i = 0; i <= gridDatads.Tables[0].Rows.Count - 1; i++)
{
dataGridView1.DataSource = gridDatads;
dataGridView1.DataMember = "X";
}
}
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
string PID = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
string PName = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
int ItemCount = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString());
txtPID.Text = PID;
cmbPName.SelectedItem = PName;
txtCount.Text = ItemCount.ToString();
btnSubmit.Text = "Update";
}
private void btnGetData_Click(object sender, EventArgs e)
{
string message = string.Empty;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
bool isSelected = Convert.ToBoolean(row.Cells["checkBoxColumn"].Value);
if (isSelected)
{
message += Environment.NewLine;
message += row.Cells["PName"].Value.ToString();
}
}
MessageBox.Show("Selected Values" + message, "Selected Values Message Box");
}
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
private void txtCount_Leave(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txtCount.Text))
{
if (Convert.ToInt32(txtCount.Text) < 11)
{
errorProvider1.SetError(txtCount, "Please Enter Values in between 0 to 100 ");
}
else
{
if (Convert.ToInt32(txtCount.Text) > 100)
errorProvider1.SetError(txtCount, "Please Enter Values in between 0 to 100 ");
else
errorProvider1.SetError(txtCount, string.Empty);
}
}
}
private void txtCount_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled=!char.IsDigit(e.KeyChar);
if(!e.Handled)
{
if (!string.IsNullOrEmpty(txtCount.Text))
{
if (Convert.ToInt32(txtCount.Text) < 11)
{
errorProvider1.SetError(txtCount, "Please Enter Values in between 0 to 100 ");
e.Handled = false;
}
else
{
if (Convert.ToInt32(txtCount.Text) > 100)
errorProvider1.SetError(txtCount, "Please Enter Values in between 0 to 100 ");
else
errorProvider1.SetError(txtCount, string.Empty);
}
}
}
}
private void btnClear_Click(object sender, EventArgs e)
{
foreach (Control c1 in this.Controls)
{
if(c1 is TextBox || c1 is ComboBox)
c1.Text = string.Empty;
}
}
}
}
No comments :
Post a Comment