Pages

Search This Blog

Wednesday 28 May 2014

Displaying Date and Time

Hi friends, in this session I will show “How to display system date and time in your website”.

In many websites we will see the time and present date. Normally many people were interested to display time and date. So I had written this code....

Step1: (In file.aspx)
First to display time and date we need to create 2 labels. In 1st label we will display time and in 2nd label we will display date. Place the below code where you want to display.

<asp:Label ID="lb1" runat="server" Text="date"> </asp:Label> <br />

<asp:Label ID="lb2" runat="server" Text="time"> </asp:Label>

Step2: (In file.aspx.cs)

Place the below code on Page_Load (that means while loading the page itself it shows the date and time)

lb1.Text = System.DateTime.Now.ToString("yyyy/MM/dd");

lb2.Text = System.DateTime.Now.ToShortTimeString();

Note: We can change the date format as we want “dd/MM/YYYY” or “MM/DD/YYYY”

You can rate my class on right side top of my blog ....
                                                                          -Your's Santoshklpkl

Monday 26 May 2014

Had different types of users in your application ??

Hi friends, today I will say the solution for “Multiple types of users in your application”.

Let us take an example so that you could understand it easily. Example Software company website. They will be different types of users like Manager, HR and Admin etc....

Then most of will be in ambiguity that “whether I must create 3 login pages for 3 users??” No, here there is a solution. In login table in the database we will add a column “Role” so it will differentiate them in the back-end.

Now comes to front-end. Here we will check the “Role” when the user presses login button. Depending upon the “Role” we will redirect to that particular page. I mean if manager then “Manager.aspx” , if HR then “HR.aspx” and if Admin “Admin.aspx”.

Example code:

I have created a blood bank website in which they were three types of users:-

1) User

2) Admin

3) Student

Login.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;
using System.Data.SqlClient;
using System.Web.Security;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page
{
// Server address
    string str = "Data Source=SANTOSHKLPKL-HP\\SQLEXPRESS;Initial Catalog=blood_bank;Integrated Security=True";

// Connection

    SqlConnection con = new SqlConnection();
    protected void Page_Load(object sender, EventArgs e)
    {
    //Making session values “Null”
        Session["Username"] = null;
        Session["Password"] = null;
    }
   
    protected void login_Click1(object sender, EventArgs e)
    {

    //Storing textboxes values into sessions.
        Session["Username"] = tblogin.Text.Trim();
        Session["Password"] = tbpwd.Text.Trim();
       
        try
        {

            string mrole;
            con.ConnectionString = str;
            con.Open();

            SqlDataReader dr;

    // Created a procedure “sp_login”

            SqlCommand cmd1 = new SqlCommand("sp_getlogin", con);
            cmd1.CommandType = CommandType.StoredProcedure;

  // Assigning textboxes values to paramaters “@user” and “@pwd”

SqlParameter x1 = new SqlParameter("@user", tblogin.Text.Trim());
SqlParameter x2 = new SqlParameter("@pwd", tbpwd.Text.Trim());

          // Adding parameters
            cmd1.Parameters.Add(x1);
            cmd1.Parameters.Add(x2);
            con.Close();

            SqlCommand cmd2 = new SqlCommand("select Role from Allusers where username ='" + tblogin.Text + "'", con);

            SqlDataReader dr1;
            con.Open();
            dr1 = cmd2.ExecuteReader();
            dr1.Read();
     
         //Assigning “Role” value to mrole string.
            mrole = dr1["Role"].ToString();
            con.Close();
            con.Open();
            dr = cmd1.ExecuteReader();

            if (!dr.HasRows)
            {
        Response.Write("<script>alert('Invalid Username or
Password!');</script>");
            }
            else
            {
                con.Close();
                if (mrole == "ADMIN")
                {
                    Response.Redirect("./Admin.aspx");
                }
                else if (mrole == "USER")
                {
                    Response.Redirect("./Display.aspx");
                }
                else if (mrole == "STUDENT")
                {
                    Response.Redirect("./Studentsearch.aspx");
                }

            }
        }
        catch (Exception)
        {
            Response.Write("<script> alert('Invalid Login details !')</script>");
        }

        con.Close();
   
    }
   
}

You can rate my class on right side top of my blog....

                                                                       -Your's santoshklpkl

Saturday 24 May 2014

How to store values into database tables from your application....

Hi friends, in this session I will show you “How to store values from your page into database”

Step1:
You must create a table in the database.

Step2:
Connect the database in to your application. (See class 2)

Step3:

Let us take an example and see how to store data from our application into database table.

Registration form consists
·        Username
·        Password
·        Email

These 3 values must be stored into the database table.

Registration.aspx:

<asp:Label ID="lb1" runat="server" Text="Username "> </asp:Label>
<asp:TextBox ID="tbuser" runat="server"></asp:TextBox>

<asp:Label ID="lb1" runat="server" Text="Enter Password"> </asp:Label>
<asp:TextBox ID="tbpwd" runat="server"></asp:TextBox>

<asp:Label ID="lb1" runat="server" Text="Email"> </asp:Label>
<asp:TextBox ID="tbemail" runat="server"></asp:TextBox>

Registration.aspx.cs:

We can store data into database in two ways :

Way1: Creating a procedure in database. By using that procedure we will store data into database table.

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

public partial class Register : System.Web.UI.Page
{
 //ServerName  
 string str = "Data Source=SANTOSHKLPKL-HP\\SQLEXPRESS;Initial Catalog=blood_bank;Integrated Security=True";
//Creating a connection
    SqlConnection con = new SqlConnection();
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnsubmit_Click(object sender, EventArgs e)
    {
      
        con.ConnectionString = str;

  //Procedure name sp_insert where I wrote insert command
        SqlCommand cmd = new SqlCommand("sp_insert", con);
        cmd.CommandType = CommandType.StoredProcedure;

 //Initializing parameters values
       SqlParameter p1 = new SqlParameter("@User",tbuser.Text.Trim());
       SqlParameter p2 = new SqlParameter("@pwd",tbpwd.Text.Trim());
       SqlParameter p3 = new SqlParameter("@mail",tbemail.Text.Trim());
       
   // Adding paramaters into procedure so it can use it.
        cmd.Parameters.Add(p1);
        cmd.Parameters.Add(p2);
        cmd.Parameters.Add(p3);
   
       con.Open();
       
       cmd.ExecuteNonQuery();
              
      Response.Write("<script> alert('User Registered successfully
         !')</script>");
                       
       }                    
    
}

Way2:

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

public partial class Register : System.Web.UI.Page
{

//ServerName  
 string str = "Data Source=SANTOSHKLPKL-HP\\SQLEXPRESS;Initial 
Catalog=blood_bank;Integrated Security=True";

   //Creating a connection
   SqlConnection con = new SqlConnection();
   
   protected void Page_Load(object sender, EventArgs e)
    {    
    }

    protected void btnsubmit_Click(object sender, EventArgs e)
    {
       
        con.ConnectionString = str;

     // Storing textbox values into strings   

       string s1,s2,s3;
       s1=tbuser.Text;
       s2=tbpwd.Text;
       s3=tbemail.Text;
    
    // I wrote insert command here only. Uname,Password,Email are       database table column names.
     SqlCommand cmd = new SqlCommand("Insert into registration               (Uname,Password,Email)  values (‘s1’,’s2’,’s3’)”, con);
      
       con.Open();
       cmd.ExecuteNonQuery();       

       Response.Write("<script> alert('User Registered successfully
         !')</script>");
                
       }                    
}  

You can rate my class on right side top of my blog.....

                                                                        -Your's santoshklpkl

Tuesday 20 May 2014

Basic Commands that we use in Sql while creating project....

Hi friends, today I will say you about basic Sql commands that we use while creating our project.

1) Create
2) Insert
3) Update
4) Delete

