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.. :)
Subscribe to:
Posts (Atom)