Thursday, December 4, 2008

Sharepoint Timer Job.

Creating Custom SharePoint Timer Jobs:

http://www.andrewconnell.com/blog/articles/CreatingCustomSharePointTimerJobs.aspx

click the above link to be redirected to the post author at his site for a detailed article on how to create a sharepoint timer job.

Tuesday, February 19, 2008

SqlHelper Example 2

Lets take a look at a more dramatic example of the benefits of using the Data Access Application Block. For this example, we will retrieve a DataSet containing the results from a stored procedure (getProductsByCategory) that takes a single parameter (CategoryID). Again, to illustrate the amount of code saved by using the Data Access Application Block, let's first look at the necessary code when not using the Data Access Block:

// Open a connection to Northwind
SqlConnection objConn = new
SqlConnection("Server=(local);Database=Northwind;Integrated Security=True;");
objConn.Open();

//Create the stored procedure command object
SqlCommand objCmd = new SqlCommand("getProductsByCategory", objConn);
objCmd.CommandType = CommandType.StoredProcedure;
//create the parameter object for the stored procedure parameter
objCmd.Parameters.Add("@CategoryID", SqlDbType.Int);
objCmd.Parameters["@CategoryID"].Value = 1;

//create our DataAdapter and DataSet objects
SqlDataAdapter objDA = new SqlDataAdapter(objCmd);
DataSet objDS = new DataSet("Category_Results");

//fill the dataset
objDA.Fill(objDS);

//databind the datagrid
DataGrid1.DataSource = objDS;
DataGrid1.DataBind();

//close connection
objConn.Close();
Now, we will call the same stored procedure and return a DataSet using the SqlHelper class's ExecuteDataset() method:


string strConn = "Server=(local);Database=Northwind;Integrated Security=True;";
DataSet objDS = SqlHelper.ExecuteDataset(strConn, CommandType.StoredProcedure,
"getProductsByCategory", new SqlParameter("@CategoryID", 1) );

DataGrid2.DataSource = objDS;
DataGrid2.DataBind();

SqlHelper examples and advantages

In order to illustrate the advantage of using the Data Access Block, let's take a look at sample code that creates a SqlDataReader object and binds it to a DataGrid without using the Data Access Block. In general, returning a DataReader involves establishing a connection, creating a SqlCommand, and executing the command against the database. The resulting SqlDataReader object can then be bound to a DataGrid:

//create the connection string and sql to be executed
string strConnTxt = "Server=(local);Database=Northwind;Integrated Security=True;";
string strSql = "select * from products where categoryid = 1";

//create and open the connection object
SqlConnection objConn = new SqlConnection(strConnTxt);
objConn.Open();

//Create the command object
SqlCommand objCmd = new SqlCommand(strSql, objConn);
objCmd.CommandType = CommandType.Text;

//databind the datagrid by calling the ExecuteReader() method
DataGrid1.DataSource = objCmd.ExecuteReader();
DataGrid1.DataBind();

//close the connection
objConn.Close();

Now lets look at the same task using the SqlHelper class's static ExecuteReader() method:

//create the connection string and sql to be executed
string strSql = "select * from products where categoryid = 1";
string strConnTxt = "Server=(local);Database=Northwind;Integrated Security=True;";

DataGrid4.DataSource = SqlHelper.ExecuteReader(strConnTxt, CommandType.Text, strSql);
DataGrid4.DataBind();

As you can see, there is considerably less code in the second example. To execute a SQL statement and return a SqlDataReader, the ExecuteReader() method requires only the connection string, command type and SQL to be executed. The SqlHelper class contains all of the "plumbing" necessary to establish a connection, create a SqlCommand and execute the command against the database with a single static method call.
The main advantage of the Application Blocks is that they greatly reduce the amount of code you need to write by encapsulating common tasks in a wrapper class. While at first glance this may not seem that profound of a benefit, realize that writing less code means more than just shorter time needed to write the code. It also means fewer bugs and typos, and an overall lower total cost to produce the software.

Using the Data Access Application Block to Execute Stored ProceduresThe ExecuteReader() method also has several overloads that enable you to perform stored procedures and transactions. Lets take a quick look at the same method, but this time we'll execute a stored procedure:

DataGrid5.DataSource = SqlHelper.ExecuteReader(strConnTxt, CommandType.StoredProcedure,
"getProductsByCategory", new SqlParameter("@CategoryID", 1));
DataGrid5.DataBind();



How to read more than one output value from Stored Procedure.

