Monday, 8 July 2013

Find age or number of years between Two dates:

 

 

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs"
Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Age</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table>
    <tr>
    <td>
    BirthDaydate:
    </td>
    <td>
        <asp:TextBox runat="server" ID="txtBirthDayDate" ></asp:TextBox>
    </td>
    <td>
    Number Of days:
    </td>
    <td>

    <asp:TextBox runat="server" ID="txtResult"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td colspan="3">
<asp:Button runat="server" ID="btnDays" OnClick="CountNumberOfdays_Click" Text="CheckDates" />
    </td>
    </tr>
    </table>


   
    </div>
    </form>
</body>
</html>

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void CountNumberOfdays_Click(object sender, EventArgs e)
    {

        DateTime date = Convert.ToDateTime(txtBirthDayDate.Text);
      
        DateTime BirthDayDate = new DateTime(date.Year, date.Month, date.Day);
        DateTime Todaydate = DateTime.Now;

        TimeSpan ts = Todaydate - BirthDayDate;
        
        float differenceInDays = ts.Days;
        float age = differenceInDays / 365;

        txtResult.Text = age.ToString();
    }
}

 

Result:

Enter Date in first Text box:

1-02-2013

 

Age:

0.5123287

Sunday, 7 July 2013

confirm box in Javascript:

 

 

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs"
Inherits="Test" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

    <script type="text/javascript">
        function conformbox() {
            var x;
            var r = confirm("please click any one");
            if (r == true) {
                x = "Ok Continue";
            }
            else {
                x = "cansel";
            }
            document.getElementById("id").innerHTML = x;
        }
</script>
    

</head>
<body>
    <form id="form1" runat="server">
    <div>
    
<input type="button" value="click me" onclick="conformbox()" />
<p id="id" ></P>
    </div>
    </form>
</body>
</html>

Find the biggest number in an array:

 

 

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs"
Inherits="Test" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

    <script type="text/javascript">
        function srinu() {
            var ar = new Array();
            ar[0] = 6;
            ar[1] = 4;
            ar[2] = 17;
            ar[3] = 3;
            ar[4] = 11;
            ar[5] = 10;
            var max = ar[0];

            for (var i = 1; i < ar.length; i++) {

                if (max < ar[i]) {
                    max = ar[i];
                }
                
            }
            alert(max);
           

        }
</script>
    

</head>
<body>
    <form id="form1" runat="server">
    <div>
    
<input type="button" onclick="srinu()" value="click me" />
    </div>
    </form>
</body>
</html>

Count the number of checked box checked:

 

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs"
Inherits="Test" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>


    <script type="text/javascript" >
        function srinu() {
            var i;
            var count = 0;

            for (i = 1; i <= 4; i++) {
                var str = "c" + i;
                if (document.getElementById(str).checked == true) {
                    count++;
                }
            }
            alert(count);
        }
</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <input type="checkbox" id="c1"  value="1">fierst checkbox<br/>
<input type="checkbox" id="c2"  value="2">second checkbox<br/>
<input type="checkbox" id="c3"  value="3">third checkbox<br/>
<input type="checkbox" id="c4"  value="4">four checkbox<br/>
<input type="button" onclick="srinu()" value="click me" />
    </div>
    </form>
</body>
</html>

Friday, 5 July 2013

Prime Number Between Two numbers:

 

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter Two Numbers");
            Console.WriteLine("Enter First Number");
            int i = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Enter second number");
            int j = Convert.ToInt32(Console.ReadLine());
            for (int k = i; k <= j; k++)
            {
                int count = 0;
                for (int l = 1; l <= k; l++)
                {

                    if (k % l == 0)
                    {
                        count++;
                    }

                }
                if (count == 2)
                {
                    Console.WriteLine(k);
                }
            }
            Console.ReadLine();


        }
    }
}

Monday, 1 July 2013

Login Example page

 

In this article i will give simple Login page.  It may be very useful for beginners. if you have any queries feel free to ask me.

1. Create a login table

2. Insert User names and passwords into login table.

3. Design login page . Take two TextBoxes , one button and one result label.

4. Write code for Onclick Event in Login.cs  file.


    -- CREATE LOGINTABLE
    CREATE TABLE LOGINTABLE
    (
    USERNAME VARCHAR(50),
    PASSWORD VARCHAR(50)
    )

    -- INSERT INTO LOGINTABLE

    INSERT INTO LOGINTABLE VALUES
                                (
                                'srinu.munagala33@gmail.com',
                                '07m11a1233'
                                )

SELECT * FROM LOGINTABLE

 

