Saturday, March 28, 2015

WEB DB connection & info sp

DB Connection

public class DBConnection
{
    protected SqlConnection sqlcon;
    public DBConnection()
    {
        sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString);
        if (sqlcon.State == ConnectionState.Open)
        {
            sqlcon.Close();
        }
    }
}


  </system.webServer>
  <connectionStrings>
    <add name="DBConnectionString" connectionString="Data Source=192.168.2.112\sqls2008;database=DBV2;user id='v2';password='erpv2';Connect Timeout=30;pooling='false'" />
  
    <!--This connection string is To Import the datas From Excel in two format-->
    <add name="Excel03ConString" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}; Extended Properties='Excel 8.0;HDR={1}'" />
    <add name="Excel07ConString" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0}; Extended Properties='Excel 8.0;HDR={1}'" />
  </connectionStrings>
  <businessObjects>



INFO

using System;
using System.Web.UI.HtmlControls;
//<summary>
//Summary description for ContractInfo
//</summary>
public class ContractInfo
{

    public int ContractId
    {
        get;
        set ;
    }
    public long SignedBy
    {
        get;
        set ;
    }
    public long ResponsibleBy
    {
        get;
        set ;
    }
    public int ProjectId
    {
        get;
        set ;
    }
    public int CustomerId
    {
        get;
        set ;
    }
    public int LedgerId
    {
        get;
        set ;
    }

}



SP


using System;
using System.Data;

