Friday, May 30, 2008

Class creating in C#

This is simple application for how to create namespace and classes in C#.After how do call them in functional class.

Step #1,

Open VSS 2005 -New->Website-> Add New Item - > add class module

If you give the file name as "DemoClass",then you will see the Class name DemoClass on your file like this,

Public DemoClass
{
If you want constructor go some codes from here..
}

Start to write below of this but under class's curly braces.
Example I create method praise

public string praise()
{
return "Praise the Lord";
}


public string praiseParameter(string s)
{

return s;
}


Now you can call this function on your different

Just initialise the class name with new operator like below
DemoClass Obj=new DemoClass();

Obj.praise();

Oracle - C#

This is my first step to taste the power of C# with Oracle Database.

OracleConnection connection = new OracleConnection(YourConnection)
OracleCommand command = new OracleCommand(SQL, connection);
connection.Open();
OracleDataReader reader = command.ExecuteReader();
try
{
while (reader.Read())
{
Response.Write((reader.GetValue(0));
}
}
finally
{
reader.Close();
}
}

Tuesday, May 27, 2008

Excel Sheet data in Gridview

How to retrieve the Excel sheet as Table and display them on Gridview.

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
public partial class ExcelGVCS : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
OleDbConnection DBConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Server.MapPath("~/App_Data/Running.xls") + ";" + "Extended Properties=\"Excel 8.0;HDR=Yes\"");
DBConnection.Open();
string SQLString = "SELECT * FROM [Sheet1$]";
OleDbCommand DBCommand = new OleDbCommand(SQLString, DBConnection);
IDataReader DBReader = DBCommand.ExecuteReader();
GridView1.DataSource = DBReader;
GridView1.DataBind();
DBReader.Close();
DBConnection.Close();
}
}

Tuesday, May 20, 2008

SQL Script for table making

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Employee]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[Employee]
GO

CREATE TABLE [dbo].[Employee] (
[EmpCode] [char] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[Name] [char] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[DOB] [char] (15) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
) ON [PRIMARY]
GO

table creation script for below application

Monday, May 19, 2008

Adding records to Gridview

This is an simple application for adding new records on the form and once the record added into GridView,it will show up with newly added record with out regenerating. :-)

Here I used delete option as discard as it wants to my specific requirement.
If possible,I put AJAX version of this same module soon.



protected void Button2_Click(object sender, EventArgs e)
{
SqlConnection con;


Cache["code"]=TextBox1.Text;
Cache["Name"]=TextBox2.Text;
Cache["DOB"]=TextBox3.Text;


con = new SqlConnection("Data Source=(local);Initial catalog=Test;user id=sa;pwd=murugesan");
con.Open();
SqlCommand smd = new SqlCommand("INSERT INTO Employee(EmpCode,Name,DOB) Values('" + Cache["code"].ToString() + "','" + Cache["Name"].ToString() + "','" + Cache["DOB"].ToString() + "')", con);

try
{

int a = (int)smd.ExecuteScalar();


}
catch (Exception ex)
{
Response.Write("Already exist !");


}


GridShow();

smd.Dispose();
con.Close();

}
public void GridShow()
{


DataSet ds = new DataSet();
con = new SqlConnection("Data Source=(local);Initial catalog=Test;user id=sa;pwd=murugesan");
con.Open();
cmd = new SqlCommand();
cmd.CommandText = "SELECT *from Employee";
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
ada = new SqlDataAdapter();
ada.SelectCommand = cmd;

ada.Fill(ds, "Employee");
GridView1.DataSource = ds;
GridView1.DataBind();
ada.Dispose();
cmd.Dispose();
con.Dispose();


}
public void Murugesan(object sender, GridViewCommandEventArgs er)
{
SqlConnection con;
SqlCommand cmd;


if (er.CommandName == "Discard")
{
string na = Convert.ToString(er.CommandArgument);
con = new SqlConnection("Data Source=(local);Initial catalog=Test;user id=sa;pwd=murugesan");
con.Open();
cmd = new SqlCommand("Delete from Employee where Name='" + na + "'", con);
cmd.ExecuteNonQuery();

GridShow();



}
}