USERNAME PASSWORD
srinu.munagala33@gmail.com 07m11a1233

 

 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="testLogin._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table align="center" >
    
    <tr>
    <td colspan="2">

    <span> Login </span>
    
    </td>
    </tr>
    <tr>
    <td>
    <span>User Name</span>
    </td>
    <td>
    <asp:TextBox runat="server" ID="txtUserName" Width="180px" ></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td>
    <span>Password</span>
    </td>
    <td>
    <asp:TextBox runat="server" ID="txtPassword" Width="180px" TextMode="Password" ></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td colspan="2" align="center">
    <asp:Button runat="server" ID="btnSubmit" Text="Login" OnClick="login_Submit" />
    
    </td>
    
    </tr>
    <tr>
    <td align="center" colspan="2">
    <asp:Label runat="server" ID="lblResult" style=" color:Red ; font-size:20px;" ></asp:Label>

    <span  ></span>
    </td>
    
    </tr>
    </table>
    </div>
    </form>
</body>
</html>

 

.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;

namespace testLogin
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }


        protected void login_Submit(object sender, EventArgs e)
        {
            SqlConnection SqlCon = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog =Test;Integrated Security=True");

            try
            {
                SqlCon.Open();
                SqlCommand SqlCom = new SqlCommand("select count(*) from LOGINTABLE where userName ='" + txtUserName.Text + "' and password ='" + txtPassword.Text + "'", SqlCon);

                int i = Convert.ToInt32(SqlCom.ExecuteScalar());
                if (i > 0)
                {
                    lblResult.Text = "Login Success";
                }

                else
                {
                    lblResult.Text = "Please enter valied username and password";
                }
            }
            catch (Exception ex)
            {
                lblResult.Text = ex.Message;
            }
            finally
            {
                SqlCon.Close();
            }
        }
        
        }
    }

Thursday, 20 June 2013

Complete List View:

 

Create two tables:

-- Create Login Page

Create Table Login

(

StudentId bigint identity(1,1),

StudentName varchar(50),

StudentPassword varchar(50)

)

STUDENT ID

STUDENT NAME

 

STUDENT PASSWORD

1

Srinivas

Srinivas

2

Krishna

Krishna

3

Ramu

Ramu

4

Rakesh

Rakesh

 

 

-- Create Marks table

Create table marks

(

StudentId bigint,

Telugu int,

Hindi int,

English int,

Maths int,

Science int,

Scoial int

)

-- Insert values to Marks table

image

 

-- Create stored procedure

CREATE PROCEDURE dbo.GetStudentMarks
(
@USERNAME VARCHAR(50),
@PASSWORD VARCHAR(50)
)
    
AS
BEGIN
    --DECLARING TEMP @STUDENTID
    DECLARE @STUDENTID BIGINT
    SET @STUDENTID = (SELECT TOP 1 STUDENTID
                        FROM    Login
                        WHERE    StudentName = @USERNAME
                        AND        STUDENTPASSWORD = @PASSWORD)

    --RETRIVING STUDENT MARKS
    SELECT *
    FROM    MARKSTABLE
    WHERE    STUDENTID = @STUDENTID
END
    /* SET NOCOUNT ON */

-- List View Page

 

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ListView.aspx.cs"
Inherits="ListView" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>ListView</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table align="center">
           <tr>
                <td>
                    UserName:
                </td>
                <td>
                    <asp:TextBox runat="server" ID="txtUsername" >
    
                    </asp:TextBox>
                </td>

           </tr>
            <tr>
                <td>
                      Password:
                 </td>
                 <td>
                    <asp:TextBox runat="server" ID="txtpassword" >
                    
                    </asp:TextBox>
                    </td>
            </tr>
            <tr>
            <td colspan="2" align="center">
               <%-- <input type="button" id="btnAdminLogin" value="Submit" />
--%>

<asp:Button runat="server" ID="btnSubmit" OnClick="GetMarks_Submit" Text="Submit" />
</td>
</tr>    
    </table>
    <table align="center">
    <tr>
    <td>
    <asp:ListView runat="server" ID="ltvData" >
    <LayoutTemplate>
    
    
    
    <table align="center">  
    <tr>
  <td>
   <asp:Label runat="server" ID="Label11" Text="StudentID" ></asp:Label>
</td>
                                                        <td>
<asp:Label runat="server" ID="lblTelgu1" Text="Telugu" ></asp:Label>
  </td>
  <td>
  <asp:Label runat="server" ID="lblHindi1" Text="Hindi" ></asp:Label>
    </td>
  <td>
  <asp:Label runat="server" ID="lblEnglish1" Text="English" ></asp:Label>
  </td>
   <td>
<asp:Label runat="server" ID="lblMaths1" Text="Maths" ></asp:Label>
   </td>
   <td>
   <asp:Label runat="server" ID="lblScience1" Text="Science" ></asp:Label>
   </td>
   <td>
    <asp:Label runat="server" ID="lblsocial1" Text="Social" ></asp:Label>
     </td>
                            
  <td>
</tr>
  <tr>
  <td>
  <tbody>
<asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder>
</tbody>
  </td>
    </tr>
                             
            </td>
            </tr>
    </table>
    
    
    </LayoutTemplate>
    <ItemTemplate>
   
    
    <table align="center">  
    <tr>
              <td>
    <asp:Label runat="server" ID="Label1" Text='<%#Eval("StudentID") %>' ></asp:Label>