Create Table Statement
    CREATE TABLE "table_name"
("column 1" "data type for column 1" [column 1 constraint(s)],
"column 2" "data type for column 2" [column 2 constraint(s)],
...
[table constraint(s)]);

·        Drop Table Statement
DROP TABLE "table_name";

·        Truncate Table Statement
TRUNCATE TABLE "table_name";

·        Insert Into Statement
INSERT INTO "table_name" ("column1", "column2", ...)
VALUES ("value1", "value2", ...);

·        Update Statement
UPDATE "table_name"
SET "column_1" = [new value]
WHERE "condition";

·        Delete From Statement
DELETE FROM "table_name"
WHERE "condition";

·        Alter Statement
ALTER TABLE tb_TableName ADD Record_Status varchar(20);

ALTER TABLE tb_TableName 

add constraint cnt_Record_Status Default '' for Record_Status;

Six types of constraints can be placed when creating a table:

·        NOT NULL Constraint: Ensures that a column cannot have NULL value.

·        DEFAULT Constraint: Provides a default value for a column when none is specified.


·        UNIQUE Constraint: Ensures that all values in a column are different.

·        CHECK Constraint: Makes sure that all values in a column satisfy certain criteria.

·        Primary Key Constraint: Used to uniquely identify a row in the table.

·        Foreign Key Constraint: Used to ensure referential integrity of the data.

SQL > SQL Syntax
In this page, I have listed the SQL syntax for each of the SQL commands in this tutorial, making this an easy reference for someone to learn SQL.

·       Select Statement
   SELECT "column_name" FROM "table_name";

·        Distinct
SELECT DISTINCT "column_name"
FROM "table_name";

·        Where
SELECT "column_name"
FROM "table_name"
WHERE "condition";

·        And/Or
SELECT "column_name"
FROM "table_name"
WHERE "simple condition"
{[AND|OR] "simple condition"}+;

·        In
SELECT "column_name"
FROM "table_name"
WHERE "column_name" IN ('value1', 'value2', ...);

·        Between
SELECT "column_name"
FROM "table_name"
WHERE "column_name" BETWEEN 'value1' AND 'value2';

·        Like
SELECT "column_name"
FROM "table_name"
WHERE "column_name" LIKE {PATTERN};

·        Order By
SELECT "column_name"
FROM "table_name"
[WHERE "condition"]
ORDER BY "column_name" [ASC, DESC];

·        Count
SELECT COUNT("column_name")
FROM "table_name";

·        Group By
SELECT "column_name1", SUM("column_name2")
FROM "table_name"
GROUP BY "column_name1";

·        Having
SELECT "column_name1", [Function("column_name2")]
FROM "table_name"
[GROUP BY "column_name1"]
HAVING (arithematic function condition);
    
    You can rate my class on right side top of my blog......
    
                                                        -Your's santoshklpkl



·        
Animated Social Gadget - Blogger And Wordpress Tips Twitter Bird Gadget