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



No comments:

Post a Comment

Animated Social Gadget - Blogger And Wordpress Tips Twitter Bird Gadget