Thursday, December 31, 2015
Tuesday, December 29, 2015
Parsing values in C# - A Global Method for Nullable Values
Casting is important and it does cost a lot in our applications. It becomes a bit more hard when we need to treat nullable variables. Many conditions to check whether the type is nullable and has value ..etc is sometimes a too much work. What if we use a common global method that we pass the type and the value that we need to cast. Look at the below method,
In above static method named as Parse,
The <T> which is the type that we need to convert the value into and the value which is the object is passed and simple enough to handle even nullable values.
As above, you can use this global method to do casting without any issue with nullable values.
Sometimes laziness help solving problems easier .. lol
public static T Parse<T>(object value)
{
try { return (T)System.ComponentModel.TypeDescriptor.GetConverter(typeof(T)).ConvertFrom(value.ToString()); }
catch { return default(T); }
}
In above static method named as Parse,
The <T> which is the type that we need to convert the value into and the value which is the object is passed and simple enough to handle even nullable values.
int? id = Parse<int?>(txtid.Text.Trim());
As above, you can use this global method to do casting without any issue with nullable values.
Sometimes laziness help solving problems easier .. lol
Tuesday, December 22, 2015
Bootstrap Accordion for ASP.Net
As discussed before as well, the Bootstrap framework is a great tool that we can use for web applications to increase the functionalities as well as development time with rich look to the presentation.
Accordion is a way of displaying information in collapsible panels so that visitors can expand it and see as needed. By using such a way, we can keep the web page simple but with lot of details inside collapsible panels.
For example, consider a search page where you will have lot of controls for user to select different kinds of parameters to set for searching records. So the whole search will require 80% from page where you will have only 20% for displaying information as well as footer values.
We can simply introduce the collapsible panels known as Accordion with Bootstrap to this page and can have a nicer look. See below for example.
If you copy this and try it on your application, you will see you will have two collapsible panels. You can open and close them by clicking on the header.
Further, you can dynamically open and close the required panels using JQuery. Look at the below code.
calling these function, you will find it is very easy to deal with collapsible panels in Bootstrap with the help of JQuery.
Try it. If any issues, post under comments.
Accordion is a way of displaying information in collapsible panels so that visitors can expand it and see as needed. By using such a way, we can keep the web page simple but with lot of details inside collapsible panels.
For example, consider a search page where you will have lot of controls for user to select different kinds of parameters to set for searching records. So the whole search will require 80% from page where you will have only 20% for displaying information as well as footer values.
We can simply introduce the collapsible panels known as Accordion with Bootstrap to this page and can have a nicer look. See below for example.
<div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
<div class="panel panel-default">
<div class="panel-heading" role="tab" id="headingOne" data-toggle="collapse" data-parent="#accordion"
href="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
<h4 class="panel-title">
<span class="text-defaultcolor fa fa-search fa-2x text-titleSize17"></span>
HEADER TEXT
</h4>
</div>
<div id="collapseOne" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="headingOne">
<div class="panel-body">
BODY OF FIRST PANEL
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading" role="tab" id="headingTwo" class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion"
href="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
<h4 class="panel-title">
<span class="text-defaultcolor fa fa-list fa-2x text-titleSize17"></span>
HEADER TEXT
</h4>
</div>
<div id="collapseTwo" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingTwo">
<div class="panel-body">
BODY OF SECOND PANEL
</div>
</div>
</div>
</div>
If you copy this and try it on your application, you will see you will have two collapsible panels. You can open and close them by clicking on the header.
Further, you can dynamically open and close the required panels using JQuery. Look at the below code.
function ShowSearchParameters() {
$('#collapseOne').collapse('show');
$('#collapseTwo').collapse('hide');
}
function ShowSearchResult() {
$('#collapseTwo').collapse('show');
$('#collapseOne').collapse('hide');
}
calling these function, you will find it is very easy to deal with collapsible panels in Bootstrap with the help of JQuery.
Try it. If any issues, post under comments.
Thursday, December 17, 2015
Using HttpContext.Current.Application to Store Data
We do database CRUD operations all the time in our applications. And some are expensive and some are not. But when it comes to Web Applications no matter the size, accessing database frequently is really affecting to the performance of the web site.
Usually for global data access such as application tokens, we can store them as Application objects to be retrieved as and when needed irrespective of the users who log in to the website. For example, look at the below method.
In InitializeTokens() method we get all the tokens from the database through TokenDAL database class and add them to HttpContext.Current.Application object.
The above code will easily get the value of the token by its id stored before. Using OOP concepts, we can use this same methods to be used across the application so that accessing common data makes faster than before.
Usually for global data access such as application tokens, we can store them as Application objects to be retrieved as and when needed irrespective of the users who log in to the website. For example, look at the below method.
public void InitializeTokens()
{
List<TokenEntity> tokenList = new List<TokenEntity>();
tokenList = new TokenDAL().GetTokens();
foreach (var item in tokenList)
{
HttpContext.Current.Application[item.TokenId].ToString().ToUpper()] = item.TokenName.ToString();
}
}
In InitializeTokens() method we get all the tokens from the database through TokenDAL database class and add them to HttpContext.Current.Application object.
HttpContext.Current.Application.Get(token.TokenId).ToString();
The above code will easily get the value of the token by its id stored before. Using OOP concepts, we can use this same methods to be used across the application so that accessing common data makes faster than before.
Tuesday, December 15, 2015
Ingres data access layer in C#
We had discussed about Ingres as our database for application development. So lets move ahead and implement an Ingres supported data access layer.
Simply copy the following class and add it to your data layer project as your data handler.
Simply as in SQL Server the ExecuteNonQuery will return an integer as an return type which you can track any error inside the method.
ExecuteQuery will return a IngresDataReader object that you can loop through till it ends and append the data into a list or a collection.
So this simple class will make you easy for using ingres and the only disadvantage here is to use sql queries as inputs to the methods.
It is also possible to use Transactions which in Ingres it is called, IngresTransaction class. You may try adding an IngresTransaction to this class and make it more valuable to the project.
Comment if you find it is interesting or need some help on this class.
Simply copy the following class and add it to your data layer project as your data handler.
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Ingres;
using Ingres.Client;
using System.Collections;
public class DataHandle
{
#region - Class Properties -
public IngresConnection Connection;
#endregion
#region - Constructors -
public DataHandle(string connectionString)
{
Connection = new IngresConnection(connectionString);
}
public DataHandle()
{
Connection = new IngresConnection(ConnectionString);
}
#endregion
public IngresDataReader ExecuteQuery(string sql)
{
IngresCommand ingresCommand = null;
try
{
if (this.Connection.State != ConnectionState.Open)
this.Connection.Open();
ingresCommand = new IngresCommand(sql, this.Connection);
ingresCommand.CommandType = CommandType.Text;
ingresCommand.CommandTimeout = 0;
IngresDataReader ingresDataReader = ingresCommand.ExecuteReader(CommandBehavior.CloseConnection);
return ingresDataReader;
}
catch
{
throw;
}
finally
{
if (ingresCommand != null)
ingresCommand.Dispose();
}
}
public IngresDataReader ExecuteQuery(string sql, IngresParameterCollection parameters)
{
IngresCommand ingresCommand = null;
try
{
if (this.Connection.State != ConnectionState.Open)
this.Connection.Open();
ingresCommand = new IngresCommand(sql, this.Connection);
ingresCommand.CommandType = CommandType.Text;
ingresCommand.CommandTimeout = 0;
if (parameters != null)
{
foreach (IngresParameter parameter in parameters)
{
ingresCommand.Parameters.Add(parameter);
}
}
IngresDataReader ingresDataReader = ingresCommand.ExecuteReader(CommandBehavior.CloseConnection);
return ingresDataReader;
}
catch
{
throw;
}
finally
{
if (ingresCommand != null)
ingresCommand.Dispose();
}
}
public int ExecuteNonQuery(string sql)
{
try
{
if (this.Connection.State != ConnectionState.Open)
this.Connection.Open();
IngresCommand ingresCommand = new IngresCommand(sql, this.Connection);
ingresCommand.CommandTimeout = 0;
int numberOfRecordsAffected = ingresCommand.ExecuteNonQuery();
return numberOfRecordsAffected;
}
catch
{
throw;
}
finally
{
if (this.Connection != null)
Connection.Close();
}
}
public int ExecuteNonQuery(string sql, IngresParameterCollection parameters)
{
try
{
if (this.Connection.State != ConnectionState.Open)
this.Connection.Open();
IngresCommand ingresCommand = new IngresCommand(sql, this.Connection);
ingresCommand.CommandTimeout = 0;
foreach (IngresParameter parameter in parameters)
{
ingresCommand.Parameters.Add(parameter);
}
int numberOfRecordsAffected = ingresCommand.ExecuteNonQuery();
return numberOfRecordsAffected;
}
catch
{
throw;
}
finally
{
if (this.Connection != null)
Connection.Close();
}
}
}
Simply as in SQL Server the ExecuteNonQuery will return an integer as an return type which you can track any error inside the method.
ExecuteQuery will return a IngresDataReader object that you can loop through till it ends and append the data into a list or a collection.
So this simple class will make you easy for using ingres and the only disadvantage here is to use sql queries as inputs to the methods.
It is also possible to use Transactions which in Ingres it is called, IngresTransaction class. You may try adding an IngresTransaction to this class and make it more valuable to the project.
Comment if you find it is interesting or need some help on this class.
Saturday, December 12, 2015
Code in C# and Ingres as your database
Most of the software development cases we face daily is addressed with certain technology. For example, if the application is developed in PHP, the database is MySQL or if it is Microsoft.Net then most probably it is MsSQL Server.
likewise, you can have your own methods to support for your data layer. Try it and it's worth. And the performance is also impressive.
We will discuss a bit more about ingres data access in our future posts. Until that, happy coding.. :)
What if we need to make a difference or the business case is not the common way we practice? For instance, a certain company may find Microsoft development platform with a separate database provider. As an example, the application can be implemented in C# and Database could be Ingres.
Lets get in to business. What we need is to download the ingres database from here and the .Net connector from here.
Make sure you register your ingres client in your project.
And the following classes will do the needful if you use it correctly for database operations.
IngresConnection for DB Connection
IngresTransaction for Transaction management
IngresDataReader for data reading
IngresParameterCollection for parameters
IngresCommand for DB Commands
The use of above classes as follows,
IngresConnection Connection = new IngresConnection("your connection string here");
And the below will return a data reader object.
public IngresDataReader ExecuteQuery(string sql, IngresParameterCollection parameters)
{
IngresCommand ingresCommand = null;
try
{
if (this.Connection.State != ConnectionState.Open)
this.Connection.Open();
ingresCommand = new IngresCommand(sql, this.Connection);
ingresCommand.CommandType = CommandType.Text;
ingresCommand.CommandTimeout = 0;
if (parameters != null)
{
foreach (IngresParameter parameter in parameters)
{
ingresCommand.Parameters.Add(parameter);
}
}
IngresDataReader ingresDataReader = ingresCommand.ExecuteReader(CommandBehavior.CloseConnection);
return ingresDataReader;
}
catch
{
throw;
}
finally
{
if (ingresCommand != null)
ingresCommand.Dispose();
}
}
likewise, you can have your own methods to support for your data layer. Try it and it's worth. And the performance is also impressive.
We will discuss a bit more about ingres data access in our future posts. Until that, happy coding.. :)
Thursday, December 10, 2015
JavaScriptSerializer in C# and JQuery AJAX - ASP.Net
This topic could have been more familiar and kind of outdated to many. But I think it is good to see how we use JavaScriptSerializer in our applications to work with entities without a language or technology barriers.
Suppose we have an entity ( class with set of properties ) class and we use it as data objects in our application. And now we need it to be serialize into JavaScript objects to be used in Ajax. We can easily follow the below way,
1. Create an entity class
2. And your asmx (web service) file has the below web method.
Here we use Serialize method to convert the object into de-serializable string and your JavaScript function can handle the response as follows,
The JSON result is the list of objects that you pass from your service. In this way, we can deal with larger objects but we need to concern about the JSON limitations. We will discuss about how we can overcome JSON limitations in the future posts. Until that, happy coding.. :)
Suppose we have an entity ( class with set of properties ) class and we use it as data objects in our application. And now we need it to be serialize into JavaScript objects to be used in Ajax. We can easily follow the below way,
1. Create an entity class
public class personEntity
{
public int? idEntity{ get; set; }
public int nameEntity{ get; set; }
}
2. And your asmx (web service) file has the below web method.
[WebMethod]
public string GetEntityList ()
{
return new JavaScriptSerializer().Serialize(new PersonBLL().GetPersonList());
}
Here we use Serialize method to convert the object into de-serializable string and your JavaScript function can handle the response as follows,
function GetPersonList() {
$.ajax({
type: "POST",
url: "ServiceURL/GetPersonList",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
var ret = JSON.parse(msg.d);
$.each(ret, function (i, item) {
alert(item.idEntity);
alert(item.nameEntity);
});
},
error: function (err) {
alert(err.responseText);
}
});
}
The JSON result is the list of objects that you pass from your service. In this way, we can deal with larger objects but we need to concern about the JSON limitations. We will discuss about how we can overcome JSON limitations in the future posts. Until that, happy coding.. :)
Monday, December 7, 2015
Side bar easy navigation with JQuery & Css, Bootstrap
When a web application is developed, the user friendliness is the most important factor. No matter how we write the data access class or business logic, the presentation is the key factor for user to feel easiness & comfort in the application. So making it a bit fancy with controls that make the user easy for navigation in a single page is important.
Certainly there is a rich menu in your application for navigation. May be the menu could be really complex to design and implement with user level restrictions with authentication and authorization to each modules.
What if the navigated page is loo lengthy and need to scroll down to see more content. Or what if your page fetch the content to the bottom each time you scroll down such as listing items? Here is an easy way.
1. Download the VCore Sidebar from here
2. Reference it from your page. (After JQuery reference)
3. Add a div on top of your page
4. And then create the control as below. Add any items as needed.
3. Here the fortawesome is used for icons and you can download the fortawesome here.
4. The following css class is added to the vcore.sidebar.css to make the items center.
5. Finally, you may get a nice set of vertical menu bar like this;
Adding a bit of JQuery for scrolling transitions, you may feel a lot better when this buttons are clicked. A smooth scrolling will make user interesting to navigate between content.
Happy coding ... :)
Certainly there is a rich menu in your application for navigation. May be the menu could be really complex to design and implement with user level restrictions with authentication and authorization to each modules.
What if the navigated page is loo lengthy and need to scroll down to see more content. Or what if your page fetch the content to the bottom each time you scroll down such as listing items? Here is an easy way.
1. Download the VCore Sidebar from here
2. Reference it from your page. (After JQuery reference)
<link href="Responsive/js/Sidebar/vcore.sidebar.css" rel="stylesheet" type="text/css" />
<script src="Responsive/js/Sidebar/vcore.sidebar.js" type="text/javascript"></script>
3. Add a div on top of your page
<div class="divId" style=""></div>
4. And then create the control as below. Add any items as needed.
$(function () {
$('.ipodSideBar').vSidebar({
'items': [
{ link: '#topDivId', 'className': 'fa fa-arrow-circle-up fa-2x set-margin', 'label': 'Top' },
{ link: '#middleDivId', 'className': 'fa fa-street-view fa-2x set-margin', 'label': 'Middle' },
{ link: '#bottomDivId', 'className': 'fa fa-arrow-circle-down fa-2x set-margin', 'label': 'Bottom' }
],
'position': 'right bottom'
});
});
3. Here the fortawesome is used for icons and you can download the fortawesome here.
4. The following css class is added to the vcore.sidebar.css to make the items center.
.set-margin
{
margin-left:auto;
margin-right:auto;
display:block;
text-align:center;
text-decoration:none;
margin-top:3px;
}
5. Finally, you may get a nice set of vertical menu bar like this;
Adding a bit of JQuery for scrolling transitions, you may feel a lot better when this buttons are clicked. A smooth scrolling will make user interesting to navigate between content.
Happy coding ... :)
ASP.Net Ajax for Bootstrap Popup & JQuery
Bootstrap is a css, javascript & html framework that is being used for creating web sites and web applications. We have discussed how bootstrap rows and columns are arranged in our previous post here.
Among 100s of rich contents in bootstrap framework, lets talk about popup today. We normally need to use popups more than web pages in our application to show various information to the user. And it is also convenient way too because we do not need to redirect to many pages instead can include all details in a single page. Using Bootstrap Popup is extremely easy. The basic html way is described below;
The button will open the popup here. but what if you need some control over this which need to query the database and show some info on popup dynamically. It is easy as cheese now. Look at the below.
We set the value of "hdnIdPerson" control when user click on the link and that value is passed to the "LoadPersonDetails" function that will use to query the database and get the details needed.
If you need to change the look and feel, change the css in your custom css file for the below classes;
modal-header : for header
modal-body : for body
modal-footer : for footer.
Finally you will get a popup like this,
Hope you can copy the script and try it. Happy coding... :)
Among 100s of rich contents in bootstrap framework, lets talk about popup today. We normally need to use popups more than web pages in our application to show various information to the user. And it is also convenient way too because we do not need to redirect to many pages instead can include all details in a single page. Using Bootstrap Popup is extremely easy. The basic html way is described below;
<button data-target="#myModal" data-toggle="modal" class="btn btn-primary btn-lg">
Open Popup
</button>
<div aria-hidden="true" aria-labelledby="myModalLabel" role="dialog" tabindex="-1" id="myModal" class="modal fade" style="display: none;">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>
<h4 id="myModalLabel" class="modal-title">Modal title</h4>
</div>
<div class="modal-body">
Model Description
</div>
<div class="modal-footer">
<button data-dismiss="modal" class="btn btn-default" type="button">Close</button>
<button class="btn btn-primary" type="button">Save changes</button>
</div>
</div>
<!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->
</div>
The button will open the popup here. but what if you need some control over this which need to query the database and show some info on popup dynamically. It is easy as cheese now. Look at the below.
$(document).ready(function () {
$("#divVCardLoading").show();
$('[data-toggle=modal]').on('click', function (e) {
var $target = $($(this).data('target'));
$target.modal('show').data('triggered', false);
//YOU CAN LOAD THE AJAX CONTENT HERE
// if ($('#hdnIdPerson').val() != "0")
// LoadPersonDetails();
// else if ($('#hdnIdPerson').val() == "0")
// SetPersonEntryForm();
// else
// $target.data('triggered', true);
$("#divVCardLoading").hide(); //HIDE LOADING
$("#txtPersonName").focus();
// THIS WILL DELAY THE POPUP 100 MILLISECONDS
$target.data('triggered', true);
setTimeout(function () {
if ($target.data('triggered')) {
$target.modal('show').data('triggered', false);
};
}, 100); // milliseconds
return false;
});
});
function LoadPersonDetails() {
//USE JQUERY AJAX TO LOAD DETAILS AND APPEND THEM TO POPUP CONTROLS HERE
};
function SetPersonEntryForm() {
//CLEAR FIELDS AND SET FOCUS
};
We set the value of "hdnIdPerson" control when user click on the link and that value is passed to the "LoadPersonDetails" function that will use to query the database and get the details needed.
If you need to change the look and feel, change the css in your custom css file for the below classes;
modal-header : for header
modal-body : for body
modal-footer : for footer.
Finally you will get a popup like this,
Hope you can copy the script and try it. Happy coding... :)
Saturday, December 5, 2015
A database layer in PHP for MySQL
Well, we discussed a lot of ASP.Net related stories and lets see a little bit about PHP as well. No matter the language, logic is the main concern that we should focus on. If the way we practice is good, better the outcome will be.
We had discussed a DB Layer in C# that we can of course use it for Web or Windows application. And lets think the same way in PHP as well. Basically what we need is to pass some connection parameters and get a resultset from DB or do our CRUD transactions. So, the below code would explain a simple PHP class that we can use to query the DB across the application.
So we can use this class as per below in our data layer.
It is that easy :) so copy this code and try once.
We had discussed a DB Layer in C# that we can of course use it for Web or Windows application. And lets think the same way in PHP as well. Basically what we need is to pass some connection parameters and get a resultset from DB or do our CRUD transactions. So, the below code would explain a simple PHP class that we can use to query the DB across the application.
<?php
class database {
public $affected_rows = 0; // Use after calling query function
public $insert_id = null; // Use after calling an insert query
public $connection; // For use with mysql php functions
public $result; // For use with mysql php functions
private $crlf = "\r\n";
private $as_file = false;
private $tmp_buffer = "";
private $db_host = "Host";
private $db_user = "Username";
private $db_pass = "Password";
private $db_name = "Database";
const GET_ARRAY = 0;
const GET_ROW = 1;
const GET_FIELD = 2;
const VTYPE_BOTH = "BOTH";
const VTYPE_SESSION = "SESSION";
const VTYPE_GLOBAL = "GLOBAL";
function __construct() {
$this->db_name = "Host";
$this->db_user = "User";
$this->db_pass = "Password";
$this->db_name = "Database";
//connect to database
$connection = @mysql_connect("Host", "User", "Password");
if (!$connection) {
$this->show_error('Could not connect to database server.');
}
$this->connection = $connection;
//select database
if($this->db_name != '') {
$db = @mysql_select_db($this->db_name, $connection);
if (!$db) {
$this->show_error('Could not connect to database.');
}
}
}
// -----------------------------------------------
// select_db($db_name)
// -----------------------------------------------
// Description: Allows you to switch databases
//
// Parameters:
// $db_name = Name of the database to connect to
// -----------------------------------------------
public function select_db($db_name) {
$db = @mysql_select_db($db_name, $this->connection);
if(!$db) {
$this->show_error('Could not connect to database.');
return false;
}
return true;
}
// -----------------------------------------------
// disconnect()
// -----------------------------------------------
// Description: Disconnects the connection
//
// Parameters: none
// -----------------------------------------------
public function disconnect() {
return mysql_close($this->connection);
}
// -----------------------------------------------
// query($query, $return_type = self::GET_ARRAY)
// -----------------------------------------------
// Description: Runs a MySQL query
//
// Parameters:
// $query = MySQL query
// $return_type = database::GET_ARRAY (default)
// Returns results to a 2-dimensional array
// database::GET_ROW
// Returns result record to an array
// database::GET_FIELD
// Returns result to a variable
//
// Notes:
// 1. After a query is called if it affected any rows, then you can use
// the included $db->affected_rows variable to get the number of
// rows the query affected. ($db is just an example)
// 2. After calling an INSERT query, you can use $db->insert_id to get
// the value of the ID generated.
// 3. Will store the connection and result resource variables in public
// variables that may be used outside of this class with mysql_
// functions.
// -----------------------------------------------
public function query($query, $return_type = self::GET_ARRAY) {
// Determine query type
$q = trim($query);
$qarr = explode(' ', $q);
// Proceed based on query type
if (strtoupper($qarr[0]) == "SELECT" || strtoupper($qarr[0]) == "SHOW" ||
strtoupper($qarr[0]) == "EXPLAIN" || strtoupper($qarr[0]) == "DESCRIBE" ||
strtoupper($qarr[0]) == "HELP") {
// Send query
$this->result = @mysql_query($query);
if (!$this->result) {
$this->show_error(mysql_error());
@mysql_free_result($this->result);
return false;
} else {
// Output number of affected rows
$this->affected_rows = mysql_affected_rows();
// Get array of field names
$fields = array();
$num_fields = mysql_num_fields($this->result);
for($i=0;$i<$num_fields;$i++) {
$fields[] = mysql_field_name($this->result, $i);
}
// Run through data returned
$n = 0;
$data = array();
while ($row=mysql_fetch_array($this->result, MYSQL_ASSOC)) {
if($return_type == self::GET_ROW) return $row;
foreach($fields as $f) {
$field = trim($f);
if ($return_type == self::GET_FIELD) return $row[$field];
$data[$n] = $row;
}
$n++;
}
}
// Free result
@mysql_free_result($this->result);
// Show error
return $data;
} else {
// Send query
$this->result = @mysql_query($query);
if (!$this->result) {
$this->show_error(mysql_error());
return false;
} else {
// Output number of affected rows
$this->affected_rows = mysql_affected_rows();
}
// If the query was an INSERT query, then store the insert_id
if(strtoupper($qarr[0]) == "INSERT") $this->insert_id = mysql_insert_id();
// Free result
@mysql_free_result($this->result);
// Return result
return true;
}
}
// -----------------------------------------------
// get_table_fields($table_name, $exclude = array())
// -----------------------------------------------
// Description: Returns an array of fields in the chosen table
//
// Parameters:
// $table_name = Name of a table to retrieve the fields from
// $exclude = Array of fields to exclude from results (i.e. id fields)
//
// Notes:
// 1. It will return information about each field including Field, Type,
// Null, Key, Default, and Extra
// -----------------------------------------------
public function get_table_fields($table_name, $exclude = array()) {
$structure = $this->query("SHOW COLUMNS FROM `$table_name`;");
if (is_array($structure)) {
$fields = array();
foreach ($structure as $k => $v) {
if (!in_array($v["Field"], $exclude)) {
$fields[] = $v;
}
}
return $fields;
}
return false;
}
// -----------------------------------------------
// get_tables($exclude = array())
// -----------------------------------------------
// Description: Returns an array of tables in the database
//
// Parameters:
// $exclude = Array of tables to exclude from results
// -----------------------------------------------
public function get_tables($exclude = array()) {
$structure = $this->query("SHOW TABLES FROM `".$this->db_name."`;");
if (is_array($structure)) {
$tables = array();
foreach ($structure as $k => $v) {
if (!in_array($v["Tables_in_".$this->db_name], $exclude)) {
$tables[] = $v["Tables_in_".$this->db_name];
}
}
return $tables;
}
return false;
}
// -----------------------------------------------
// get_databases($exclude = array())
// -----------------------------------------------
// Description: Returns an array of databases excluding system databases
//
// Parameters:
// $exclude = Array of databases to exclude from results
// -----------------------------------------------
public function get_databases($exclude = array()) {
$exc = array('information_schema','mysql');
$exclude_all = array_merge($exclude, $exc);
$structure = $this->query("SHOW DATABASES;");
if (is_array($structure)) {
$databases = array();
foreach ($structure as $k => $v) {
if (!in_array($v["Database"], $exclude_all)) {
$databases[] = $v["Database"];
}
}
return $databases;
}
return false;
}
// -----------------------------------------------
// get_mysql_variables()
// -----------------------------------------------
// Description: Returns an array of the MySQL variables and their values
//
// Parameters:
// $variable_type = They type of variable
// database::VTYPE_BOTH (default)
// database::VTYPE_SESSION
// database::VTYPE_GLOBAL
// -----------------------------------------------
public function get_mysql_variables($variable_type = self::VTYPE_BOTH) {
return $this->query('SHOW ' . ($variable_type != self::BOTH ? $variable_type : '') . ' VARIABLES;');
}
// -----------------------------------------------
// sort_nonindexed_table($table_name, $columns = array())
// -----------------------------------------------
// Description: Sorts a table with no index column by the chosen column.
//
//
// Parameters:
// $table_name = Name of table being sorted
// $columns = Array of columns/fields being sorted by
// Example syntaxes:
// As an array: array('`column_1` ASC','`column_2` DESC')
// As a string: '`column_1` ASC, `column_2` ASC'
//
// Returns:
// Success = true
// Failure = false
//
// Notes:
// The table will not stay sorted after INSERTs and UPDATEs.
// -----------------------------------------------
public function sort_nonindexed_table($table_name, $columns = array()) {
if(is_array($columns)) $c = implode(", ",$columns);
else $c = $columns;
return $this->query("ALTER TABLE `$table_name` ORDER BY $c;");
}
// -----------------------------------------------
// write_backup_sql($generate_file = false, $all_dbs = false)
// -----------------------------------------------
// Description: Write MySQL backup code for your database
//
// Parameters:
// $generate_file = User can opt to have this generate an SQL file.
// If true, then this function must be called before
// anything is output to browser when headers are called.
// $all_dbs = User may also opt to have this function backup all the
// databases on their host. (Better when $generate_file = true)
//
// Returns:
// If $generate_file = true, then result is an SQL file.
// If $generate_file = false, then result is a string.
// -----------------------------------------------
public function write_backup_sql($generate_file = false, $all_dbs = true) {
// If the user wants to generate an SQL file, then convert the Content Type
if ($generate_file) {
$this->as_file = $generate_file;
header('Content-Type: application/octetstream');
header('Content-Disposition: filename="' . $this->db_name . '.sql"');
}
// Write header
$dump_buffer = "# MySQL Database Dump".$this->crlf;
$dump_buffer .= "# Backup made: " . date("F j, Y, g:i a") . $this->crlf;
$dump_buffer .= "# Database: " . ($all_dbs == true ? 'All Databases' : $this->db_name) . $this->crlf;
if(!$all_dbs) $dump_buffer .= "# Backed up tables: " . count($this->get_tables()) . $this->crlf;
else $dump_buffer .= "# Backed up databases: " . count($this->get_databases()) . $this->crlf;
$dump_buffer .= "# Generated by: class_database by Matt DeKok" .$this->crlf;
if($all_dbs) {
// Retrieve SQL dump for each database
$db_name = $this->db_name;
$dbs = $this->get_databases();
foreach($dbs as $db) {
$this->db_name = $db;
$dump_buffer .= $this->generate_sql($generate_file, $db);
}
$this->db_name = $db_name;
} else {
// Retrieve SQL dump for each table
$dump_buffer .= $this->generate_sql($generate_file, $this->db_name);
}
// Output SQL dump
if ($generate_file) {
echo $dump_buffer;
exit;
} else {
return $dump_buffer;
}
}
// -----------------------------------------------
// execute_file($file)
// -----------------------------------------------
// Description: Execute the MySQL commands inside a file
//
// Parameters:
// $file = The path of the file
//
// Returns:
// On success = true
// On failure = false
// -----------------------------------------------
public function execute_file($file) {
if(!file_exists($file)) {
$this->show_error("File '$file' does not exist.");
return false;
}
$content = file_get_contents($file);
if(!$content) {
$this->show_error("Unable to read '$file'.");
return false;
}
$sql = explode(";",$content);
foreach($sql as $command) {
if(!empty($command)) {
if(!$this->query($command)) return false;
}
}
return true;
}
// -----------------------------------------------
// sql_date_format($date)
// -----------------------------------------------
// Description: Converts a timestamp or string format date
// to MySQL format so it can be inserted into
// a table
//
// Parameters:
// $date = A numeric timestamp or a string date
// -----------------------------------------------
public function sql_date_format($date) {
if(gettype($date) == 'string') $value = strtotime($date);
return date('Y-m-d H:i:s', $value);
}
// -----------------------------------------------
// print_table($results,
// $row_color = null,
// $alternate_row_color = null,
// $row_style = null,
// $table_style = null,
// $header_row_style = null)
// -----------------------------------------------
// Description: Prints a table from a query.
//
// Parameters:
// $results = The associative array returned from the query function
// $row_color = The primary row color
// $alternate_row_color = The alternate row color
// $row_style = The style of every row
// $table_style = The table style
// $header_row_style = The header row style
// -----------------------------------------------
public function print_table($results, $row_color = null, $alternate_row_color = null, $row_style = null, $table_style = null, $header_row_style = null) {
if(!is_array($results)) return false;
if($row_color == null) $row_color = "#fff";
if($alternate_row_color == null) $alternate_row_color = "#ddd";
if($row_style == null) $row_style = "vertical-align: top;";
if($table_style == null) $table_style = "border: 2px solid #888; border-collapse: collapse;";
if($header_row_style == null) $header_row_style = "text-align: left; background-color: #ccc; border-bottom: 2px solid #888; color: #000;";
$headers = array_keys($results[0]);
echo "<div style=\"overflow: auto; overflow-y: hidden;\">\n";
echo "<table style=\"$table_style\">\n";
echo " <tr>\n";
foreach($headers as $header) {
echo " <th style=\"$header_row_style\">$header</th>\n";
}
echo " </tr>\n";
$i = 0;
foreach($results as $result) {
$i++;
if(floor($i / 2) == $i / 2) $style_row = "background-color: $row_color; $row_style";
else $style_row = "background-color: $alternate_row_color; $row_style";
echo " <tr style=\"$style_row\">\n";
foreach($headers as $header) {
$item = $result[$header];
echo " <td>$item</td>\n";
}
echo " </tr>\n";
}
echo "</table>\n";
echo "</div>\n";
}
// -----------------------------------------------
// prevent_injection($value)
// -----------------------------------------------
// Description: If you use this on all your values for UPDATE
// and INSERT queries.
//
// Note: It will be better to let this add the quotes to your
// strings for you.
// -----------------------------------------------
public function prevent_injection($value) {
// Stripslashes
if (get_magic_quotes_gpc()) {
$value = stripslashes($value);
}
// Quote if not a number or a numeric string
if (!is_numeric($value)) {
$value = "'" . mysql_real_escape_string($value) . "'";
}
return $value;
}
// -----------------------------------------------
// Pay no mind to the following functions
// -----------------------------------------------
private function show_error($error) {
//echo "<p><strong>Error:</strong> " . $error . "</p>";
header('../index.php');
}
private function get_table_def($table) {
$this->error = array();
$schema_create = "DROP TABLE IF EXISTS `" . $this->db_name . "`.`$table`;".$this->crlf;
$db = $table;
$structure = $this->query("SHOW CREATE TABLE `$table`;");
$schema_create = $structure[0]['Create Table'];
$schema_create = str_replace("`$table`","`" . $this->db_name . "`.`$table`",$schema_create);
if (get_magic_quotes_gpc()) {
return (stripslashes($schema_create));
} else {
return ($schema_create);
}
}
private function get_table_content($table, $limit_from = 0, $limit_to = 0) {
// Defines the offsets to use
if ($limit_from > 0) {
$limit_from--;
} else {
$limit_from = 0;
}
if ($limit_to > 0 && $limit_from >= 0) {
$add_query = " LIMIT $limit_from, $limit_to";
} else {
$add_query = '';
}
$result = mysql_query('SELECT * FROM ' . $this->db_name . '.' . $table . $add_query);
if (!$result) {
$this->show_error(mysql_error());
return false;
} else {
@set_time_limit(1200); // 20 Minutes
// Checks whether the field is an integer or not
for ($j = 0; $j < mysql_num_fields($result); $j++) {
$field_set[$j] = mysql_field_name($result, $j);
$type[$j] = mysql_field_type($result, $j);
$primary[$j] = substr_count(mysql_field_flags($result, $j),"primary_key");
$auto[$j] = substr_count(mysql_field_flags($result, $j),"auto_increment");
if ($type[$j] == 'tinyint' || $type[$j] == 'smallint' || $type[$j] == 'mediumint' || $type[$j] == 'int' || $type[$j] == 'bigint' || $type[$j] == 'timestamp') {
$field_num[$j] = true;
} else {
$field_num[$j] = false;
}
} // end for
// Get the scheme
if (isset($GLOBALS['showcolumns'])) {
$fields = implode('`, `', $field_set);
$schema_insert = "INSERT INTO `" . $this->db_name . "`.`$table` (`$fields`) VALUES (";
} else {
$schema_insert = "INSERT INTO `" . $this->db_name . "`.`$table` VALUES (";
}
$field_count = mysql_num_fields($result);
$search = array("\x0a", "\x0d", "\x1a", "'"); //\x08\\x09, not required
$replace = array("\\n", "\\r", "\Z", "\\'");
while ($row = mysql_fetch_row($result)) {
for ($j = 0; $j < $field_count; $j++) {
if (!isset($row[$j]) || ($primary[$j] && $auto[$j])) {
$values[] = 'NULL';
} elseif (!empty($row[$j])) {
// a number
if ($field_num[$j] && is_numeric($row[$j])) {
$values[] = $row[$j];
}
// a string
else {
$values[] = "'" . str_replace($search, $replace, $row[$j]) . "'";
}
} else {
$values[] = "''";
} // end if
} // end for
$insert_line = $schema_insert . implode(', ', $values) . ')';
unset($values);
// Call the handler
$this->handler($insert_line);
} // end while
}
return true;
}
private function handler($sql_insert) {
if (!$this->as_file)
$this->tmp_buffer .= htmlspecialchars("$sql_insert;" . $this->crlf);
else
$this->tmp_buffer .= "$sql_insert;" . $this->crlf;
}
private function faqe_db_error() {
return mysql_error();
}
private function faqe_db_insert_id($result) {
return mysql_insert_id($result);
}
private function generate_sql($generate_file, $db_name) {
$dump_buffer = "";
$dump_buffer .= $this->crlf."# --------------------------------------------------------".$this->crlf.$this->crlf;
$dump_buffer .= "#".$this->crlf;
$dump_buffer .= "# Database structure for database '$db_name'".$this->crlf;
$dump_buffer .= "#".$this->crlf;
$dump_buffer .= "DROP DATABASE IF EXISTS `$db_name`;".$this->crlf;
$cdb = $this->query("SHOW CREATE DATABASE `$db_name`;");
$dump_buffer .= str_replace(" /*!40100 DEFAULT CHARACTER SET latin1 */",";",$cdb[0]["Create Database"]).$this->crlf;
$dump_buffer .= $this->crlf;
$tables = $this->query("SHOW TABLES FROM `" . $db_name . "`;");
$num_tables = $this->affected_rows;
// Check if tables were found
if ($num_tables == 0) {
if ($generate_file) {
echo "# 0 tables found";
} else {
return false;
}
}
foreach($tables as $table) {
$table = $table["Tables_in_$db_name"];
// Write table structure
$dump_buffer .= "# --------------------------------------------------------".$this->crlf;
$dump_buffer .= $this->crlf."#".$this->crlf;
$dump_buffer .= "# Table structure for table '$table'".$this->crlf;
$dump_buffer .= "#".$this->crlf;
$dump_buffer .= $this->get_table_def($table) . ";".$this->crlf;
// Write table data
$dump_buffer .= $this->crlf."#".$this->crlf;
$dump_buffer .= "# Dumping data for table '$table'".$this->crlf;
$dump_buffer .= "#".$this->crlf;
$this->tmp_buffer = "";
$this->get_table_content($table, 0, 0);
$dump_buffer .= $this->tmp_buffer;
$dump_buffer .= $this->crlf;
}
return $dump_buffer;
}
private function get_column_type($table_name, $column_name) {
$result = $this->query("SELECT $column_name FROM $table_name;");
if(!$result) {
return false;
}
$type = mysql_field_type($result, 0);
if(!$type) {
$this->show_error(mysql_error());
$return = false;
}
@mysql_free_result($result);
return $type;
}
}
?>
So we can use this class as per below in our data layer.
$sql = "SELECT id,name from tabel_name";
$arrSelect = $db->query($sql);
for ($i = 0; $i < count($arrSelect); $i++)
{
$id = $arrSelect[$i]['id'];
$name = $arrSelect[$i]['name'];
}
It is that easy :) so copy this code and try once.
Friday, December 4, 2015
Creating Data Reader Extensions in C# for Nullable values
When we use data readers in our data access layers of a c# application that could be a part of web or windows, we have to handle nullable parameters and nullable values before casting or accessing to avoid any run time errors or basically to avoid crashing the application. So we can write additional reader extension to use that can handle null values for a nullable property. For example see the below method.
Many data types are covered with this method and this could be copied to your base class of data access layer which then be accessible as a static method to any data layer methods. Look at the below example.
This is a peace of code of a data access method that the reader is a SqlDataReader and the string value of lbActionType can also be nullable. So directly using GetString may throw null reference if you don't handle it in reader object. So the "GetStringNullable" will save lot of your time and additional coding.
Get this code and try it yourself. It is easy as say cheese. :)\
Remember the saying,
“I choose a lazy person to do a hard job. Because a lazy person will find an easy way to do it.” ― Bill Gates
It is not being lazy sometimes, it is the way of finding alternatives. :)
public static class DataReaderExtensions
{
public static string GetStringNullable(this IDataReader reader, int ordinal)
{
return reader.IsDBNull(ordinal) ? null : reader.GetString(ordinal);
}
public static int? GetInt32Nullable(this IDataReader reader, int ordinal)
{
return reader.IsDBNull(ordinal) ? (int?)null : reader.GetInt32(ordinal);
}
public static DateTime? GetDateTimeNullable(this IDataReader reader, int ordinal)
{
return reader.IsDBNull(ordinal) ? (DateTime?)null : reader.GetDateTime(ordinal);
}
public static decimal? GetDecimalNullable(this IDataReader reader, int ordinal)
{
return reader.IsDBNull(ordinal) ? (decimal?)null : reader.GetDecimal(ordinal);
}
public static bool? GetBooleanNullable(this IDataReader reader, int ordinal)
{
return reader.IsDBNull(ordinal) ? (bool?)null : reader.GetBoolean(ordinal);
}
public static byte? GetByteNullable(this IDataReader reader, int ordinal)
{
return reader.IsDBNull(ordinal) ? (byte?)null : reader.GetByte(ordinal);
}
public static double? GetDoubleNullable(this IDataReader reader, int ordinal)
{
return reader.IsDBNull(ordinal) ? (double?)null : reader.GetDouble(ordinal);
}
}
Many data types are covered with this method and this could be copied to your base class of data access layer which then be accessible as a static method to any data layer methods. Look at the below example.
while (reader.Read())
{
AdmActionEntity action = new AdmActionEntity();
action.idActionType = action.ActionType.idActionType = reader.GetInt32(idActionTypeOrdinal);
action.ActionType.lbActionType = reader.GetStringNullable(lbActionTypeOrdinal);
action.nbToBeAssigned = reader.GetInt32(nbToBeAssignedOrdinal);
action.bnToBeDone = reader.GetInt32(bnToBeDoneOrdinal);
ActionList.Add(action);
}
This is a peace of code of a data access method that the reader is a SqlDataReader and the string value of lbActionType can also be nullable. So directly using GetString may throw null reference if you don't handle it in reader object. So the "GetStringNullable" will save lot of your time and additional coding.
Get this code and try it yourself. It is easy as say cheese. :)\
Remember the saying,
“I choose a lazy person to do a hard job. Because a lazy person will find an easy way to do it.” ― Bill Gates
It is not being lazy sometimes, it is the way of finding alternatives. :)
Encryption and Decryption (Cryptography) in C#
Application level encryption and decryption is needed for certain cases when security is applied. It can simply be user credentials or even may be user specific data. Anyway, it is a good way or using a unique encryption and decryption method across the application.
Cryptography is widely used by almost all the developers and it is not that hard to have a common EncryptionUtility for your own application. Look at the sample class below;
Because the methods are defined as static, you can use it as below;
Copy this code and try once. :)
Cryptography is widely used by almost all the developers and it is not that hard to have a common EncryptionUtility for your own application. Look at the sample class below;
public class EncryptionUtility
{
static readonly string PasswordHash = "Z!R3cVwPa_b7^5TZ!_rE";
static readonly string SaltKey = "&7fHXcc2^$8x@AwrdC$c";
static readonly string VIKey = "6=ZvwA##4Ms3*yV2D&6$";
//Encryption method
public static string Encrypt(string plainText)
{
try
{
byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
byte[] keyBytes = new Rfc2898DeriveBytes(PasswordHash, Encoding.ASCII.GetBytes(SaltKey)).GetBytes(256 / 8);
var symmetricKey = new RijndaelManaged() { Mode = CipherMode.CBC, Padding = PaddingMode.Zeros };
var encryptor = symmetricKey.CreateEncryptor(keyBytes, Encoding.ASCII.GetBytes(VIKey));
byte[] cipherTextBytes;
using (var memoryStream = new MemoryStream())
{
using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
{
cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
cryptoStream.FlushFinalBlock();
cipherTextBytes = memoryStream.ToArray();
cryptoStream.Close();
}
memoryStream.Close();
}
return Convert.ToBase64String(cipherTextBytes);
}
catch (Exception)
{
//You can use your own error handling method. In this case, the text is returned.
return plainText;
}
}
//Decryption method
public static string Decrypt(string encryptedText)
{
try
{
byte[] cipherTextBytes = Convert.FromBase64String(encryptedText);
byte[] keyBytes = new Rfc2898DeriveBytes(PasswordHash, Encoding.ASCII.GetBytes(SaltKey)).GetBytes(256 / 8);
var symmetricKey = new RijndaelManaged() { Mode = CipherMode.CBC, Padding = PaddingMode.None };
var decryptor = symmetricKey.CreateDecryptor(keyBytes, Encoding.ASCII.GetBytes(VIKey));
var memoryStream = new MemoryStream(cipherTextBytes);
var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
byte[] plainTextBytes = new byte[cipherTextBytes.Length];
int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
memoryStream.Close();
cryptoStream.Close();
return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount).TrimEnd("\0".ToCharArray());
}
catch (Exception)
{
return encryptedText;
}
}
//Method to see a given text is encrypted or not.
public static bool IsEncrypted(string text)
{
if (text.Equals(Decrypt(text)))
return false;
else
return true;
}
}
Because the methods are defined as static, you can use it as below;
EncryptionUtility.Encrypt(username)
Copy this code and try once. :)
Thursday, December 3, 2015
Email Helper in C# - A Common & Easy Way of Sending Emails
Most of the time, it is common to send emails through the application which some may related to technical or error handling and sometimes to customers which could be even internal or external. Such cases, SmtpClient is the class we use in ASP.Net
Using a bit of OOP can help to have a common way of sending emails and can be widely used across the application. Below is an example of an Email helper class which can be used widely.
public class EmailHelper
{
private MailMessage Message = null;
private SmtpClient smtpClient = null;
public MailAddress FromAddress { get; set; }
public string Subject { get; set; }
public string Body { get; set; }
public EmailHelper()
{
smtpClient = new SmtpClient();
smtpClient.Host = ConfigurationManager.AppSettings["smtpHost"]; //Configure as your email provider
smtpClient.UseDefaultCredentials = false;
//smtpClient.EnableSsl = true;//comment if you don't need SSL
smtpClient.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["userName"], ConfigurationManager.AppSettings["password"]);
smtpClient.Port = Convert.ToInt32(ConfigurationManager.AppSettings["port"]);
Message = new MailMessage();
}
public EmailHelper(string host, int port, string userName, string password, bool ssl)
: this()
{
smtpClient.Host = host;
smtpClient.Port = port;
smtpClient.EnableSsl = ssl;
smtpClient.Credentials = new NetworkCredential(userName, password);
}
public void AddToAddress(string email, string name = null)
{
if (!string.IsNullOrEmpty(email))
{
email = email.Replace(",", ";");
string[] emailList = email.Split(';');
for (int i = 0; i < emailList.Length; i++)
{
if (!string.IsNullOrEmpty(emailList[i]))
Message.To.Add(new MailAddress(emailList[i], name));
}
}
}
public void AddCcAddress(string email, string name = null)
{
if (!string.IsNullOrEmpty(email))
{
email = email.Replace(",", ";");
string[] emailList = email.Split(';');
for (int i = 0; i < emailList.Length; i++)
{
if (!string.IsNullOrEmpty(emailList[i]))
Message.CC.Add(new MailAddress(emailList[i], name));
}
}
}
public void AddBccAddress(string email, string name = null)
{
if (!string.IsNullOrEmpty(email))
{
email = email.Replace(",", ";");
string[] emailList = email.Split(';');
for (int i = 0; i < emailList.Length; i++)
{
if (!string.IsNullOrEmpty(emailList[i]))
Message.Bcc.Add(new MailAddress(emailList[i], name));
}
}
}
public void AddAttachment(string file, string mimeType)
{
Attachment attachment = new Attachment(file, mimeType);
Message.Attachments.Add(attachment);
}
public void AddAttachment(Attachment objAttachment)
{
Message.Attachments.Add(objAttachment);
}
public void SendMail()
{
if (FromAddress == null || (FromAddress != null && FromAddress.Address.Equals("")))
{
throw new Exception("From address not defined");
}
else
{
if (string.IsNullOrEmpty(FromAddress.DisplayName))
FromAddress = new MailAddress(FromAddress.Address, string.Empty);
Message.From = FromAddress;
}
if (Message.To.Count <= 0)
{ throw new Exception("To address not defined"); }
Message.Subject = Subject;
Message.IsBodyHtml = true;
Message.Body = Body;
smtpClient.Send(Message);
}
public static string GetFileMimeType(string fileName)
{
string fileExt = Path.GetExtension(fileName.ToLower());
string mimeType = string.Empty;
switch (fileExt)
{
case ".htm":
case ".html":
mimeType = "text/html";
break;
case ".xml":
mimeType = "text/xml";
break;
case ".jpg":
case ".jpeg":
mimeType = "image/jpeg";
break;
case ".gif":
mimeType = "image/gif";
break;
case ".png":
mimeType = "image/png";
break;
case ".bmp":
mimeType = "image/bmp";
break;
case ".pdf":
mimeType = "application/pdf";
break;
case ".doc":
mimeType = "application/msword";
break;
case ".docx":
mimeType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
break;
case ".xls":
mimeType = "application/x-msexcel";
break;
case ".xlsx":
mimeType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
break;
case ".csv":
mimeType = "application/csv";
break;
case ".ppt":
mimeType = "application/vnd.ms-powerpoint";
break;
case ".pptx":
mimeType = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
break;
case ".rar":
mimeType = "application/x-rar-compressed";
break;
case ".zip":
mimeType = "application/x-zip-compressed";
break;
default:
mimeType = "text/plain";
break;
}
return mimeType;
}
}
}
This class can be used as follows,
Copy this code and paste it in your project. Remember to apply the correct SMTPClient settings as per your email provider.
Happy coding.. :)
StringBuilder fileNames = new StringBuilder();
string strPath = string.Empty;
string coType = IPODMT.common.IQuestConst.MessageStatus.Sent;
string url = string.Empty;
EmailHelper emailHelper = new EmailHelper();
emailHelper.FromAddress = new System.Net.Mail.MailAddress(hdnFromAddress.Value.Trim());
emailHelper.AddToAddress(txtEmailTo.Text.Trim());
emailHelper.AddCcAddress(txtEmailCc.Text.Trim());
emailHelper.AddBccAddress(txtEmailBcc.Text.Trim());
Dictionary<int, KeyValuePair<string, string>> fileList = null;
//Attaching files. Here I keep the values in a session
fileList = (Dictionary<int, KeyValuePair<string, string>>)Session["UploadedFileList"];
foreach (var item in fileList)
{
emailHelper.AddAttachment(item.Value.Key + item.Value.Value, EmailHelper.GetFileMimeType(item.Value.Value));
FileInfo file = new FileInfo(item.Value.Key + item.Value.Value);
fileNames.Append(item.Value.Value + "|" + file.Length.ToString() + ";");
}
string strAttachment = hdnAttachmentTemplate.Value;
string msgBody = string.Empty;
try
{
emailHelper.Subject = txtEmailSubject.Text.Trim();
if (!string.IsNullOrEmpty(strAttachment) && fileList != null && fileList.Count > 0)
{
strAttachment = strAttachment.Replace("[flAttachment]", fileList.Count.ToString());
msgBody = txtEmailBody.InnerText.Trim() + "<br/>" + strAttachment;
}
else
msgBody = txtEmailBody.InnerText.Trim();// +"<br/>" + txtCustomerInfo.Text.Trim();
msgBody = msgBody.Replace("[lbCustomerInfo]", txtCustomerInfo.Text.Trim());
emailHelper.Body = msgBody;
emailHelper.SendMail(); //After sending email you can clear the session "UploadedFileList"
}
catch (Exception)
{
//Handle your errors. May be a server log file? Its up to you!
}
Copy this code and paste it in your project. Remember to apply the correct SMTPClient settings as per your email provider.
Happy coding.. :)
Wednesday, December 2, 2015
FormsAuthentication in ASP.Net
First, you can read and find rich set of contents here. And going further, it is not that complex to implement and use FormsAuthentication in ASP.Net. For instance, look at the below login method which the FormsAuthentication is used if the user clicked on the "Remember Me" function.
The context is an object containing user details such as usernam, id and many more. The encryption utility is a global method that is used to encrypt any given string. We will discuss the method in detail in the next post.
Lets see how the above "RememberAuthentication" method can be used in login implementation.
The remember me option will make the user to be remembered if he comes back to the site after logged in. To do so, we need to validate the users identity at the page load event.
Remember to validate Home page with the user logged in and you need to provide authorization as well.
public void RememberAuthentication(ContextUtility context,string username)
{
Response.Cookies.Clear();
FormsAuthentication.Initialize();
FormsAuthentication.SetAuthCookie(EncryptionUtility.Encrypt(username), false);
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
1, // ticket version
EncryptionUtility.Encrypt(username), // authenticated username
DateTime.Now, // issued date
DateTime.Now.AddDays(7), // expiry date
true, // true to persist across browser sessions
EncryptionUtility.Encrypt(context.GetValueAsInt("idPerson").ToString()), // can be used to store additional user data
FormsAuthentication.FormsCookiePath); // the path for the cookie
// Encrypt the ticket using the machine key
string encryptedTicket = FormsAuthentication.Encrypt(ticket);
// Add the cookie to the request to save it
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
cookie.HttpOnly = true;
Response.Cookies.Add(cookie);
}
The context is an object containing user details such as usernam, id and many more. The encryption utility is a global method that is used to encrypt any given string. We will discuss the method in detail in the next post.
Lets see how the above "RememberAuthentication" method can be used in login implementation.
private void Login()
{
ContextUtility context = new AdmLoginBLL().Login(txtUsername.Text.Trim(), txtPassword.Text.Trim(), ContextEntity);
if (cbRememberMe.Checked)
RememberAuthentication(context, txtUsername.Text.Trim());
if (context.GetValueAsInt("idPerson") > 0)
Response.Redirect("Home.aspx", false);
}
The remember me option will make the user to be remembered if he comes back to the site after logged in. To do so, we need to validate the users identity at the page load event.
if (Context.User.Identity.IsAuthenticated)
{
Response.Redirect("Home.aspx");
}
Remember to validate Home page with the user logged in and you need to provide authorization as well.
Convert C# DateTime type to JavaScript Date property
When a web method or service serialize an object and pass it to client side with DateTime properties, We need to cast the C# DateTime to JavaScript Date format so that we can treat it as a valid date property. The below method can be used to cast the c# datetime property in to javascript date property.
2. Web Method
1. The entity class
public class icqQuestResourcePrior
{
public int idQuestPrior { get; set; }
public int idResource { get; set; }
public int sqOrder { get; set; }
public bool flDevFinish { get; set; }
public int idQuest { get; set; }
public DateTime dtBegin { get; set; }
public DateTime? dtEnd { get; set; }
public string lbQuest { get; set; }
public string lbPersonInitials { get; set; }
public int nvWeekNumber { get; set; }
public string coType { get; set; }
public string coLifeCycle { get; set; }
public string lbLateColor { get; set; }
public string lbPersonName { get; set; }
public int idPerson { get; set; }
}
2. Web Method
[WebMethod]
public string icqQuestResourcePriorGet(int idQuest, int? idPerson, int? idResource, int? idQuestPrior)
{
return new JavaScriptSerializer().Serialize(new icqHome().icqQuestResourcePriorGet(idQuest, idPerson == 0 ? (int?)null : idPerson, idResource == 0 ? (int?)null : idResource, idQuestPrior == 0 ? (int?)null : idQuestPrior));
}
3. JQuery AJAX Call
Copy the code and try yourself.
function LoadWRDetails() {
var objPara = {
idQuest: $("#hdnWRIdQuest").val(),
idPerson: 0,
idResource: 0,
idQuestPrior: 0
};
$.ajax({
type: "POST",
url: $('#hdnUrlPrefix').val() + "IPODCommonWS.asmx/icqQuestResourcePriorGet",
data: JSON.stringify(objPara),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
var ret = JSON.parse(msg.d);
var re = /-?\d+/;
var StartDate = re.exec(ret.dtBegin); // returns "/Date(1245398693390)/";
var txtWRStartDate = new Date(parseInt(StartDate[0]));
var EndDate = re.exec(ret.dtEnd); // returns "/Date(1245398693390)/";
var txtWREndDate = new Date(parseInt(EndDate[0]));
var dtBegin = (txtWRStartDate.getUTCDate()) + "-" + (txtWRStartDate.getMonth() + 1) + "-" + txtWRStartDate.getFullYear();
var dtEnd = (txtWREndDate.getUTCDate()) + "-" + (txtWREndDate.getMonth() + 1) + "-" + txtWREndDate.getFullYear();
$('#txtWRIdQuest').val(ret.idQuest);
$('#txtWRTitle').val(ret.lbQuest);
$('#txtWRResource').val(ret.lbPersonName);
$('#txtWRStartDate').val(dtBegin);
$('#txtWREndDate').val(dtEnd);
$('#txtWROrder').val(ret.sqOrder);
$('#hdnWRIdQuest').val("0");
$('#txtWREndDate').datepicker('destroy');
$("#txtWREndDate").datepicker(
{
dateFormat: 'dd-mm-yy',
minDate: $('#txtWRStartDate').val()
});
},
error: function (err) {
alert(err.responseText);
}
});
}
Copy the code and try yourself.
Tuesday, December 1, 2015
Web API in ASP.Net Web Forms - Simple Way
ASP.NET Web API is a framework that makes it easy to build HTTP services that reach a broad range of clients, including browsers and mobile devices. ASP.NET Web API is an ideal platform for building RESTful applications on the .NET Framework.
So this is the basic definition of Web API and lets see how we can use it in an ASP.Net Web Forms application. You need to have Visual Studio 2013 or 2015 which may be the community edition.
1. Right click on your project and Add "Web API Controller class" which you can find under
Web > Web API
2. Add a newer folder for API Classes & You may also have a separate project too. Here I have added the classes in to a Folder.
4. Now you can write a JQuery AJAX method to call the API Method.
Remember to use valid error messages for null returns.
Happy coding.. :)
So this is the basic definition of Web API and lets see how we can use it in an ASP.Net Web Forms application. You need to have Visual Studio 2013 or 2015 which may be the community edition.
1. Right click on your project and Add "Web API Controller class" which you can find under
Web > Web API
2. Add a newer folder for API Classes & You may also have a separate project too. Here I have added the classes in to a Folder.
3. You can write API Methods in this controller (Ex: SignOut)
Remember to use valid error messages for null returns.
Happy coding.. :)
Friday, November 27, 2015
C# - DataTable to Entity List
When accessing data in C#, the generally accepted way is to use List & Generics. There are many advantages of those such as performance and rich with features such as LINQ to Entities. What if we need a DataTable to be converted into a Entity List? There are easiest ways as discussed here but the following methods will be able to use for easier conversion of DataTables in to relevant Entity List.
These two methods will convert the values in the data table to lists in a way as it used below;
The list can be passed as a return value of the method if needed.
Note that this is only to convert DataTable to a list and if you write a data access method, always try to use DataReader instead of DataTables.
Copy these codes and modify, reuse as needed. :)
public static T? GetValue<T>(this DataRow row, string columnName) where T : struct
{
if (!row.Table.Columns.Contains(columnName))
return null;
if (row.IsNull(columnName))
return null;
return row[columnName] as T?;
} //HANDLE NULLABLE VALUES TOO
public static string GetText(this DataRow row, string columnName)
{
if (!row.Table.Columns.Contains(columnName))
return string.Empty;
if (row.IsNull(columnName))
return string.Empty;
return row[columnName] as string ?? string.Empty;
} //FOR STRINGS
These two methods will convert the values in the data table to lists in a way as it used below;
foreach (var item in dataTable.Select())
{
entityList.Add(new myEntity()
{
id = ExtensionMethods.GetValue<int>(item, "idColumnName").Value,
dateTime = ExtensionMethods.GetValue<DateTime>(item, "dateTimeColumnName").Value,
dateTimeNullable = ExtensionMethods.GetValue<DateTime>(item, "dateTimeNullableColumnName"),
string = ExtensionMethods.GetText(item, "stringColumnName"),
number = ExtensionMethods.GetValue<int>(item, "numberColumnName").Value,
numberNullable = ExtensionMethods.GetValue<int>(item, "numberNullableColumnName")
});
}
The list can be passed as a return value of the method if needed.
Note that this is only to convert DataTable to a list and if you write a data access method, always try to use DataReader instead of DataTables.
Copy these codes and modify, reuse as needed. :)
Wednesday, November 25, 2015
tablesorter - A sort-able HTML table with JQuery plugin for ASP.Net GridView Control
When displaying information, such as a table in HTML, it is always better if an additional features can be provided in a way that the users can filter on client side without posting back. The benefits is that its faster as it processes on client side and it has rich contents which is able to filter as well as sort columns.
The following example will show how it can be applied to ASP.Net GridView Control.
1. Create your own GridView with the following properties.
The grdMyGrid_DataBound is important to set the header properties for the grid (table after rendered).
2. In code behind, add the following,
3. Now you need to refer the tablesorter js and css files to the page.
4. Now the table sorter can applied as per below from the relevant page related JS file.
5. The final result looks like below;
Read the documentation here:
http://tablesorter.com/docs/
Happy coding :)
The following example will show how it can be applied to ASP.Net GridView Control.
1. Create your own GridView with the following properties.
ClientIDMode="Static" OnDataBound="grdMyGrid_DataBound"
This declaration must be needed to keep the id of the grid to be constant after rendered especially if there are any master pages.The grdMyGrid_DataBound is important to set the header properties for the grid (table after rendered).
2. In code behind, add the following,
protected void grdMyGrid_DataBound(object sender, EventArgs e)
{
if (grdMyGrid.Rows.Count > 0)
{
grdMyGrid.UseAccessibleHeader = true;
grdMyGrid.HeaderRow.TableSection = TableRowSection.TableHeader;
}
}
3. Now you need to refer the tablesorter js and css files to the page.
<link rel="stylesheet" href="style/TableSorter/theme.blue.css" type="text/css" />
<script src="script/TableSort/jquery.tablesorter.min.js" type="text/javascript"></script>
<script src=">script/TableSort/jquery.tablesorter.widgets.js" type="text/javascript"></script>
* Also make sure the to add references to JQuery too.4. Now the table sorter can applied as per below from the relevant page related JS file.
$(document).ready(function () {
var $table = $('#grdMyGrid').tablesorter({
theme: 'blue', //The theme to be applied
widgets: ["zebra", "filter"],
widgetOptions: {
filter_hideFilters: false,
filter_external: '.search',
filter_placeholder: { search: 'Search...' }, //The Search field mask text
filter_saveFilters: false,
filter_reset: '.reset'
},
headers: {
5: { sorter: false, filter: false }, //Disable sorting and filtering
6: { sorter: false, filter: false } //Disable sorting and filtering
}
});
});
5. The final result looks like below;
Read the documentation here:
http://tablesorter.com/docs/
Happy coding :)
Subscribe to:
Posts (Atom)