Interview Questions

Just another WordPress.com weblog

Archive for the ‘Uncategorized’ Category

GridView

Posted by ananddesai on March 9, 2010

GridView – Paging and Sorting

1) Default.aspx

<%@ Page Language=”C#” AutoEventWireup=”true”  CodeFile=”Default.aspx.cs” Inherits=”_Default” %>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd“>

<html xmlns=”http://www.w3.org/1999/xhtml“>
<head id=”Head1″ runat=”server”>
    <title></title>
</head>
<body>
    <form id=”form1″ runat=”server”>
    <asp:ScriptManager ID=”ScriptManager1″ runat=”server” />
        <asp:UpdatePanel ID=”UP1″ runat=”server”><ContentTemplate>
    <div>
        Select Table: &nbsp;<asp:DropDownList ID=”DropDownList1″ runat=”server”
            AutoPostBack=”True” onselectedindexchanged=”DropDownList1_SelectedIndexChanged”>
        </asp:DropDownList>
    </div>
    <div>&nbsp;</div>
    <div>&nbsp;</div>
    <div>
        <asp:GridView ID=”GridView1″ runat=”server” CellPadding=”4″ ForeColor=”#333333″
            GridLines=”None” AllowPaging=”True” AllowSorting=”True”
            onpageindexchanging=”GridView1_PageIndexChange2″ OnSorting=”GridView1_SortIndexChange” >
            <RowStyle BackColor=”#F7F6F3″ ForeColor=”#333333″ />
            <FooterStyle BackColor=”#5D7B9D” Font-Bold=”True” ForeColor=”White” />
            <PagerStyle BackColor=”#284775″ ForeColor=”White” HorizontalAlign=”Right”
                Font-Names=”Calibri” />
            <SelectedRowStyle BackColor=”#E2DED6″ Font-Bold=”True” ForeColor=”#333333″ />
            <HeaderStyle BackColor=”#5D7B9D” Font-Bold=”True” ForeColor=”White” />
            <EditRowStyle BackColor=”#999999″ />
            <AlternatingRowStyle BackColor=”White” ForeColor=”#284775″ />
        </asp:GridView>
    </div>
    </ContentTemplate>
    </asp:UpdatePanel>
    </form>
</body>
</html>

2)  Default.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
    string conn = “……………………….”;    //Your ConnectionString will come here.
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            filltable(DropDownList1);
        }
    }

    public void filltable(DropDownList ddl)
    {
        ddl.Items.Add(new ListItem(“— Select table —“, “0”));

        string strQuery = “select name from sys.tables order by name”;
        SqlConnection cn = new SqlConnection(conn);
        SqlCommand cmd = new SqlCommand(strQuery, cn);
        cn.Open();

        SqlDataReader dr = cmd.ExecuteReader();

        if (dr.HasRows)
        {
            while (dr.Read())
            {
                string val = Convert.ToString(dr[“name”]);
                ddl.Items.Add(new ListItem(val, val));
            }
        }
        dr.Close();
        cn.Close();
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string selectedTable = DropDownList1.SelectedItem.Value;

        string strQuery = “select * from ” + selectedTable;

        DataTable dt = SqlManager.ExecuteSqlDataTable(strQuery);

        GridView1.DataSource = dt;
        GridView1.DataBind();
        ViewState[“gv”] = dt;
    }

    protected void GridView1_PageIndexChange2(object sender, GridViewPageEventArgs e)
    {       
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataSource = ViewState[“gv”];
        GridView1.DataBind();
    }
    protected void GridView1_SortIndexChange(object sender, GridViewSortEventArgs e)
    {
        GridView1.DataSource = ViewState[“gv”];
        GridView1.DataBind();
        DataTable dataTable = GridView1.DataSource as DataTable;

        if (dataTable != null)
        {
            DataView dataView = new DataView(dataTable);
            dataView.Sort = e.SortExpression + ” ” + ConvertSortDirectionToSql(e.SortDirection);

            GridView1.DataSource = dataView;
            GridView1.DataBind();
        }       
    }

    private string ConvertSortDirectionToSql(SortDirection sortDirection)
    {
        string newSortDirection = String.Empty;

        switch (sortDirection)
        {
            case SortDirection.Ascending:
                newSortDirection = “ASC”;
                break;

            case SortDirection.Descending:
                newSortDirection = “DESC”;
                break;
        }

        return newSortDirection;
    }
}

