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>

Wednesday, 12 June 2013

JSON with single parameter:

 

Json.aspx

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

<!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 src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
    </script>
<script type="text/javascript">
     function Json()  {
         var number1 = 11;
        
         $.ajax({
             url: "WebService.asmx/AddTwoNumbers",
         data:"{'x': '"+ number1+"'}",
         dataType:"json",
         type:"post",
         contentType:"application/json; charset=utf-8",
         success:function (data) {
             alert(data.d);
         },
         error: function (data) {
             alert("Error");
         }
     });
}
</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <input type="button" value="Test" onclick="Json()" />
    
    </div>
    </form>
</body>
</html>

 

 

webservice.asmx code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using
    //ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService {

    public WebService () {

        //Uncomment the following line if using designed components
        //InitializeComponent();
    }

    [WebMethod]
    public string AddTwoNumbers(int x)
    {
        int y = 11;

        int sum = x + y;
        return sum.ToString();
    }
    
}

Tuesday, 11 June 2013

JSON(Javascript Object Notation) Examples:

 

Introduction

This article i will explain what is Json and how we will perform operations on data using Json. 

 

JSON: JavaScript Object Notation.

JSON is syntax for storing and exchanging text information. Much like XML.

JSON is smaller than XML, and faster and easier to parse.

JSON is lightweight text-data interchange format

JSON is language independent

JSON is "self-describing" and easy to understand


JSON uses JavaScript syntax for describing data objects, but JSON is still language and platform independent. JSON parsers and JSON libraries exists for many different programming languages.

Examples:

Step 1:  Open visual studio

File->New->Web site-> select ASP.NET web Application.

After selecting new ASP.NET Application Go to solution explorer.

Right click on project in solution explorer -> Add New Item. You will get Add New Item popup window in that select Web Form and give a name to  page in Name Box then click on OK.  New web page will be added to you solution explorer.

                  Again right on Project –> Add New Item –> Select web service –> name it as JSON –> OK .  web service class add to your project.

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService {

    public WebService () {

        //Uncomment the following line if using designed components
        //InitializeComponent();
    }

    [WebMethod]
    public string HelloWorld() {
        return "Hello World";
    }
    
}

 

 

Note:  Uncomment

[System.Web.Script.Services.ScriptService]

 

Uncomment.bmp

 

Uncomment the above line. If That NameSpace did not exists add that above class name.

I will give 3 Examples with out using parameters , single parameters, and multiple parameters

 

1  With out using parameters.

In this example web method do not have parameters.  So no need to pass any parameters.

web methods must have.

This method called by JSON and display integer.

web method attribute at above web method, as like below.

[WebMethod]

web service class.

[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService {
    
    public WebService () {

        //Uncomment the following line if using designed components
        //InitializeComponent();
    }
    [WebMethod]
    public int DisplayNumber()
    {
        int x = 10;
        return x;
    }
  }

JSON.aspx page Syntax

To run Query add below script in header section of web page.

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
  </script>

 

Or download Jquery file and add that file to your project.

 

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

<!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>Json</title>
<%-- <script src="Jquery.js" type="text/javascript"></script>--%>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
  </script>
   
    <script type="text/jscript" >

        function Json() {
           
            var str = "srinu";

            $.ajax({
                url: 'WebService.asmx/DisplayNumber',
                data: '{}',
                dataType: 'json',
                type: 'POST',
                contentType: 'application/json; charset=utf-8',
                success: function (data) {
                    alert(data.d);
                },
                error: function (data) {

                    alert("Error");
                }
            });    // end $.ajax
        }
    
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <input id="Button1" type="button" onclick="Json()" value="button" />
    
    </div>
    </form>
</body>
</html>

Note: 

Be careful with single quotes ‘ ’  and double quotes “  ”  They must be nested. if you made any mistakes web method will not be called by this function.

And try to use HTML controls.

In above Code Snippet

URL: It is Url of web method which is going to be calling.

Data:  It is pass parameters to web method. In this Example we are not passing any parameters so data part is empty.

Run This example you will  get following output:

 

OUT PUT:

10.