//<summary>
//Summary description for ContractSP
//</summary>
public class ContractSP : DBConnection
{
    public int ContractAdd(ContractInfo contractinfo)
    {
        int inId = 0;
        try
        {
            if (sqlcon.State == ConnectionState.Closed)
            {
                sqlcon.Open();
            }
            SqlCommand sccmd = new SqlCommand("ContractAdd", sqlcon);
            sccmd.CommandType = CommandType.StoredProcedure;
            SqlParameter sprmparam = new SqlParameter();
            sprmparam = sccmd.Parameters.Add("@signedBy", SqlDbType.SmallInt);
            sprmparam.Value = contractinfo.SignedBy;
            sprmparam = sccmd.Parameters.Add("@responsibleBy", SqlDbType.SmallInt);
            sprmparam.Value = contractinfo.ResponsibleBy;
            sprmparam = sccmd.Parameters.Add("@projectId", SqlDbType.Int);
            sprmparam.Value = contractinfo.ProjectId;
            inId = int.Parse(sccmd.ExecuteScalar().ToString());
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
        finally
        {
            sqlcon.Close();
        }
        return inId;
    }
    public void ContractEdit(ContractInfo contractinfo)
    {
        try
        {
            if (sqlcon.State == ConnectionState.Closed)
            {
                sqlcon.Open();
            }
            SqlCommand sccmd = new SqlCommand("ContractEdit", sqlcon);
            sccmd.CommandType = CommandType.StoredProcedure;
            SqlParameter sprmparam = new SqlParameter();
            sprmparam = sccmd.Parameters.Add("@contractId", SqlDbType.Int);
            sprmparam.Value = contractinfo.ContractId;
            sccmd.ExecuteNonQuery();
        }
        catch (Exception ex)
        {

        }

        finally
        {
            sqlcon.Close();
        }

    }
    public DataTable ContractViewAll()
    {
        DataTable dtbl = new DataTable();
        try
        {
            SqlDataAdapter sdaadapter = new SqlDataAdapter("ContractViewAll", sqlcon);
            sdaadapter.SelectCommand.CommandType = CommandType.StoredProcedure;
            sdaadapter.Fill(dtbl);
        }
        catch (Exception ex)
        {

        }

        return dtbl;
    }
    public ContractInfo ContractView(int contractId)
    {
        ContractInfo contractinfo = new ContractInfo();
        SqlDataReader sdrreader = null;
        try
        {
            if (sqlcon.State == ConnectionState.Closed)
            {
                sqlcon.Open();
            }
            SqlCommand sccmd = new SqlCommand("ContractView", sqlcon);
            sccmd.CommandType = CommandType.StoredProcedure;
            SqlParameter sprmparam = new SqlParameter();
            sprmparam = sccmd.Parameters.Add("@contractId", SqlDbType.Int);
            sprmparam.Value = contractId;
            sdrreader = sccmd.ExecuteReader();
            while (sdrreader.Read())
            {
                contractinfo.ContractId = int.Parse(sdrreader[0].ToString());
                contractinfo.SignedBy = long.Parse(sdrreader[1].ToString());
                contractinfo.ResponsibleBy = long.Parse(sdrreader[2].ToString());
                contractinfo.ProjectId = int.Parse(sdrreader[3].ToString());
            }
        }
        catch (Exception ex)
        {

        }
        finally
        {
            sqlcon.Close();
            sdrreader.Close();
        }
        return contractinfo;
    }
    public void ContractDelete(ContractInfo infoContract)
    {
        try
        {
            if (sqlcon.State == ConnectionState.Closed)
            {
                sqlcon.Open();
            }
            SqlCommand sccmd = new SqlCommand("ContractDelete", sqlcon);
            sccmd.CommandType = CommandType.StoredProcedure;
            SqlParameter sprmparam = new SqlParameter();
            sprmparam = sccmd.Parameters.Add("@contractId", SqlDbType.Int);
            sprmparam.Value = infoContract.ContractId;
            sprmparam = sccmd.Parameters.Add("@modifiedBy", SqlDbType.SmallInt);
            sprmparam.Value = infoContract.ModifiedBy;
            sprmparam = sccmd.Parameters.Add("@modifiedDate", SqlDbType.DateTime);
            sprmparam.Value = infoContract.ModifiedDate;
            sccmd.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
        finally
        {
            sqlcon.Close();
        }
    }
    public int ContractCount()
    {
        int count = 0;
        try
        {
            if (sqlcon.State == ConnectionState.Closed)
            {
                sqlcon.Open();
            }
            SqlCommand sccmd = new SqlCommand("ContractCount", sqlcon);
            sccmd.CommandType = CommandType.StoredProcedure;
            count = int.Parse(sccmd.ExecuteScalar().ToString());
        }
        catch (Exception ex)
        {

        }

        finally
        {
            sqlcon.Close();
        }
        return count;
    }
    public string ContractNoGenerate(decimal dcId)
    {
        string ContractNo = "";
        try
        {
            if (sqlcon.State == ConnectionState.Closed)
                sqlcon.Open();
            SqlCommand sccmd = new SqlCommand("ContractNoGenerate", sqlcon);
            sccmd.CommandType = CommandType.StoredProcedure;
            sccmd.Parameters.Add("@projectId", SqlDbType.Decimal).Value = dcId;
            ContractNo = sccmd.ExecuteScalar().ToString();
        }
        catch (Exception ex)
        {
        }
        finally
        {
            sqlcon.Close();
        }
        return ContractNo;
    }
    public DataTable ProjectDetailsViewForContractFill(decimal projectId, string strStatus)
    {
        DataTable dtbl = new DataTable();
        try
        {
            if (sqlcon.State == ConnectionState.Closed)
                sqlcon.Open();
            SqlDataAdapter sdadapter = new SqlDataAdapter("ProjectDetailsViewForContractFill", sqlcon);
            sdadapter.SelectCommand.CommandType = CommandType.StoredProcedure;
            sdadapter.SelectCommand.Parameters.Add("@projectId", SqlDbType.Decimal).Value = projectId;
            sdadapter.SelectCommand.Parameters.Add("@status", SqlDbType.VarChar).Value = strStatus;
            sdadapter.Fill(dtbl);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
        finally
        {
            sqlcon.Close();
        }
        return dtbl;
    }
    public DataTable SearchGridFill(int dcCustomerId,int inProjectId, int salesCenterId, string strProject, DateTime from, DateTime to, decimal amontFrom, decimal amountTo, bool deleteStatus)
    {
        // Coded By Jaseel For Search In Contract Register
        DataTable dtbl = new DataTable();
        dtbl.Columns.Add("Sl No", typeof(decimal));
        dtbl.Columns["Sl No"].AutoIncrement = true;
        dtbl.Columns["Sl No"].AutoIncrementSeed = 1;
        dtbl.Columns["Sl No"].AutoIncrementStep = 1;
        try
        {
            if (sqlcon.State == ConnectionState.Closed)
            {
                sqlcon.Open();
            }
            SqlDataAdapter sdadapter = new SqlDataAdapter("ContractSearchGridFill", sqlcon);
            sdadapter.SelectCommand.CommandType = CommandType.StoredProcedure;
            sdadapter.SelectCommand.Parameters.Add("@customerId", SqlDbType.BigInt).Value = dcCustomerId;
            sdadapter.SelectCommand.Parameters.Add("@projectId", SqlDbType.BigInt).Value = inProjectId;
            sdadapter.SelectCommand.Parameters.Add("@salesCenterId", SqlDbType.BigInt).Value = salesCenterId;
            sdadapter.SelectCommand.Parameters.Add("@project", SqlDbType.NVarChar).Value = strProject;
            sdadapter.SelectCommand.Parameters.Add("@from", SqlDbType.DateTime).Value = from;
            sdadapter.SelectCommand.Parameters.Add("@to", SqlDbType.DateTime).Value = to;
            sdadapter.SelectCommand.Parameters.Add("@amountFrom", SqlDbType.Decimal).Value = amontFrom;
            sdadapter.SelectCommand.Parameters.Add("@amountTo", SqlDbType.Decimal).Value = amountTo;
            sdadapter.SelectCommand.Parameters.Add("@deleteStatus", SqlDbType.Bit).Value = deleteStatus;
            sdadapter.Fill(dtbl);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
        finally
        {
            sqlcon.Close();
        }
        return dtbl;
    }
    public DataTable JobordersInContract(int inContractId)
    {
        DataTable dtbl = new DataTable();
        try
        {
            SqlDataAdapter sdadapter = new SqlDataAdapter("JobordersInContract", sqlcon);
            sdadapter.SelectCommand.CommandType = CommandType.StoredProcedure;
            sdadapter.SelectCommand.Parameters.Add("@contractId", SqlDbType.Int).Value = inContractId;
            sdadapter.Fill(dtbl);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
        finally
        {
            sqlcon.Close();
        }
        return dtbl;
    }
    public DataTable ContractFillByProjectId(int inProjectId)
    {
        DataTable dtbl = new DataTable();
        dtbl.Columns.Add("Sl No", typeof(decimal));
        dtbl.Columns["Sl No"].AutoIncrement = true;
        dtbl.Columns["Sl No"].AutoIncrementSeed = 1;
        dtbl.Columns["Sl No"].AutoIncrementStep = 1;
        try
        {
            SqlDataAdapter sdadapter = new SqlDataAdapter("ContractFillByProjectId", sqlcon);
            sdadapter.SelectCommand.CommandType = CommandType.StoredProcedure;
            sdadapter.SelectCommand.Parameters.Add("@projectId", SqlDbType.Int).Value = inProjectId;
            sdadapter.Fill(dtbl);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
        finally
        {
            sqlcon.Close();
        }
        return dtbl;
    }
}