0% found this document useful (0 votes)
20 views5 pages

database code

Uploaded by

bushrayawar928
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
20 views5 pages

database code

Uploaded by

bushrayawar928
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 5

BLL code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestConnection.BLL
{
internal class userbll
{
public int Id { get; set; }
public string First_name { get; set; }
public string Last_name { get; set; }
public string Email { get; set; }
public string Username { get; set; }
public string Password{ get; set; }
public string Address { get; set; }
public string Contact_no { get; set; }
public string Gender { get; set; }
public string User_type { get; set; }
public DateTime Added_date { get; set; }
public int Added_by { get; set; }
}
}
DAL CODE
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;
using System.Windows.Forms;
using TestConnection.BLL;

namespace TestConnection.DAL
{
internal class userdal
{
static string myconnstring =
ConfigurationManager.ConnectionStrings["connstring"].ConnectionString;
#region select Data from Database
public DataTable select()
{
SqlConnection conn = new SqlConnection(myconnstring);
DataTable dt = new DataTable();
try
{
string sql = "SELECT * FROM tbl_user";
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
conn.Open();
adapter.Fill(dt);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
return dt;
}
#endregion

#region Insert Data into Database


public bool INSERT(userbll u)
{
bool isSuccess = false;
SqlConnection conn = new SqlConnection(myconnstring);
try
{
string sql = "INSERT INTO tbl_user(First_name, Last_name, Email, Username, Password,
Address, Contact_no, Gender, User_type, Added_date, Added_by) VALUES (@First_name,
@Last_name, @Email, @Username, @Password, @Address, @Contact_no, @Gender, @User_type,
@Added_date, @Added_by)";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.AddWithValue("@First_name", u.First_name);
cmd.Parameters.AddWithValue("@Last_name", u.Last_name);
cmd.Parameters.AddWithValue("@Email", u.Email);
cmd.Parameters.AddWithValue("@Username", u.Username);
cmd.Parameters.AddWithValue("@Password", u.Password);
cmd.Parameters.AddWithValue("@Address", u.Address);
cmd.Parameters.AddWithValue("@Contact_no", u.Contact_no);
cmd.Parameters.AddWithValue("@Gender", u.Gender);
cmd.Parameters.AddWithValue("@User_type", u.User_type);
cmd.Parameters.AddWithValue("@Added_date", u.Added_date);
cmd.Parameters.AddWithValue("@Added_by", u.Added_by);

conn.Open();
int rows = cmd.ExecuteNonQuery();
if(rows>0)
{
isSuccess = true;
}
else
{
isSuccess = false;
}

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
return isSuccess;
}
#endregion

#region update Data into Database


public bool UPDATE(userbll u)
{
bool isSuccess = false;
SqlConnection conn = new SqlConnection(myconnstring);
try
{
string sql = "UPDATE tbl_user SET First_name=@First_name, Last_name=@Last_name,
Email=@Email, Username=@Username, Password=@Password, Address=@Address,
Contact_no=@Contact_no, Gender=@Gender, User_type=@User_type,
Added_date=@Added_date, Added_by=@Added_by WHERE Id=@Id";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.AddWithValue("@First_name", u.First_name);
cmd.Parameters.AddWithValue("@Last_name", u.Last_name);
cmd.Parameters.AddWithValue("@Email", u.Email);
cmd.Parameters.AddWithValue("@Username", u.Username);
cmd.Parameters.AddWithValue("@Password", u.Password);
cmd.Parameters.AddWithValue("@Address", u.Address);
cmd.Parameters.AddWithValue("@Contact_no", u.Contact_no);
cmd.Parameters.AddWithValue("@Gender", u.Gender);
cmd.Parameters.AddWithValue("@User_type", u.User_type);
cmd.Parameters.AddWithValue("@Added_date", u.Added_date);
cmd.Parameters.AddWithValue("@Added_by", u.Added_by);
cmd.Parameters.AddWithValue("@Id", u.Id);

conn.Open();
int rows = cmd.ExecuteNonQuery();
if (rows > 0)
{
isSuccess = true;
}
else
{
isSuccess = false;
}

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
return isSuccess;
}
#endregion

#region Delete Data From Database


public bool DELETE(userbll U)
{
bool isSuccess = false;
SqlConnection conn = new SqlConnection(myconnstring);
try
{
string sql = "DELETE FROM tbl_user WHERE Id=@Id";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.AddWithValue("@Id", U.Id);
conn.Open();
int rows = cmd.ExecuteNonQuery();
if(rows>0)
{
isSuccess = true;
}
else
{
isSuccess = false;
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close ();
}
return isSuccess;
}
#endregion

#region search Data from Database


public DataTable Search(string keywords)
{
SqlConnection conn = new SqlConnection(myconnstring);
DataTable dt = new DataTable();
try
{
string sql = "SELECT * FROM tbl_user WHERE Id LIKE '%" + keywords + "%' OR
First_name LIKE '%" + keywords + "%' OR Last_name LIKE '%" + keywords + "%' OR Username
LIKE '%" + keywords + "%'";
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
conn.Open();
adapter.Fill(dt);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
return dt;
}
#endregion

#region Getting user ID from username


public userbll GetIDfromusername(string username)
{
userbll u = new userbll();
SqlConnection conn = new SqlConnection(myconnstring);
DataTable dt = new DataTable();
try
{
string sql = "SELECT id FROM tbl_user WHERE Username =" + username + "";
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
conn.Open();
adapter.Fill(dt);
if(dt.Rows.Count>0)
{
u.Id = int.Parse(dt.Rows[0]["Id"].ToString());
}
else
{

}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
return u;
}
#endregion

}
}
APP.CONFIG
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
<connectionStrings>
<add name="connstring" connectionString="Data Source=ALIHA\SQLEXPRESS;Initial
Catalog=Mart;Integrated Security=True;"/>
</connectionStrings>
</configuration>

You might also like