Friday, May 16, 2008

Data Row dynamically adding to DataTable

DataTable tbl = new DataTable();
tbl.Columns.Add("ColumnA");

tbl.Columns.Add("ColumnB");

tbl.Columns.Add("ColumnC");

for(int i = 0; i<10; i++)

{

DataRow nr = tbl.NewRow();

nr["ColumnA"] = "A" + i.ToString();

nr["ColumnB"] = "B" + i.ToString();

nr["ColumnC"] = "C" + i.ToString();

tbl.Rows.Add(nr);

}

PrintRows(tbl);
}


private static void PrintRows(DataTable tbl)
{

for(int i=0; i
{

Console.WriteLine("row: {0}, ColumnA: {1}, ColumnB: {2}", i, tbl.Rows[i]["ColumnA"], tbl.Rows[i]["ColumnB"]);

}

}

Tuesday, May 13, 2008

Multiple file reading in C#

Today I met one situation that I have to read all text files under one general folder.One of My senior Mr.Jignesh hada developed an utility to convert the raw text into DataSet,then it will be taken out as Crystal Report in C#.
I learned many things from him just sitting and watching carefully.Then he suggest me that he have more than 20 files of text file those has to be merged as one as well as create one Database.
He asked me to merge all notepad files.
Simple I read all textfiles then on the fly i created a text files thats going to be serve as Main file for creating DataSet.

Here is the code snippet to read all text files under one folder.
string []files = Directory.GetFiles(@"D:\Murugesan\");
foreach(string sp in files)
{

StreamReader sr = new StreamReader(sp);
string contents = sr.ReadToEnd();
Response.Write(contents);
//use pre tag with contents as concatenation.

sr.Close();

Sunday, May 04, 2008

AJAX -Introduction

Introduction
AJAX stands for Asynchronous JavaScript and XML. Working in ASP.Net with AJAX, developers can develop ASP.Net web applications more user friendly and fast enough to provide data with page postbacks. This programming methodology has removed old fashioned page refreshing method that throws the user back to the page top and lose the context where he was working before clicking the submit button to save data.

ASP.Net Postbacks
ASP.Net methodology based on client server data exchange works as all the data from the client end is sent to the web server then the web server processes the data and sends back to the client. It means AJAX has the ability to make asynchronous calls to a web server. AJAX sends only necessary data to the web server.

AJAX and ASP.Net Framework
ASP.Net framework considers AJAX as a client callback scripts. AJAX enables you to update the page content without posting the page back to the server. Basically AJAX uses XMLHttp ActiveX component or the XMLHttpRequest according to the client’s browser such as Internet Explorer or FireFox.

AJAX allows partial page postbacks that minimize the network usage by not sending the all form data to the server. This method enables the server to perform only limited actions on the data received coz the server does not need to process the whole page elements or images. There is no need to process the viewstate, images or all page elements to send back to the client while processing partial postbacks.

With AJAX there is no full page postback that maintains page location even when user clicks the submit button to perform some action on the page. Hence the use state is maintained and the user doesn’t need to scroll down to the location where he was working before clicking the submit button.

AJAX GridView Databinding























C# code to Load the GridView using AJAX Asynchronous Postback

protected void bindGridView()
{
string connectionString = ConfigurationManager.ConnectionStrings["SqlConnectionString"].ToString();
SqlConnection sqlCon = new SqlConnection(connectionString);

try
{
SqlCommand sqlCmd = new SqlCommand("select * from categories", sqlCon);
sqlCmd.CommandType = CommandType.Text;
SqlDataAdapter sqlAdp = new SqlDataAdapter(sqlCmd);
DataSet myDataSet = new DataSet();
sqlAdp.Fill(myDataSet);

// Delay the current Thread to display
// the UpdateProgress status
// Not required in real projects
System.Threading.Thread.Sleep(4000);

GridView1.DataSource = myDataSet;
GridView1.DataBind();
}
catch(Exception ex)
{
lblErrorMsg.Text = ex.Message;
}
}

protected void btnLoadData_Click(object sender, EventArgs e)
{
bindGridView();
}