Thursday, November 6, 2008

ASP.NET Client Side State Management

 

http://dotnet.dzone.com/news/aspnet-server-side-state-manag

How to launch Window application from C# console application

This article is targeted for mid-level programmers who have already worked on C# language. This article uses batch files for demonstration and the reader should have preliminary knowledge of batch files.

Sample application describes how to call any Windows application or batch file using a C# console application. This application launches a new process and executes Windows applications in that process.

API Used

  • System.Console.WriteLine()
  • System.Diagnostics.Process
  • Process.Start()

 

Step by Step process to create a sample application

1.       Create a Console project called as InvokeBatchFile.

2.      Include the following namespce

                     Using System.Diagnostics;

3.      Add following code for the Main function

        Process p = null;

        try

        {

            string targetDir;

            targetDir = string.Format(@"C:\ CallBatchFile ");

            p = new Process();

            p.StartInfo.WorkingDirectory = targetDir;

            p.StartInfo.FileName = "MyBatch.bat";

p.StartInfo.Arguments = string.Format("C-Sharp Console application");

            p.StartInfo.CreateNoWindow = false;

            p.Start();

            p.WaitForExit();

         }

         catch (Exception ex)

         {

                Console.WriteLine("Exception Occurred :{0},{1}",

                          ex.Message, ex.StackTrace.ToString());

         }

 

 

               


C# Class Generator

 

Friday, August 29, 2008

Undocumented SQL Server 2000 functions

Encryption in SQL server [pwdencrypt,pwdcompare]

There are some hidden functions in SQL server through which we can encrypt any string and store the same in the table. This will be very helpful in encrypting the user password and other sensitive user data. Encryption supported by SQL server is one way hash. One way hash is nothing but the string encrypted cannot be decrypted. The only way is to compare values with encrypted string.

DECLARE @ClearPIN varchar (255)
DECLARE @EncryptedPIN varbinary(255)
SELECT @ClearPIN = 'test'
SELECT @EncryptedPIN = CONVERT (varbinary (255), pwdencrypt (@ClearPIN))
SELECT pwdcompare(@ClearPIN, @EncryptedPIN, 0)

In the above example @EncryptedPIN will store the cipher Text. The data in this string is not the encrypted string instead it will return the hash code of the supplied plain string.

Monday, June 2, 2008

Now let's get into details. First of all say in our aspx page we need to save the image to the server at the runtime. For this we need to use the stream class to read the file as a byte and then save it to the required path at a particular event. The event can vary from situation to situation. e.g in my project I did this in the page_load event. Some would definitely like to avail this just before producing the report.

Code for storing your Image in to the server

Listing 1

System.Drawing.Image drawingImage;

drawingImage = System.Drawing.Image.FromStream(new System.IO.MemoryStream (byteArr));

string strPath;

strPath = System.Web.HttpContext.Current.Request.MapPath("path");

drawingImage.Save(strPath, System.Drawing.Imaging.ImageFormat.Bmp);

After saving the image now its turn for designing the report. For this we need a dataset(xsd) which will store the Image.

For this add a dataset to your project. Create a datatable inside your dataset and add a column to the datatable for storing the image.One thing to note here is the datatype for the column. It should be System.Byte[].

Figure 1

After Creating the DataSet we can start on designing the report. Open the Crystal report, add the DataTable to the database fields of your report and then drag and drop the image field to the report.

Now for populating the datatable with the data at the runtime we will have a function inside a class that takes one paramater i.e the Image that needs to be displayed and using the data row we will add it to the datatable and finally return the datatable.

Now for displaying it actually in the report, set the datasource of the crystal report with the data table. The below code demonstrates this.

Code for setting the datasource of the Crystal Report

Listing 2

C#

ReportDocument rptTest1 = new ReportDocument();

rptTest1.Load(System.Web.HttpContext.Current.Request.MapPath("App_Reports/rptpic1.rpt"));

rptTest1.Database.Tables["Images"].SetDataSource(rptTest.ImageTable(System.Web.HttpContext.

Current.Request.MapPath("App_Data/test1.bmp")));

Code for filling the data table with the Image

Listing 3

C#

public class rptTest

