Pages

Search This Blog

Wednesday, 18 June 2014

Display & Edit a value from database.....

Hi friends, in this post I will show you “How to display a value in the label from Database”

Before saying that I will say at what situations we need of this??
Let us take an example “Facebook” view profile.. In that situation we need to display your details in the labels. Your details will be there in the database we must bring them from database with the help of “Session value” (See the class about Session) we will bring details of a particular person.

Here is the code ....

update.aspx:

<table style="height: 250px; width: 376px">
                                      <tr>
                                           <center>
                                                 <font color="#890208" size="3">
                                                        <h1>                                                                  Here You Can Update Your Details.
                                                          </h1>
                                                    </font>
                                                </center>
                                            </tr>
                                            <tr>
                                                <td class="style3">
                                                    <asp:Label ID="Label1" runat="server" Text="Mail ID" ForeColor="#890208"></asp:Label>
                                                </td>
                                                <td class="style3">
                                                    <asp:Label ID="lbmail" runat="server"></asp:Label>
                                                </td>
                                                <td class="style3">
                                                    <asp:Button runat="server" ID="Editmail" Text="Edit" OnClick="Editmail_Click" CausesValidation="false" />
                                                </td>
                                                <td class="style3">
                                                    <asp:TextBox ID="tblogin" runat="server"> </asp:TextBox>
                                                    <asp:RequiredFieldValidator ID="reqlogin" runat="server" ControlToValidate="tblogin"
                                                        ErrorMessage="Enter Mail ID">*</asp:RequiredFieldValidator>
                                                </td>
                                                <td>
                                                    <asp:Button ID="updatedetails" Text="Update" runat="server" CausesValidation="True"
                                                        OnClick="updatedetails_Click" />
                                                </td>
                                            </tr>
                                            <tr>
                                                <td class="style2">
                                                    <asp:Label ID="Label2" runat="server" Text="Phone Number" ForeColor="#890208"> </asp:Label>
                                                </td>
                                                <td class="style2">
                                                    <asp:Label ID="lbphn" runat="server"></asp:Label>
                                                </td>
                                                <td class="style2">
                                                    <asp:Button ID="Editphn" runat="server" Text="Edit" OnClick="Editphn_Click" CausesValidation="false" />
                                                </td>
                                                <td class="style2">
                                                    <asp:TextBox ID="tbphn" runat="server"> </asp:TextBox>
                                                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="tbphn"
                                                        ErrorMessage="Enter Phone Number">*</asp:RequiredFieldValidator>
                                                </td>
                                                <td>
                                                    <asp:Button ID="updatephn" runat="server" Text="Update" CausesValidation="True" OnClick="updatephn_Click" />
                                                    <asp:ValidationSummary runat="server" ID="vs" ShowMessageBox="true" ShowSummary="false" />
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>
                                                </td>
                                                <td>
                                                </td>
                                                <td>
                                                </td>
                                                <td>
                                                </td>
                                                <td>
                                                    <asp:Button ID="cancel" runat="server" Text="CANCEL" CausesValidation="false" OnClick="cancel_Click" />
                                                </td>
                                            </tr>
                                        </table>


update.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.Net;
using System.IO;

public partial class update : System.Web.UI.Page
{
    string str = "Data Source=SANTOSHKLPKL-HP\\SQLEXPRESS;Initial Catalog=blood_bank;Integrated Security=True";
    SqlConnection con = new SqlConnection();

    protected void Page_Load(object sender, EventArgs e)
    {
        tblogin.Visible = false;
        tbphn.Visible = false;
        updatedetails.Visible = false;
        updatephn.Visible = false;
        cancel.Visible = false;

        string uname = Session["Username"].ToString();
        lbname.Text = Session["Username"].ToString();
        con.ConnectionString = str;
        con.Open();
        SqlCommand cm = new SqlCommand("Select * from users where USER_NAME='"+uname+"'", con);
        SqlDataReader dr;
        dr = cm.ExecuteReader();
        if (dr.Read())
        {
           
            lbmail.Text = dr["MAIL_ID"].ToString();
            lbphn.Text = dr["PHONE_NUMBER"].ToString();
            con.Close();
        }
       
    }
   
    protected void Editmail_Click(object sender, EventArgs e)
    {
      
        tblogin.Visible = true;
        updatedetails.Visible = true;
        cancel.Visible = true;
       
    }
    protected void Editphn_Click(object sender, EventArgs e)
    {
        tbphn.Visible = true;
        updatephn.Visible = true;
        cancel.Visible = true;
       
    }
    protected void updatedetails_Click(object sender, EventArgs e)
    {
        con.ConnectionString = str;
        // “updatemail” is the procedure I had created where I wrote the query for update of mail.
        SqlCommand cm1 = new SqlCommand("updatemail", con);
        cm1.CommandType = CommandType.StoredProcedure;
        SqlParameter x1 = new SqlParameter("@uname", lbname.Text);
        SqlParameter x2 = new SqlParameter("@newmail", tblogin.Text.Trim());

        cm1.Parameters.Add(x1);
        cm1.Parameters.Add(x2);
        con.Open();

        SqlDataReader dr1;
        dr1 = cm1.ExecuteReader();
        con.Close();
        con.Open();
        cm1.ExecuteNonQuery();
        con.Close();
        Response.Redirect("./update.aspx");
    }
    protected void updatephn_Click(object sender, EventArgs e)
    {
        con.ConnectionString = str;
        // “updatephone” is the procedure where I wrote the query for updating phone number
        SqlCommand cm2 = new SqlCommand("updatphone", con);
        cm2.CommandType = CommandType.StoredProcedure;
        SqlParameter x1 = new SqlParameter("@uname", lbname.Text);
        SqlParameter x2 = new SqlParameter("@newphn", tbphn.Text.Trim());

        cm2.Parameters.Add(x1);
        cm2.Parameters.Add(x2);
        con.Open();

        SqlDataReader dr2;
       
            dr2 = cm2.ExecuteReader();
            con.Close();
            con.Open();
            cm2.ExecuteNonQuery();
            con.Close();
            Response.Redirect("./update.aspx");
       
    }
    protected void cancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("./Display.aspx");
    }
}



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

                                                                    -Your's Santoshklpkl



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
Animated Social Gadget - Blogger And Wordpress Tips Twitter Bird Gadget