Monday, October 15, 2012

Loging system in OOP using ASP.Net(C#)

the connectionStrings in Web.config ....

<connectionStrings>
    <add name="UserLoginSystem" connectionString="Data Source=KNIGHT-PC;Initial Catalog=dbLogin;Integrated Security=True" providerName="System.Data.SqlClient"/>
  </connectionStrings>


then I have created a class and named it DBGateway which will be used as a base class of all class for db connection.

Codes of the DBGateway class.


using System.Configuration;
using System.Data.SqlClient;

namespace UserLoginSystem.DAL.Gateway
{
    public class DBGateway
    {
        private SqlConnection sqlConnection;
        public SqlCommand SqlCommand;

        public DBGateway()
        {
            sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["UserLoginSystem"].ConnectionString);
            SqlCommand =new SqlCommand();
        }
        public SqlConnection SqlConnectionObj
        {
            get
            {
                return sqlConnection;
            }
        }

        public SqlCommand SqlSqlcommandObj
        {
            get
            {
                SqlCommand.Connection = sqlConnection;
                return SqlCommand;
            }
        }
    }
}





now codes of Entity class .... I named the class LoginSys and declared two properties UserName and Password and used auto property.

namespace UserLoginSystem.DAL.DAO
{
    public class LoginSys
    {
        public string UserName { get; set; }
        public string Password { get; set; }
    }
}



now codes of Gateway class ... I named the class LoginGateway and used DBGateway as base class for db connection.



using System;
using System.Data.SqlClient;
using UserLoginSystem.DAL.DAO;

namespace UserLoginSystem.DAL.Gateway
{
    public class LoginGateway:DBGateway

    {
        public LoginSys loginSys;
        public bool GetLoginInfoSys(string username, string password)
        {
            try
            {
                SqlConnectionObj.Open();
                string query = "";
                query = "SELECT * FROM dbo.tbl_user_login WHERE 

                username='" + username + "' and   
                password='" + password + "'";
                SqlCommand = new SqlCommand(query, SqlConnectionObj);
                SqlDataReader sqlDataReader = SqlCommand.ExecuteReader();
                if (sqlDataReader!=null)
                {
                    return sqlDataReader.HasRows;
                }
            }
            catch (Exception)
            {
                throw new Exception("Username not found");
            }

            finally
            {
                if (SqlConnectionObj != null && SqlConnectionObj.State == ConnectionState.Open)
                {
                    SqlConnectionObj.Close();
                }
            }
            return false;
        }
    }
}



public class LoginGateway:DBGateway .... [here DBGateway  is base class and LoginGateway is sub class] and finally I closed the connection that's very important.



now codes of Default.aspx page .... First of all I have taken two textbox, one label, one button  and I named those as usernameTextBox.Text, passwordTextBox.Text, lblmsg.Text and loginButton.


using System;
using UserLoginSystem.DAL.DAO;
using UserLoginSystem.DAL.Gateway;

namespace UserLoginSystem
{
    public partial class _Default : System.Web.UI.Page
    {
        private LoginSys loginSys;
        private LoginGateway loginGateway;
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void loginButton_Click(object sender, EventArgs e)
        {
             try
            {
                loginGateway = new LoginGateway();
                if (loginGateway.GetLoginInfoSys(usernameTextBox.Text, passwordTextBox.Text) == true)
                {
                    loginSys = new LoginSys();
                    loginSys.UserName = usernameTextBox.Text;
                    Session["username"] = loginSys.UserName;
                    Response.Redirect("Hello.aspx");
                }
                else
                {
                    lblmsg.Text = "Username/Password wrong";
                }
            }
            catch (Exception exception)
            {
                lblmsg.Text = exception.Message;
            }
        }
    }
}



I have used the Session variable to get the user name in the redirected page.

                loginSys = new LoginSys();
                loginSys.UserName = usernameTextBox.Text;
                Session["username"] = loginSys.UserName;


and finally the codes of redirected page ...



using System;

namespace UserLoginSystem
{
    public partial class Hello : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                lblmsg.Text = "Hi " + Session["username"];
            }
        }
    }
}


used a lebel lblmsg.Text to show the username and caught the data using Session variable  Session["username"]

Never hasitate to  comment or ask me.
Happy Codding .......

Sunday, October 14, 2012

GridView to Excel . . . .



using Microsoft.Office.Interop.Excel;

private void exportButton_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Excel.Application xApplication = new
Microsoft.Office.Interop.Excel.Application();

Workbook workbook =
xApplication.Workbooks.Add(XlSheetType.xlWorksheet);
Worksheet worksheet = (Worksheet)xApplication.ActiveSheet;
xApplication.Visible = true;

for (int i = 1; i < purchaseReportDataGridView.Rows.Count; i++)
{
for (int j = 0; j < 13; j++)
{
worksheet.Cells[i + 1, j + 1] =
purchaseReportDataGridView.Rows[i].Cells[j].Value;
}
}
}

Never hasitate to  comment or ask me.
Happy Codding .......

Saving multiple data with single click . . . . .



for (int i = 1; i <= pQuantity; i++){

product.Product1 = Product1 .Text;
product.Product2= Product2.Text;
product.Product3 = Product3 .Text;
product.Product4 = Product4 .Text;
product.Product5 = Product5 .Text;
product.Product6 = Product6 .Text;
}
MessageBox.Show(msg);

Never hasitate to  comment or ask me.
Happy Codding .......