Code to Create Connection String Page:
-------------------------------------
DAL Code:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
namespace VehicleManagement
{
public class VehicalDetailsDAL
{
public int VehicalDetailsId { get; set; }
public int OwnerId { get; set; }
public string VehicalName { get; set; }
public string ChechisNo { get; set; }
public string VehicalNo { get; set; }
SqlConnection conn = new SqlConnection(Setting.ConnectionString);
---------------------------------------------------------------------------------------------------
To Insert:-
public int AddVehical(VehicalDetailsDAL vehicalDetailsDAL)
{
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "usp_VehicalDetails";
cmd.Parameters.AddWithValue("@VehicalDetailsId", vehicalDetailsDAL.VehicalDetailsId);
cmd.Parameters.AddWithValue("@OwnerDetailsId", vehicalDetailsDAL.OwnerId);
cmd.Parameters.AddWithValue("@VehicalName", vehicalDetailsDAL.VehicalName);
cmd.Parameters.AddWithValue("@ChechisNo", vehicalDetailsDAL.ChechisNo);
cmd.Parameters.AddWithValue("@VehicalNo", vehicalDetailsDAL.VehicalNo);
int VehicalDetailsId = Convert.ToInt16(cmd.ExecuteScalar());
conn.Close();
return VehicalDetailsId;
}
---------------------------------------------------------------------------------------------------
To Delete:-
public void DeleteVehical(int VehicalDetailsId)
{
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "usp_DeleteVehicalDetails";
cmd.Parameters.AddWithValue("@VehicalDetailsId", VehicalDetailsId);
cmd.ExecuteNonQuery();
conn.Close();
}
---------------------------------------------------------------------------------------------------
To Search:-
public VehicalDetailsDAL SearchOwnerDetails(int VehicalDetailsId)
{
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "usp_SearchVehicalDetails";
SqlDataReader dr = cmd.ExecuteReader();
VehicalDetailsDAL v = new VehicalDetailsDAL();
while (dr.Read())
{
v.VehicalDetailsId = Convert.ToInt16(dr["VehicalDetailsId"]);
v.OwnerId = Convert.ToInt16(dr["OwnerDetailsId"]);
v.VehicalName = dr["VehicalName"].ToString();
v.ChechisNo = dr["ChechisNo"].ToString();
v.VehicalNo = dr["VehicalNo"].ToString();
}
conn.Close();
return v;
}
---------------------------------------------------------------------------------------------------
To Update:-
public void UpdateVehicalDetails(VehicalDetailsDAL vehicalDetailsDAL)
{
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "usp_UpdateVehicalDetails";
cmd.Parameters.AddWithValue("@VehicalDetailsId", vehicalDetailsDAL.VehicalDetailsId);
cmd.Parameters.AddWithValue("@OwnerDetailsId", vehicalDetailsDAL.OwnerId);
cmd.Parameters.AddWithValue("@VehicalName", vehicalDetailsDAL.VehicalName);
cmd.Parameters.AddWithValue("@ChechisNo", vehicalDetailsDAL.ChechisNo);
cmd.Parameters.AddWithValue("@VehicalNo", vehicalDetailsDAL.VehicalNo);
cmd.ExecuteNonQuery();
conn.Close();
}
---------------------------------------------------------------------------------------------------
}
}
-------------------------------------
DAL Code:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
namespace VehicleManagement
{
public class VehicalDetailsDAL
{
public int VehicalDetailsId { get; set; }
public int OwnerId { get; set; }
public string VehicalName { get; set; }
public string ChechisNo { get; set; }
public string VehicalNo { get; set; }
SqlConnection conn = new SqlConnection(Setting.ConnectionString);
---------------------------------------------------------------------------------------------------
To Insert:-
public int AddVehical(VehicalDetailsDAL vehicalDetailsDAL)
{
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "usp_VehicalDetails";
cmd.Parameters.AddWithValue("@VehicalDetailsId", vehicalDetailsDAL.VehicalDetailsId);
cmd.Parameters.AddWithValue("@OwnerDetailsId", vehicalDetailsDAL.OwnerId);
cmd.Parameters.AddWithValue("@VehicalName", vehicalDetailsDAL.VehicalName);
cmd.Parameters.AddWithValue("@ChechisNo", vehicalDetailsDAL.ChechisNo);
cmd.Parameters.AddWithValue("@VehicalNo", vehicalDetailsDAL.VehicalNo);
int VehicalDetailsId = Convert.ToInt16(cmd.ExecuteScalar());
conn.Close();
return VehicalDetailsId;
}
---------------------------------------------------------------------------------------------------
To Delete:-
public void DeleteVehical(int VehicalDetailsId)
{
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "usp_DeleteVehicalDetails";
cmd.Parameters.AddWithValue("@VehicalDetailsId", VehicalDetailsId);
cmd.ExecuteNonQuery();
conn.Close();
}
---------------------------------------------------------------------------------------------------
To Search:-
public VehicalDetailsDAL SearchOwnerDetails(int VehicalDetailsId)
{
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "usp_SearchVehicalDetails";
SqlDataReader dr = cmd.ExecuteReader();
VehicalDetailsDAL v = new VehicalDetailsDAL();
while (dr.Read())
{
v.VehicalDetailsId = Convert.ToInt16(dr["VehicalDetailsId"]);
v.OwnerId = Convert.ToInt16(dr["OwnerDetailsId"]);
v.VehicalName = dr["VehicalName"].ToString();
v.ChechisNo = dr["ChechisNo"].ToString();
v.VehicalNo = dr["VehicalNo"].ToString();
}
conn.Close();
return v;
}
---------------------------------------------------------------------------------------------------
To Update:-
public void UpdateVehicalDetails(VehicalDetailsDAL vehicalDetailsDAL)
{
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "usp_UpdateVehicalDetails";
cmd.Parameters.AddWithValue("@VehicalDetailsId", vehicalDetailsDAL.VehicalDetailsId);
cmd.Parameters.AddWithValue("@OwnerDetailsId", vehicalDetailsDAL.OwnerId);
cmd.Parameters.AddWithValue("@VehicalName", vehicalDetailsDAL.VehicalName);
cmd.Parameters.AddWithValue("@ChechisNo", vehicalDetailsDAL.ChechisNo);
cmd.Parameters.AddWithValue("@VehicalNo", vehicalDetailsDAL.VehicalNo);
cmd.ExecuteNonQuery();
conn.Close();
}
---------------------------------------------------------------------------------------------------
}
}
------------------------------ BAL------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VehicleManagement;
namespace VehicalManagementBAL
{
public class OwnerDetailsBAL
{
public string AddOwnerBAL(OwnerDetailsDAL ownerDetailsDAL)
{
if (string.IsNullOrEmpty(ownerDetailsDAL.OwnerFirstName))
{
throw new Exception("OwnerFirstName can not be Empty");
}
if (string.IsNullOrEmpty(ownerDetailsDAL.OwnerMiddleName))
{
throw new Exception("ownerMiddleName can not be empty");
}
if (string.IsNullOrEmpty(ownerDetailsDAL.OwnerLastName))
{
throw new Exception("ownerLastName can not be Empty");
}
return ownerDetailsDAL.AddOwner(ownerDetailsDAL).ToString ();
}
-----------------------------------------------------
public DataSet SerachUserRecord(string txtFirstName)
{
SqlConnection con = new SqlConnection(Setting.ConnectionString);
string query = "Select u.FirstName,p.ContactNumber,e.UserEmailId from UserInformation u inner join PhoneNumber p on u.UserId=p.UserId inner join EmailId e on p.UserId=e.UserId where u.FirstName='txtSearch.Text%'";
con.Open();
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@txtFirstName", txtFirstName);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
UserInformationDal userinfo = new UserInformationDal();
userinfo.FirstName = ds.Tables[0].Rows[0]["FirstName"].ToString();
userinfo.ContactNumber = ds.Tables[0].Rows[0]["ContactNumber"].ToString();
userinfo.UserEmailId = ds.Tables[0].Rows[0]["UserEmailId"].ToString();
}
con.Close();
return ds;
---------------------------------------
Aspx-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VehicleManagement;
using VehicalManagementBAL;
namespace VehicleManagementSystem
{
public partial class VehicleInformation : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSaveDetails_Click(object sender, EventArgs e)
{
VehicalDetailsDAL vehicalDetailsDAL = new VehicalDetailsDAL();
vehicalDetailsDAL.VehicalName = txtVehicleName.Text;
vehicalDetailsDAL.VehicalNo = txtVehicleNo.Text;
vehicalDetailsDAL.ChechisNo = txtChechsisNo.Text;
lblMessage.Text = "Added successfully ";
}
}
}
--------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VehicleManagement;
namespace VehicalManagementBAL
{
public class OwnerDetailsBAL
{
public string AddOwnerBAL(OwnerDetailsDAL ownerDetailsDAL)
{
if (string.IsNullOrEmpty(ownerDetailsDAL.OwnerFirstName))
{
throw new Exception("OwnerFirstName can not be Empty");
}
if (string.IsNullOrEmpty(ownerDetailsDAL.OwnerMiddleName))
{
throw new Exception("ownerMiddleName can not be empty");
}
if (string.IsNullOrEmpty(ownerDetailsDAL.OwnerLastName))
{
throw new Exception("ownerLastName can not be Empty");
}
return ownerDetailsDAL.AddOwner(ownerDetailsDAL).ToString ();
}
-----------------------------------------------------
public DataSet SerachUserRecord(string txtFirstName)
{
SqlConnection con = new SqlConnection(Setting.ConnectionString);
string query = "Select u.FirstName,p.ContactNumber,e.UserEmailId from UserInformation u inner join PhoneNumber p on u.UserId=p.UserId inner join EmailId e on p.UserId=e.UserId where u.FirstName='txtSearch.Text%'";
con.Open();
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@txtFirstName", txtFirstName);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
UserInformationDal userinfo = new UserInformationDal();
userinfo.FirstName = ds.Tables[0].Rows[0]["FirstName"].ToString();
userinfo.ContactNumber = ds.Tables[0].Rows[0]["ContactNumber"].ToString();
userinfo.UserEmailId = ds.Tables[0].Rows[0]["UserEmailId"].ToString();
}
con.Close();
return ds;
---------------------------------------
Aspx-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VehicleManagement;
using VehicalManagementBAL;
namespace VehicleManagementSystem
{
public partial class VehicleInformation : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSaveDetails_Click(object sender, EventArgs e)
{
VehicalDetailsDAL vehicalDetailsDAL = new VehicalDetailsDAL();
vehicalDetailsDAL.VehicalName = txtVehicleName.Text;
vehicalDetailsDAL.VehicalNo = txtVehicleNo.Text;
vehicalDetailsDAL.ChechisNo = txtChechsisNo.Text;
lblMessage.Text = "Added successfully ";
}
}
}
--------------------------------------
Thanx shail for your valuable guidance ......
ReplyDelete