using System;
using System.Data;
using System.Data.SqlClient;
namespace OutPutParms
{
class OutputParams
{
[STAThread] static void Main(string[] args)
{
using( SqlConnection cn = new SqlConnection("server=(local);Database=Northwind;user id=sa;password=;"))
{
SqlCommand cmd = new SqlCommand("CustOrderOne", cn); cmd.CommandType=CommandType.StoredProcedure ;
SqlParameter parm=new SqlParameter("@CustomerID",SqlDbType.NChar) ; parm.Value="ALFKI";
parm.Direction =ParameterDirection.Input ;
cmd.Parameters.Add(parm);
SqlParameter parm2=new SqlParameter("@ProductName",SqlDbType.VarChar); parm2.Size=50;
parm2.Direction=ParameterDirection.Output;
cmd.Parameters.Add(parm2);
SqlParameter parm3=new SqlParameter("@Quantity",SqlDbType.Int); parm3.Direction=ParameterDirection.Output;
cmd.Parameters.Add(parm3);
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
Console.WriteLine(cmd.Parameters["@ProductName"].Value);
Console.WriteLine(cmd.Parameters["@Quantity"].Value.ToString());
Console.ReadLine();
}
}
}
}

Monday, November 12, 2007

how to create typed dataset.

A Typed DataSet can be generated in two ways,
  1. Using Visual Studio .NET IDE.
  2. Using XSD.exe (Using VS.Net command prompt)
    Open VS.Net command prompt and Type XSD /? For the help on this exe.

Creating a Typed DataSet using Visual Studio .NET IDE

Let me explain a step by step procedure to create a Typed DataSet,

1. Open VS .Net IDE and Click on File -> New -> Project and Select Console Application.
2. Enter name for the project. Say TypedDataSetTest.


3. Right click on the solution and click on Add-> Add New Item will show a dialog box.



Select DataSet from templates pane, give the name (Say TypedDs.xsd) and click on Open. This will add file by name TypedDs.xsd to the solution.



4. Click on the Server Explorer browse to the database and drop the table on the TypedDs.xsd file.

If we check the xml file for the same then we can see the schema for the table.

This dataset can be used in the same manner as the normal dataset to get the data.

Typed Dataset Vs Dataset

what is typed dataset .. ?? Is it any different from normal dataset that we use in our daily tasks and application??
what happens if we have a datatype mismatch error in our dataset..
what type of error can we expect in such situation..

As all of we know, we can specify the Data type when we create a DataColumn for a DataTable. This is to enforce the runtime Type-safety for the column so that only data of specified data type can be stored in the column. In the same way, in most of the cases we prefer to make a DataSet itself as Type-safe so as to protect it from runtime mismatch. Hence Typed DataSets generate classes that expose each object the in the DataSet in Type-safe manner. These classes inherits directly from DataSet class.


Let us look into a small example which explain the Typed DataSet,

1. Using DataSet:

//Create DataAdapter
SqlDataAdapter daEmp = new SqlDataAdapter("SELECT empno,empname,empaddress FROM EMPLOYEE",conn);
//Create a DataSet Object
DataSet dsEmp = new DataSet();
//Fill the DataSet
daEmp.Fill(dsEmp,"EMPLOYEE");
//Let us print first row and first column of the table
Console.Write(dsEmp.Tables["EMPLOYEE"].Rows[0][0].ToString());
//Assign a value to the first column
dsEmp.Tables["EMPLOYEE"].Rows[0][0] = "12345";//This will generate runtime error as empno column is integer

If we observe above code we will get a runtime error when this code gets executed as the value assigned to the column (empno) does not take string value. Also any misspell of the column will generate a runtime error. And also we need to go thro the hierarchy to get the final value.


2. Using Typed DataSet:

//Create DataAdapter
SqlDataAdapter daEmp = new SqlDataAdapter("SELECT empno,empname,empaddress FROM EMPLOYEE",conn);
//Create a DataSet Object
EmployeeDS dsEmp = new EmployeeDS ();
//Fill the DataSet
daEmp.Fill(dsEmp,"EMPLOYEE");
//Let us print first row and first column of the table
Console.Write(dsEmp.EMPLOYEE[0].empno.ToString());
//Assign a value to the first column
dsEmp.EMPLOYEE[0].empno = "12345";//This will generate compile time error.

If we see above code, a typed dataset is very much similar to a normal dataset. But the only difference is that the sehema is already present for the same. Hence any mismatch in the column will generate compile time errors rather than runtime error as in the case of normal dataset. Also accessing the column value is much easier than the normal dataset as the column definition will be available in the schema.

Sunday, November 11, 2007

Remoting

what is remoting..
the use and implementation of remoting in our project development cycle.
if any one has any useful information then please help me .

awaiting comments.

Thursday, November 8, 2007

Importing and exporting a custom webpart in to sharepoint using dotnet API

I would like to import a custom webpart that was exported from other system in to mine .
I have tried adding the safe control tag to the web.config file and i have also place the assembly in the GAC. but I still don't know why i am facing error.