<asp:LinkButton ID="btnupdate" runat="server" Text="Edit"
PostBackUrl='<%#"MarksEdit.aspx?id=" + Eval("StudentID") %>'>
    </asp:LinkButton></td>
    <td>
     <asp:Label runat="server" ID="lblTelgu" Text='<%#Eval("Telugu") %>' ></asp:Label>
     </td>
    <td>
     <asp:Label runat="server" ID="lblHindi" Text='<%#Eval("Hindi") %>' ></asp:Label>
      </td>
        <td>
     <asp:Label runat="server" ID="lblEnglish" Text='<%#Eval("English") %>' ></asp:Label>
     </td>
     <td>
   <asp:Label runat="server" ID="lblMaths" Text='<%#Eval("Maths") %>' ></asp:Label>
     </td>
     <td>
       <asp:Label runat="server" ID="lblScience" Text='<%#Eval("Science") %>' ></asp:Label>
     </td>
       <td>
        <asp:Label runat="server" ID="lblsocial" Text='<%#Eval("Social") %>' ></asp:Label>
           </td>
                            </tr>
    </table>
    
    </ItemTemplate>

    </asp:ListView>
    </td>
    </tr>
    
    </table>
    </div>
    </form>
</body>
</html>

.Cs code

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;

public partial class ListView : System.Web.UI.Page
{
    BL objBL = new BL();
    DataSet dt;
         
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
           // FillDataList();
        }

    }
    protected void GetMarks_Submit(object sender, EventArgs e)
    {
        FillDataList();
    }
    #region FillDatalist
    /// <summary>
    /// FillDatalist
    /// </summary>
    public void FillDataList()
    {
        try
        {
            string StudentName = txtUsername.Text.ToString();
            string StudentPassword = txtpassword.Text.ToString();
            dt = objBL.GetStudentMarks(StudentName, StudentPassword);
            ltvData.DataSource = dt;
            ltvData.DataBind();
        }
        catch (Exception Ex)
        {
            Response.Write("<script>alert('Error')</script>");
        }

    }

    #endregion
}

 

 

-- Take 3 classes.

That is

1. BL.

2. DL.

3. Sqlcommon.

 

image 

BL:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Summary description for BL
/// </summary>
public class BL
{
    DL objDL = new DL();
    DataSet dt;
    public BL()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    #region GetStudentMarks

    /// <summary>
    /// GetStudentMarks
    /// </summary>
    /// <param name="StudentName"></param>
    /// <param name="StudentPassword"></param>
    /// <returns></returns>
    public DataSet GetStudentMarks(string StudentName, string StudentPassword)
    {
        try
        {
            return dt = objDL.GetStudentMarks(StudentName, StudentPassword);
        }
        catch (Exception Ex)
        {
            throw Ex;
        }
    }
    #endregion
}

 

DL:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;

/// <summary>
/// Summary description for DL
/// </summary>
public class DL
{
    SqlCommon objSqlcommom = new SqlCommon();
    DataSet dt;
   
    public DL()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    #region SetProperties
    string StudentName;
    string StudentPassword;

    public string StudentPassword1
    {
        get { return StudentPassword; }
        set { StudentPassword = value; }
    }

    public string StudentName1
    {
        get { return StudentName; }
        set { StudentName = value; }
    }
   
    #endregion
    #region GetStudentMarks
    public DataSet GetStudentMarks(string StudentName, string StudentPassword)
    {
        string ProcedureName = "GetStudentMarks";
        try
        {
            SqlParameter parStudentName = objSqlcommom.CreateParameter("@USERNAME", DbType.String, StudentName);
            SqlParameter parStudentPassword = objSqlcommom.CreateParameter("@PASSWORD", DbType.String, StudentPassword);
            SqlParameter[] par = { parStudentName, parStudentPassword };
            dt = SqlHelper.ExecuteDataset(SqlCommon.connectionString, ProcedureName, par);
            return dt;
            
        }
        catch (Exception Ex)
        {
            throw Ex;
        }
    }
    #endregion
}

SQLCOMMON CLASS:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

/// <summary>
/// Summary description for SqlCommon
/// </summary>
public class SqlCommon
{
    public SqlCommon()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    #region connectionString
    /// <summary>
    /// GetConnecction String
    /// </summary>
    public static string connectionString {
        get { return ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; }

    }
    #endregion
    #region CreateSqlParameter

    /// <summary>
    /// Createparameter
    /// </summary>
    /// <param name="Paramenter"></param>
    /// <param name="type"></param>
    /// <param name="value"></param>
    /// <returns></returns>
    public SqlParameter CreateParameter(string Paramenter, DbType type, object value)
    {
        SqlParameter par = new SqlParameter();
        par.ParameterName = Paramenter;
        par.DbType = type;
        par.Value = value;
        return par;
    }
    #endregion
}

 

Create a connection in web config:

<connectionStrings>
        <add name="ConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ListView.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>
    </connectionStrings>