{

public static DataTable ImageTable(string ImageFile)

{

DataTable data = newDataTable();

DataRow row;

data.TableName = "Images";

data.Columns.Add("img", System.Type.GetType("System.Byte[]"));

FileStream fs = new FileStream(ImageFile, FileMode.Open);

BinaryReader br = new BinaryReader(fs);

row = data.NewRow();

row[0] = br.ReadBytes(br.BaseStream.Length);

data.Rows.Add(row);

br = null;

fs.Close();

fs = null;

return data;

}

}

When the program runs, the image gets added up to the data table and then is pulled up by the crystal report without any database overhead.


Wednesday, February 20, 2008

Remove Spaces From String

using System.Text.RegularExpressions;

private string RemoveSpaces(string str)
{
string result = "";
Regex regulEx = new Regex(@"[\s]+");
result = regulEx.Replace(str," ");
return result;
}

Thursday, February 7, 2008

Understanding XSD Schema

Introduction

Those who deal with data transfer or document exchange within or across organizations with heterogeneous platforms will certainly accept and appreciate the need and power of XML. I am not going to delve into the merits of XML. I will, however, address a simple but powerful schema concept called XSD or XML Schema Definition.

  • What is XSD Schema?
  • What are the advantages of XSD Schema?
  • What is important in XSD Schema?
What Is a Schema?

A schema is a "Structure", and the actual document or data that is represented through the schema is called "Document Instance". Those who are familiar with relational databases can map a schema to a Table Structure and a Document Instance to a record in a Table. And those who are familiar with object-oriented technology can map a schema to a Class Definition and map a Document Instance to an Object Instance.

A structure of an XML document can be defined as follows:

  • Document Type Definition (DTDs)
  • XML Schema Definition (XSD)
  • XML Data Reduced (XDR) -proprietary to Microsoft Technology

We are specifically going to work with XML Schema Definitions (XSD).

What Is XSD?

XSD provides the syntax and defines a way in which elements and attributes can be represented in a XML document. It also advocates that the given XML document should be of a specific format and specific data type.