Posted in Uncategorized | Leave a Comment »

Pagination Query

Posted by ananddesai on March 9, 2010

Great Pagination Query

declare @rowsPerPage  int
declare @pageNum  int
SET @rowsPerPage = 10
SET @pageNum = 2
 
;With SQLPaging 
As 

 Select Top(@rowsPerPage * @pageNum) ROW_NUMBER() OVER (ORDER BY Content_id) as RowNo, * 
 FROM Content 

select * from SQLPaging where RowNo > ((@pageNum – 1) * @rowsPerPage)

Posted in Uncategorized | Leave a Comment »

My Static Files

Posted by ananddesai on March 9, 2010

This are two static classes which are highly useful in programming

1)  SQLManager.cs

This file serves all sql related transaction just like SQL Helper.

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
/// <summary>
/// Created By: Anand Desai
/// Purpose:  Personalized SQL Helper
/// </summary>
public class SqlManager
{
public SqlManager()
{
}
public struct ParameterArray
{
public string strParamName;
public string strSourceColumn;
public SqlDbType sqlDbType;
public int intSize;
public ParameterDirection direction;
public object objValue;
}
public static SqlConnection OpenConnection(SqlConnection sqlConnection)
{
sqlConnection = new SqlConnection(ConfigurationSettings.AppSettings[“ConnString”].ToString());           // YOur ConnectionString will come here
sqlConnection.Open();
return sqlConnection;
}
public static SqlConnection OpenConnection()
{
SqlConnection sqlConnection = new SqlConnection();
sqlConnection = new SqlConnection(ConfigurationSettings.AppSettings[“ConnString”].ToString());           // YOur ConnectionString will come here
sqlConnection.Open();
return sqlConnection;
}
public static SqlDataReader ExecuteSql(string sql, SqlDataReader reader)
{
SqlConnection sqlConnection = new SqlConnection();
sqlConnection = OpenConnection(sqlConnection);
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
reader = sqlCommand.ExecuteReader(CommandBehavior.CloseConnection);
return reader;
}
public static SqlDataReader ExecuteSql(string sql)
{
SqlConnection sqlConnection = new SqlConnection();
SqlDataReader reader;
sqlConnection = OpenConnection(sqlConnection);
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
reader = sqlCommand.ExecuteReader(CommandBehavior.CloseConnection);
return reader;
}
public static DataTable ExecuteSqlDataTable(string sql)
{
DataTable dTable = new DataTable();
SqlConnection sqlConnection = new SqlConnection();
DataSet dataset = new DataSet();
sqlConnection = OpenConnection(sqlConnection);
SqlDataAdapter da = new SqlDataAdapter(sql, sqlConnection);
da.Fill(dataset);
da.Dispose();
sqlConnection.Close();
dTable = dataset.Tables[0];
dataset.Dispose();
return dTable;
}
public static DataTable ExecuteSqlDataTable(string sql, DataTable dTable)
{
SqlConnection sqlConnection = new SqlConnection();
DataSet dataset = new DataSet();
sqlConnection = OpenConnection(sqlConnection);
SqlDataAdapter da = new SqlDataAdapter(sql, sqlConnection);
da.Fill(dataset);
da.Dispose();
sqlConnection.Close();
dTable = dataset.Tables[0];
dataset.Dispose();
return dTable;
}
public static SqlDataReader ExecuteSql(string sql, SqlConnection sqlConnection, SqlTransaction sqlTransaction, SqlDataReader reader)
{
//SqlConnection sqlConnection = OpenConnection();
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection, sqlTransaction);
reader = sqlCommand.ExecuteReader();
return reader;
}
public static DataSet ExecuteSqlDataSet(string sql, DataSet dataSet)
{
SqlConnection sqlConnection = new SqlConnection();
sqlConnection = OpenConnection(sqlConnection);
SqlDataAdapter da = new SqlDataAdapter(sql, sqlConnection);
da.Fill(dataSet);
da.Dispose();
sqlConnection.Close();
return dataSet;
}
public static DataSet ExecuteSqlDataSet(string sql, SqlConnection sqlConnection, SqlTransaction sqlTransaction, DataSet dataSet)
{
dataSet = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(sql, sqlConnection);
da.Fill(dataSet);
da.Dispose();
return dataSet;
}
public static Object ExecuteScalar(string sql)
{
SqlConnection sqlConnection = new SqlConnection();
sqlConnection = OpenConnection(sqlConnection);
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
Object obj = sqlCommand.ExecuteScalar();
sqlConnection.Close();
return obj;
}
public static Object ExecuteScalar(string sql, SqlConnection sqlConnection, SqlTransaction sqlTransaction)
{
// SqlConnection sqlConnection = OpenConnection();
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection, sqlTransaction);
Object obj = sqlCommand.ExecuteScalar();
// sqlConnection.Close();
return obj;
}
public static bool ExecuteNonResultSql(string sql)
{
SqlConnection sqlConnection = new SqlConnection();
sqlConnection = OpenConnection(sqlConnection);
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
int returnValue = sqlCommand.ExecuteNonQuery();
sqlConnection.Close();
return returnValue != -1 ? false : true;
}
public static bool ExecuteNonResultSqlSupplier(string sql, SqlConnection sqlConnection, SqlTransaction sqlTransaction)
{
//SqlConnection sqlConnection = OpenConnection();
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection, sqlTransaction);
int returnValue = sqlCommand.ExecuteNonQuery();
//sqlConnection.Close();
return returnValue != 0 ? false : true;
}
public static bool ExecuteNonResultSql(string sql, SqlConnection sqlConnection, SqlTransaction sqlTransaction)
{
//SqlConnection sqlConnection = OpenConnection();
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection, sqlTransaction);
int returnValue = sqlCommand.ExecuteNonQuery();
// sqlConnection.Close();
return returnValue != -1 ? false : true;
}
public static bool ExecuteNonResultSql_SP(string strCommandText, ParameterArray[] paramArray)
{
SqlCommand sqlCommand = new SqlCommand();
sqlCommand = PrepareCommand(strCommandText, paramArray);
int returnValue = sqlCommand.ExecuteNonQuery();
sqlCommand.Parameters.Clear();
return returnValue != -1 ? true : false;
}
public static Object ExecuteScalar_SP(string strCommandText, ParameterArray[] paramArray)
{
SqlCommand sqlcmd = new SqlCommand();
sqlcmd = PrepareCommand(strCommandText, paramArray);
Object obj = sqlcmd.ExecuteScalar();
sqlcmd.Parameters.Clear();
return obj;
}
public static DataSet ExecuteDataSet_SP(string strCommandText, ParameterArray[] paramArray)
{
DataSet ds = new DataSet();
try
{
SqlCommand sqlcmd = new SqlCommand();
sqlcmd = PrepareCommand(strCommandText, paramArray);
SqlDataAdapter sqlDa = new SqlDataAdapter(sqlcmd);
sqlDa.Fill(ds);
sqlcmd.Parameters.Clear();
}
catch (Exception ex)
{
string strError = ex.Message;
}
return ds;
}
private static SqlCommand PrepareCommand(string strSqlText, ParameterArray[] param)
{
SqlCommand sqlCommand = new SqlCommand();
SqlParameter sqlParameter;
sqlCommand.CommandText = strSqlText;
sqlCommand.CommandType = CommandType.StoredProcedure;
SqlConnection sqlconnect = new SqlConnection();
sqlconnect = OpenConnection(sqlconnect);
sqlCommand.Connection = sqlconnect;
for (int i = 0; i < param.Length; i++)
{
sqlParameter = new SqlParameter(param[i].strParamName, param[i].sqlDbType, param[i].intSize, param[i].strSourceColumn);
sqlParameter.Value = param[i].objValue;
sqlParameter.Direction = param[i].direction;
sqlCommand.Parameters.Add(sqlParameter);
}
return sqlCommand;
}
public static DataSet ExecuteSqlDataSet(string sql)
{
SqlConnection sqlConnection = new SqlConnection();
sqlConnection = OpenConnection(sqlConnection);
DataSet dataset = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(sql, sqlConnection);
da.Fill(dataset);
da.Dispose();
sqlConnection.Close();
return dataset;
}
}

