Wednesday, 10 July 2013

Common Class for C#

using System;
using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI.WebControls;using System.Data;using System.Data.SqlClient;using System.Configuration;namespace shajil.CommonClass{

public class Common{
public SqlConnection con = new SqlConnection(Convert.ToString(System.Configuration.ConfigurationSettings.AppSettings["connectionString"]));
//Escape Sequence for XML public string ConvertToXMLFormat(string xml){

if (xml != null && xml != string.Empty && xml != ""){
xml = xml.Replace(
"&", "&");xml = xml.Replace(
"\"", """);xml = xml.Replace(
"'", "'");xml = xml.Replace(
"<", "&lt;");xml = xml.Replace(
">", "&gt;");
return xml;}

elsereturn xml;}

//Fill Dropdown List public void FillDropdownlist(DropDownList ddl, string strTextField, string strValueField, DataTable dt){
ddl.DataSource = dt;
ddl.DataTextField = strTextField;
ddl.DataValueField = strValueField;
ddl.DataBind();
ddl.Items.Insert(0,
new ListItem("Select", string.Empty));
//if (dt.Rows.Count == 1)// ddl.SelectedValue = dt.Rows[0][0].ToString();//else// ddl.SelectedValue = string.Empty;}
//Get Data With Parameter Using Stored Procedure and Dataset.public DataSet GetData1(string procedure, string[,] Parameters){

using (SqlDataAdapter da = new SqlDataAdapter(procedure, con)){
da.SelectCommand.CommandType =
CommandType.StoredProcedure;
for (int i = 0; i < Parameters.GetLength(0); i++){
da.SelectCommand.Parameters.Add(
new SqlParameter(Parameters[i, 0], Parameters[i, 1]));}

DataSet ds = new DataSet();da.Fill(ds);

return ds;}
}

//Get Data With Parameter Using Stored Procedure nd Datatable.public DataTable GetData(string procedure, string[,] Parameters){

using (SqlDataAdapter da = new SqlDataAdapter(procedure, con)){
da.SelectCommand.CommandType =
CommandType.StoredProcedure;
for (int i = 0; i < Parameters.GetLength(0); i++){
da.SelectCommand.Parameters.Add(
new SqlParameter(Parameters[i, 0], Parameters[i, 1]));}

DataTable dt = new DataTable();da.Fill(dt);

return dt;}
}

//Get Data With Out Parameter Using Stored Procedure.public DataTable GetData(string procedure){

using (SqlDataAdapter da = new SqlDataAdapter(procedure, con)){
da.SelectCommand.CommandType =
CommandType.StoredProcedure;
DataTable dt = new DataTable();da.Fill(dt);

return dt;}
}
 

//Insert/Update/Delete Data With Parameter Using Stored Procedure.public string Insert_Update_Data(string procedure, string[,] Parameters){

//Checking the connection stateif (con.State != ConnectionState.Open)con.Open();

//Creating a command objectusing (SqlCommand ObjCom = new SqlCommand(procedure, con)){

//Command type is for stored procedure ObjCom.CommandType = CommandType.StoredProcedure;
//Passing parfor (int i = 0; i < Parameters.GetLength(0); i++){
ObjCom.Parameters.Add(
new SqlParameter(Parameters[i, 0].ToString(), Parameters[i, 1].ToString()));}

string Message = string.Empty;
if (Parameters[1, 1] == "UPDATE" || Parameters[1, 1] == "INSERT" || Parameters[1, 1] == "DELETE"){

DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(ObjCom);da.Fill(dt);

if (dt.Rows.Count > 0)Message = dt.Rows[0][0].ToString();
}

elseObjCom.ExecuteNonQuery();
if (con.State != ConnectionState.Closed)con.Close();

return Message;}
}
}
}

No comments:

Post a Comment