XSD is fully recommended by W3C consortium as a standard for defining an XML Document. To know more about latest information on XSD, please refer the W3C site(http://www.w3.org/).

Advantages of XSD

So what is the benefit of this XSD Schema?

  • XSD Schema is an XML document so there is no real need to learn any new syntax, unlike DTDs.

  • XSD Schema supports Inheritance, where one schema can inherit from another schema. This is a great feature because it provides the opportunity for re-usability.

  • XSD schema provides the ability to define own data type from the existing data type.

  • XSD schema provides the ability to specify data types for both elements and attributes.
For more information Click Here

Friday, January 25, 2008

Different Options for Importing Data into SQL Server

In addition to using the Import / Export wizards and/or DTS or SSIS to move data into SQL Server there are also a few other options for doing this that are built into SQL Server.  Some these other options include bcp, BULK INSERT, OPENROWSET as well as others.  The following examples show you some of these different options for importing data and how you can use some of these inline with your T-SQL code as well as others that can be run from the command line.

BCP
This is one of the options that is mostly widely used.  One reason for this is that it has been around for awhile, so DBAs have come quite familiar with this command.  This command allows you to both import and export data, but is primarily used for text data formats.  In addition, this command is generally run from a Windows command prompt, but could also be called from a stored procedure by using xp_cmdshell or called from a DTS or SSIS package.

Here is a simple command for importing data from file C:\ImportData.txt into table dbo.ImportTest.

bcp dbo.ImportTest in 'C:\ImportData.txt' -T -SserverName\instanceName

BULK INSERT
This command is a T-SQL command that allows you to import data directly from within SQL Server by using T-SQL.  This command imports data from file C:\ImportData.txt into table dbo.ImportTest.

BULK INSERT dbo.ImportTest
FROM 'C:\ImportData.txt'
WITH ( FIELDTERMINATOR =',', FIRSTROW = 2 )

OPENROWSET
This command is a T-SQL command that allows you to query data from other data sources directly from within SQL Server.  By using this command along with an INSERT INTO command we can load data from the specified data source into a SQL Server table.

This command will pull in all data from worksheet [Sheet1$]. By using the INSERT INTO command you can insert the query results into table dbo.ImportTest.

INSERT INTO dbo.ImportTest
SELECT * FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=C:\ImportData.xls', [Sheet1$])

Here is another example where data is pulled from worksheet [Sheet1$] by using a SELECT * FROM command. Again, by using the INSERT INTO command you can insert the query results into table dbo.ImportTest.   The query can be any valid SQL query, so you can filter the columns and rows by using this option.

INSERT INTO dbo.ImportTest
SELECT * FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=C:\ImportData.xls', 'SELECT * FROM [Sheet1$]')

OPENDATASOURCE
This command is a T-SQL command that allows you to query data from other data sources directly from within SQL Server. This is similar to the OPENROWSET command.
INSERT INTO dbo.ImportTest
SELECT * FROM OPENDATASOURCE('Microsoft.Jet.OLEDB.4.0',
'Data Source=C:\ImportData.xls;Extended Properties=Excel 8.0')...[Sheet1$]

OPENQUERY
Another option is OPENQUERY.  This is another command that allows you to issue a T-SQL command to select data and again with the INSERT INTO option we can load data into our table.  There are two steps with this process, first a linked server is setup and then second the query is issued using the OPENQUERY command.  This option allow you to filter the columns and rows by the query that is issued against your linked data source.
EXEC sp_addlinkedserver 'ImportData',
   'Jet 4.0', 'Microsoft.Jet.OLEDB.4.0',
   'C:\ImportData.xls',
   NULL,
   'Excel 8.0'
GO

INSERT INTO dbo.ImportTest
SELECT *
FROM OPENQUERY(ImportData, 'SELECT * FROM [Sheet1$]')

Linked Server
Here is yet another option with setting up a linked server and then issuing a straight SQL statement against the linked server.  This again has two steps, first the linked server is setup and secondly a SQL command is issued against the linked data source.
EXEC sp_addlinkedserver 'ImportData',
   'Jet 4.0', 'Microsoft.Jet.OLEDB.4.0',
   'C:\ImportData.xls',
   NULL,
   'Excel 8.0'
GO

INSERT INTO dbo.ImportTest
SELECT * FROM ImportData...Sheet1$


Export data from SQL Server to Excel

     Exporting data from SQL Server to Excel can be achieved in a variety of ways.  Some of these options include Data Transformation Services (DTS), SQL Server Integration Services (SSIS) and Bulk Copy (BCP).  Data Transformation Services (SQL Server 2000) and SQL Server Integration Services (SQL Server 2005) offers a GUI where widgets can be dragged and dropped Each option has advantages and disadvantages, but all can do the job.  It is just a matter of your comfort level with the tools and the best solution to meet the need.
     Another option that is available directly via the T-SQL language is the OPENROWSET command (SQL Server 2000 and SQL Server 2005).  This command can be called directly in any stored procedure, script or SQL Server Job from T-SQL..
    
Below is a simple example of writing out the Job name and date to Sheet1 of an Excel spreadsheet in either SQL Server 2005 or 2000.
INSERT INTO OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=C:\testing.xls;',
'SELECT Name, Date FROM [Sheet1$]')
SELECT [Name], GETDATE() FROM msdb.dbo.sysjobs
GO

Thursday, January 24, 2008

Dynamic SQL

SQL Server offers a few ways of running a dynamically built SQL statement.  These ways are:
  1. Writing a query with parameters
  2. Using EXEC
  3. Using sp_executesql
For more information see: http://www.mssqltips.com/tip.asp?tip=1160

Factory Class for SQL and OLEDB Data Providers

Introduction

While developing a real life software project you need to take in to account many things. One of these things is the choice of database. Most of the times your client will have predefined database (e.g. SQL Server or Oracle). However, some times situation arises such that your database is not fixed at development time. For example if you are developing a product then you will not be knowing the database of your prospective clients in advance. Similarly, if your client demands for database independence (may be because he is migrating his database) then also the database is not fixed. The common solution can be to develop two different sets of data access components - one for SQL server and another for Oracle. However, this will increate your maintenance and version tracking. .NET provides an easy way to tackle this problem. All the data access classes i.e. classes from System.Data.SQLClient and System.Data.OleDb namespaces actually implement certain interfaces from System.Data namespace. In order to make your data access layer components independent of any database you can code against these interfaces rather than actual classes. In this article we will develop a factory class that returns objects from appropriate namespace based on the data provider type you supply. We will also see how to code your data access layer against these interfaces.