2)  Utility.cs

This file serves all utilities like safe string convert or integer convert, date format, month counting etc.

using System;

using System.Data;

using System.Configuration;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

/// <summary>

///   Created By: Anand Desai

///    Purpose:  To personalize Utilites management

/// </summary>

///

public class Utility

{

public Utility()

{

}

}

public sealed class Util

{

private Util()

{

}

public static int GetInteger(bool b)

{

return b ? 1 : 0;

}

public static bool GetBoolean(object str)

{

return (str == null || str == DBNull.Value) ? false : Convert.ToBoolean(str);

}

public static string GetString(object str)

{

return (str == null || str == DBNull.Value) ? string.Empty : str.ToString().Trim();

}

public static int GetInteger(object str)

{

return (str == null || str == DBNull.Value) ? 0 : Convert.ToInt32(str);

}

public static DateTime GetDateTime(object str)

{

return (str == null || str == DBNull.Value) ? DateTime.MinValue : Convert.ToDateTime(str);

}

public static Single GetSingle(object str)

{

return (str == null || str == DBNull.Value) ? Convert.ToSingle(0.0) : Convert.ToSingle(str);

}

public static string GetRelpace(string str)

{

if (str != string.Empty && str != null)

{

str = str.Replace(“&”, “&amp;”);

str = str.Replace(“‘”, “”);

//str = str.Replace(“‘”,”‘”);

str = str.Replace(“<“, “&lt;”);

str = str.Replace(“>”, “&gt;”);

str = str.Replace(“\n”, “”);

str = str.Replace(“\r”, ” “);

str = str.Replace(“-“, “”);

}

return str;

}

public static string DateTimeFormat(DateTime Date)

{

string Day, month, year, FormattedDate;

Day = Date.Day.ToString();

month = Date.Month.ToString();

//year = Date.Year.ToString().Remove(0,2);

year = Date.Year.ToString();

if (month.Length <= 1)

month = “0” + Date.Month.ToString();

if (Day.Length > 1)

FormattedDate = Day + “/” + month + “/” + year;

else

FormattedDate = “0” + Day + “/” + month + “/” + year;

return FormattedDate;

}

public static string DisplayMonth(int Month)

{

switch (Month)

{

case 1:

return “Jan”;

case 2:

return “Feb”;

case 3:

return “Mar”;

case 4:

return “Apr”;

case 5:

return “May”;

case 6:

return “Jun”;

case 7:

return “Jul”;

case 8:

return “Aug”;

case 9:

return “Sep”;

case 10:

return “Oct”;

case 11:

return “Nov”;

case 12:

return “Dec”;

default:

return “Jan”;

}

}

}

public class ConvertUtility

{

public static string ToString(bool b)

{

return b ? “1” : “0”;

}

/*  public static string ToString(int i)

{

return XmlConvert.ToString(i);

}*/

public static int ToInt32(bool b)

{

return b ? 1 : 0;

}

public static bool ToBool(string str)

{

return str == “1”;

}

/*   public static int ToInt32(string str)

{

return XmlConvert.ToInt32(str);

}*/

}

Posted in Computer, Object, OOP, Programming, Static Classes, Technology, Uncategorized | Leave a Comment »

MyLot

Posted by ananddesai on March 14, 2008

myLot User Profile

Posted in Uncategorized | Leave a Comment »