-----------------------------------------------------------
-----------------------------------------------------------
Repository Project
-----------------------------------------------------------
dalc.cs -- DataServices
-----------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Repository.DataServices
{
public class dalc
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Cn_Team"].ConnectionString);
public dalc()
{
//
// TODO: Add constructor logic here
//
}
public SqlCommand GetCommand()
{
SqlCommand cmd = new SqlCommand();
cmd.CommandTimeout = 0;
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
return cmd;
}
public List<T> selectbyqueryList<T>(string SPName, SqlParameter[] para)
{
DataTable dt = new DataTable();
SqlCommand cmd = GetCommand();
cmd.CommandText = SPName.ToString();
cmd.Parameters.AddRange(para);
SqlDataReader dr;
try
{
conn.Open();
dr = cmd.ExecuteReader();
List<T> lst = new List<T>();
while (dr.Read())
{
T item = CommonFunctions.GetItem<T>(dr);
lst.Add(item);
}
return lst;
}
catch (Exception e)
{
throw e;
}
finally
{
conn.Close();
cmd.Parameters.Clear();
cmd.Dispose();
// conn.Dispose();
}
}
public DataTable SelectAll(string SPName, SqlParameter[] para)
{
DataTable dt = new DataTable();
SqlCommand cmd = GetCommand();
cmd.CommandText = SPName.ToString();
cmd.Parameters.AddRange(para);
SqlDataAdapter da = new SqlDataAdapter(cmd);
try
{
conn.Open();
da.Fill(dt);
return dt;
}
catch (Exception e)
{
throw e;
}
finally
{
conn.Close();
cmd.Dispose();
}
}
public void IUD(string SPName, SqlParameter[] para)
{
SqlCommand cmd = GetCommand();
cmd.CommandText = SPName.ToString();
cmd.Parameters.AddRange(para);
try
{
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
catch (Exception ex)
{
throw ex;
}
finally
{
conn.Close();
cmd.Dispose();
}
}
}
}
-----------------------------------------------------------
Repository Project
-----------------------------------------------------------
dalc.cs -- DataServices
-----------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Repository.DataServices
{
public class dalc
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Cn_Team"].ConnectionString);
public dalc()
{
//
// TODO: Add constructor logic here
//
}
public SqlCommand GetCommand()
{
SqlCommand cmd = new SqlCommand();
cmd.CommandTimeout = 0;
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
return cmd;
}
public List<T> selectbyqueryList<T>(string SPName, SqlParameter[] para)
{
DataTable dt = new DataTable();
SqlCommand cmd = GetCommand();
cmd.CommandText = SPName.ToString();
cmd.Parameters.AddRange(para);
SqlDataReader dr;
try
{
conn.Open();
dr = cmd.ExecuteReader();
List<T> lst = new List<T>();
while (dr.Read())
{
T item = CommonFunctions.GetItem<T>(dr);
lst.Add(item);
}
return lst;
}
catch (Exception e)
{
throw e;
}
finally
{
conn.Close();
cmd.Parameters.Clear();
cmd.Dispose();
// conn.Dispose();
}
}
public DataTable SelectAll(string SPName, SqlParameter[] para)
{
DataTable dt = new DataTable();
SqlCommand cmd = GetCommand();
cmd.CommandText = SPName.ToString();
cmd.Parameters.AddRange(para);
SqlDataAdapter da = new SqlDataAdapter(cmd);
try
{
conn.Open();
da.Fill(dt);
return dt;
}
catch (Exception e)
{
throw e;
}
finally
{
conn.Close();
cmd.Dispose();
}
}
public void IUD(string SPName, SqlParameter[] para)
{
SqlCommand cmd = GetCommand();
cmd.CommandText = SPName.ToString();
cmd.Parameters.AddRange(para);
try
{
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
catch (Exception ex)
{
throw ex;
}
finally
{
conn.Close();
cmd.Dispose();
}
}
}
}
-----------------------------------------------------------
BALgetpara.cs -- DataServices
-----------------------------------------------------------
BALgetpara.cs -- DataServices
-----------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Repository.DataServices
{
public class BALGetpara
{
public SqlParameter Getpara(string ParaName, string ParaValue)
{
SqlParameter para = new SqlParameter();
para.SqlDbType = SqlDbType.VarChar;
para.SqlValue = ParaValue;
para.ParameterName = ParaName;
return para;
}
public SqlParameter Getpara(string paraname, decimal paravalue)
{
SqlParameter para = new SqlParameter();
para.ParameterName = paraname.ToString();
para.Value = paravalue.ToString();
para.SqlDbType = SqlDbType.Decimal;
return para;
}
public SqlParameter Getpara(string ParaName, int Paravalue)
{
SqlParameter para = new SqlParameter();
para.SqlDbType = SqlDbType.Int;
para.SqlValue = Paravalue;
para.ParameterName = ParaName;
return para;
}
public SqlParameter Getpara(string ParaName, bool Paravalue)
{
SqlParameter para = new SqlParameter();
para.SqlDbType = SqlDbType.Bit;
para.SqlValue = Paravalue;
para.ParameterName = ParaName;
return para;
}
//public SqlParameter Getpara(string paraname, bool paravalue)
//{
// SqlParameter para = new SqlParameter();
// para.ParameterName = paraname.ToString();
// para.Value = paravalue.ToString();
// para.SqlDbType = SqlDbType.VarChar;
// return para;
//}
public SqlParameter Getpara(string paraname, DateTime paravalue)
{
SqlParameter para = new SqlParameter();
para.ParameterName = paraname.ToString();
para.Value = paravalue.ToString();
para.SqlDbType = SqlDbType.DateTime;
return para;
}
public SqlParameter Getpara(string paraname, float paravalue)
{
SqlParameter para = new SqlParameter();
para.ParameterName = paraname.ToString();
para.Value = paravalue.ToString();
para.SqlDbType = SqlDbType.Float;
return para;
}
public SqlParameter Getpara(string paraname, double paravalue)
{
SqlParameter para = new SqlParameter();
para.ParameterName = paraname.ToString();
para.Value = paravalue.ToString();
para.SqlDbType = SqlDbType.Float;
return para;
}
}
}
-----------------------------------------------------------
CommonFunctions.cs -- DataServices
-----------------------------------------------------------
CommonFunctions.cs -- DataServices
-----------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace Repository.DataServices
{
public static class CommonFunctions
{
public static List<T> ConvertToList<T>(this DataTable dt)
{
List<T> data = new List<T>();
foreach (DataRow row in dt.Rows)
{
T item = GetTItem<T>(row);
data.Add(item);
}
return data;
}
private static T GetTItem<T>(DataRow dr)
{
Type temp = typeof(T);
T obj = Activator.CreateInstance<T>();
foreach (DataColumn column in dr.Table.Columns)
{
foreach (PropertyInfo pro in temp.GetProperties())
{
object value = dr[column.ColumnName];
if (value != DBNull.Value)
{
if (pro.Name == column.ColumnName)
pro.SetValue(obj, dr[column.ColumnName], null);
else
continue;
}
}
}
return obj;
}
public static T GetItem<T>(SqlDataReader dr)
{
Type temp = typeof(T);
T obj = Activator.CreateInstance<T>();
for (int i = 0; i < dr.FieldCount; i++)
{
foreach (PropertyInfo pro in temp.GetProperties())
{
if (pro.Name.ToLower() == dr.GetName(i).ToLower())
{
if (!string.IsNullOrEmpty(Convert.ToString(dr[i])))
{
if (pro.PropertyType.Name == "String")
pro.SetValue(obj, Convert.ToString(dr[i]));
else if (pro.PropertyType.Name == "Byte[]" && string.IsNullOrEmpty(Convert.ToString(dr[i])))
pro.SetValue(obj, new byte[0]);
else
pro.SetValue(obj, dr[i]);
}
break;
}
}
}
return obj;
}
}
}
-----------------------------------------------------------
EmpDTO.cs -- DTO
-----------------------------------------------------------
EmpDTO.cs -- DTO
-----------------------------------------------------------
namespace Repository.DTO
{
public class EmpDTO
{
public int Id { get; set; }
public string Name { get; set; }
public string City { get; set; }
public decimal Salary { get; set; }
}
}
-----------------------------------------------------------
CommonRepository.cs -- Service
-----------------------------------------------------------
-----------------------------------------------------------
CommonRepository.cs -- Service
-----------------------------------------------------------
using Repository.DataServices;
using Repository.DTO;
using Repository.ServiceContact;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Repository.Service
{
public class CommonRepository : ICommonRepository
{
public CommonRepository()
{
}
public DataTable GetAll()
{
DataTable obj = new DataTable();
BALGetpara ogetpara = new BALGetpara();
SqlParameter[] para = new SqlParameter[1];
para[0] = ogetpara.Getpara("@mode", "ALL");
dalc odal = new dalc();
return odal.SelectAll("SP_Select", para);
}
public EmpDTO GetById(int Id)
{
BALGetpara ogetpara = new BALGetpara();
SqlParameter[] para = new SqlParameter[2];
para[0] = ogetpara.Getpara("@mode", "GET");
para[1] = ogetpara.Getpara("@Id", Id);
List<EmpDTO> obj = new dalc().selectbyqueryList<EmpDTO>("SP_Select", para);
return obj[0];
}
public void Save(EmpDTO obj,string mode)
{
try
{
BALGetpara ogetpara = new BALGetpara();
SqlParameter[] para = new SqlParameter[4];
para[0] = ogetpara.Getpara("@mode", mode);
para[1] = ogetpara.Getpara("@Name", obj.Name);
para[2] = ogetpara.Getpara("@City", obj.City);
para[3] = ogetpara.Getpara("@Salary", obj.Salary);
new dalc().IUD("SP_IUD", para);
}
catch (Exception ex)
{
}
}
public void Delete(int Id)
{
try
{
BALGetpara ogetpara = new BALGetpara();
SqlParameter[] para = new SqlParameter[2];
para[0] = ogetpara.Getpara("@mode", "DELETE");
para[1] = ogetpara.Getpara("@Id", Id);
new dalc().IUD("SP_IUD", para);
}
catch (Exception ex)
{
}
}
}
}
-----------------------------------------------------------
ICommonRepository.cs -- Service
-----------------------------------------------------------
ICommonRepository.cs -- Service
-----------------------------------------------------------
using Repository.DTO;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Repository.ServiceContact
{
public interface ICommonRepository
{
DataTable GetAll();
EmpDTO GetById(int Id);
void Save(EmpDTO obj, string mode);
void Delete(int Id);
}
}
-----------------------------------------------------------
-----------------------------------------------------------
Webapplication Project
-----------------------------------------------------------
-----------------------------------------------------------
Default.aspx
-----------------------------------------------------------
-----------------------------------------------------------
-----------------------------------------------------------
Webapplication Project
-----------------------------------------------------------
-----------------------------------------------------------
Default.aspx
-----------------------------------------------------------
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication2._Default" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<div>
<table style="width: 100%;">
<%-- <tr>
<td> ID :- </td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
<td> </td>
</tr>--%>
<tr>
<td>Name :-</td>
<td>
<asp:HiddenField ID="hdnId" runat="server" />
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</td>
<td> </td>
</tr>
<tr>
<td>City :-</td>
<td>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</td>
<td> </td>
</tr>
<tr>
<td>Salary :-</td>
<td>
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
</td>
<td> </td>
</tr>
<tr>
<td> </td>
<td>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Add" />
</td>
<td> </td>
</tr>
</table>
</div>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</asp:Content>
-----------------------------------------------------------
Default.aspx.cs
-----------------------------------------------------------
Default.aspx.cs
-----------------------------------------------------------
using Repository.DTO;
using Repository.Service;
using Repository.ServiceContact;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication2
{
public partial class _Default : Page
{
private ICommonRepository _comRes = new CommonRepository();
DataSet ds = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}
}
public void bind()
{
ds.Tables.Add(_comRes.GetAll());
GridView1.DataSource = ds.Tables["Table1"];
GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
EmpDTO emp = new EmpDTO();
//emp.Id = Convert.ToInt16(TextBox1.Text);
emp.Name = TextBox2.Text;
emp.City = TextBox3.Text;
emp.Salary = Convert.ToInt16(TextBox4.Text);
_comRes.Save(emp, "INSERT");
bind();
}
}
}
-----------------------------------------------------------
Web.Config
-----------------------------------------------------------
-----------------------------------------------------------
Web.Config
-----------------------------------------------------------
<connectionStrings>
<add name="Cn_Team" connectionString="data source=DESKTOP-9QF30OA;initial catalog=EmpDB;persist security info=True;Integrated Security=True;" providerName="System.Data.SqlClient"/>
<add name="Cn_Team" connectionString="data source=myIp;initial catalog=myDB;persist security info=True;user id=myUser;password=myPasssword;Connection Timeout=150;Connection Lifetime=0;Min Pool Size=0;Max Pool Size=10000;Pooling=true;"/>
</connectionStrings>
-----------------------------------------------------------
SM
-----------------------------------------------------------
SM
-----------------------------------------------------------
Console.Write("Enter Number : ");
int n = Convert.ToInt32(Console.ReadLine());
int[,] arr = new int[n, n];
int c = 0;
int r = 0;
int l = n * n;
string d = "R";
for (int i = 1; i <= l; i++)
{
if ((d == "R") && (c > n - 1 || arr[r, c] != 0))
{
d = "D"; c--; r++;
}
else if ((d == "D") && (r > n - 1 || arr[r, c] != 0))
{
d = "L"; c--; r--;
}
else if ((d == "L") && (c < 0 || arr[r, c] != 0))
{
d = "U"; c++; r--;
}
else if ((d == "U") && (r < 0 || arr[r, c] != 0))
{
d = "R"; c++; r++;
}
arr[r, c] = i;
if (d == "R") c++;
else if (d == "D") r++;
else if (d == "L") c--;
else if (d == "U") r--;
}
for (int j = 0; j < n; j++)
{
for (int k = 0; k < n; k++)
{
Console.Write("{0,4}", arr[j, k]);
}
Console.WriteLine();
}
Console.ReadLine();
int n = Convert.ToInt32(Console.ReadLine());
int[,] arr = new int[n, n];
int c = 0;
int r = 0;
int l = n * n;
string d = "R";
for (int i = 1; i <= l; i++)
{
if ((d == "R") && (c > n - 1 || arr[r, c] != 0))
{
d = "D"; c--; r++;
}
else if ((d == "D") && (r > n - 1 || arr[r, c] != 0))
{
d = "L"; c--; r--;
}
else if ((d == "L") && (c < 0 || arr[r, c] != 0))
{
d = "U"; c++; r--;
}
else if ((d == "U") && (r < 0 || arr[r, c] != 0))
{
d = "R"; c++; r++;
}
arr[r, c] = i;
if (d == "R") c++;
else if (d == "D") r++;
else if (d == "L") c--;
else if (d == "U") r--;
}
for (int j = 0; j < n; j++)
{
for (int k = 0; k < n; k++)
{
Console.Write("{0,4}", arr[j, k]);
}
Console.WriteLine();
}
Console.ReadLine();