System.Data Interfaces

All the .NET providers ( SQL and OleDB) implement certain set of interfaces found in System.Data namespace. Following is the list of some of these interfaces:
  • IDbConnection
  • IDbCommand
  • IDbDataAdapter
  • IDataParameter
You will easily be able to identify the mapping between the interfaces and actual classes.

Developing a factory class

We will develop a factory class that will simply return you objects of specified data provider. For example if you specify that your data provider is SQL server, you will be returned a SQLConnection object where as if you specify that your data provider is any OLEDB compatible database then a OleDbConnection will be returned. Following is the complete code for the class.
using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.OleDb;

namespace DALFactory
{
public class DALObjects
{

public static IDbConnection
GetConnection(DataProviderType type)
{
if(type==DataProviderType.SQL)
{
return new SqlConnection();
}
else
{
return new OleDbConnection();
}
}

public static IDbCommand
GetCommand(DataProviderType type)
{
if(type==DataProviderType.SQL)
{
return new SqlCommand();
}
else
{
return new OleDbCommand();
}
}

public static IDbDataAdapter
GetDataAdapter(DataProviderType type)
{
if(type== DataProviderType.SQL)
{
return new SqlDataAdapter();
}
else
{
return new OleDbDataAdapter();
}
}


public static IDataParameter
GetParameter(DataProviderType type)
{
if(type== DataProviderType.SQL)
{
return new SqlParameter();
}
else
{
return new OleDbParameter();
}
}
}

public enum DataProviderType
{
SQL=0,OLEDB=1
}
}

As you will notice we are returning objects that can be instantiated on their own. That is why we have not included DataReader here. Note that we have created all the methods as static. This way we can easily make use of our methods without creating class instance. Also, note that we will be using DataProviderType enum in our code that indicates which namespaces to use. Now that your have your factory class ready let us see how to use it.

Coding against System.Data interfaces

As mentioned earlier in this section we will see how to use the factory class in our code. Typically when you develop a data access class your code looks like this:
OleDbConnection cnn;
OleDbDataAdapter cmd;
OleDbDataReader dr;

cnn = New OleDbConnection(connectionstring);
cmd = New OleDbDataAdapter(sqlquery,cnn);

dr=cmd.ExecuteReader();
Now, since we need to make our class database independent we will not explicitly declare objects from OleDb or SqlClient namespaces. Instead we will code as follows:
IDbConnection cnn=
DALObjects.GetConnection(DataProviderType.OLEDB);
cnn.Open();
IDbCommand cmd=
DALObjects.GetCommand(DataProviderType.OLEDB);
cmd.ConnectionString="some_sql";
cmd.Connection=cnn ;
IDataReader dr=cmd.ExecuteReader();
Here, we have avoided using explicit classes of specific data provider. Note that since you are using interfaces here you will not be able to invoke methods and properties specific to a particular provider. For example, if you want to use IDbTransaction interface then you can not call Save() method which is specific to SQL Server. However, what we achieve is database independence. Now, in above example the data provider type parameter can be stored in some external XML file that can be easily changed after installation at client's place.

Thursday, January 10, 2008

What is CRM?

      CRM stands for Customer Relationship Management. It is a process or methodology used to learn more about customers' needs and behaviors in order to develop stronger relationships with them. There are many technological components to CRM, but thinking about CRM in primarily technological terms is a mistake. The more useful way to think about CRM is as a process that will help bring together lots of pieces of information about customers, sales, marketing effectiveness, responsiveness and market trends.

CRM helps businesses use technology and human resources to gain insight into the behavior of customers and the value of those customers.

Using CRM, a business can:

  • Provide better customer service
  • Increase customer revenues
  • Discover new customers
  • Cross sell/Up Sell products more effectively
  • Help sales staff close deals faster
  • Make call centers more efficient
  • Simplify marketing and sales processes