DropDown Helper class

DropDownHelperDB class
so many time write query for drop down i will create one dropdown helper class in getlist method to return DataTable there are there are three type of argument first one is a columns argument in this argument pass columns name for i.e "TrnId,City"
second one is a table name pass that columns name find will in this table that is sure that is notice and third one is where clause.

i.e DropDownHelperDB.GetList("TrnId,City","tbl_city","state_TrnId = 1")

if u don't wont to where close that time write string.Empty

i.e DropDownHelperDB.GetList("TrnId,City","tbl_city",string.Empty)

public class DropDownHelperDB
{
    public static DataTable GetList(string columns, string tableName, string where)
    {
        DataTable tempDt = new DataTable();
        using (SqlConnection myConnection = new SqlConnection(AppConfiguration.ConnectionString))
        {
            using (SqlCommand myCommand = new SqlCommand("DropDownHelper", myConnection))
            {
                myCommand.CommandType = CommandType.StoredProcedure;
                myCommand.Parameters.AddWithValue("@columns", columns);
                myCommand.Parameters.AddWithValue("@tableName", tableName);
                myCommand.Parameters.AddWithValue("@where", where);
                myConnection.Open();
                using (SqlDataReader myReader = myCommand.ExecuteReader())
                {
                    if (myReader.HasRows)
                    {
                        tempDt.Load(myReader);
                    }
                    myReader.Close();
                }
            }
        }
        return tempDt;
    }
}

stored procedure
ALTER PROCEDURE dbo.DropDownHelper
(

@columns varchar(max),
@tableName varchar(max),
@where varchar(max)
)
AS

if @where = ''
BEGIN

EXEC('SELECT ' + @columns  + ' FROM ' + @tableName );

END

ELSE

EXEC('SELECT ' + @columns  + ' FROM ' + @tableName + ' WHERE ' + @where);
DropDown Helper class DropDown Helper class Reviewed by Bhaumik Patel on 3:19 AM Rating: 5