EDUDOTNET

Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Friday, April 28, 2017

How to convert and bind JSON string in to asp.net GridView with c# without help of looping and object mapper class (wrapper)?



How to convert and bind JSON string in to asp.net GridView with c# without help of looping and object mapper class (wrapper)?
Requirement: - If we don`t know about json string/object, how many columns are there and no any idea of column name, In this case we can`t able to create a c# class for object deserializeObject, I mean wrapper class. Here requirement is bind gridview from json object without ajax/jquery and looping in c# and without wrapper class.

1.       Create a web application in visual studio


2.        Right click on project and add a web form by click on "Web Form".


3.        After that open a windows popup in this enter your Web Form name and click on "Add" button.





4.        Open your web form .aspx source/design part and go to ToolBox and drag & drop asp.net gridview control in to web form and set gridview style as per your requirement.





5.        Here way to bind gridview from JSON following:
a.        Using dynamic keyword with JsonConvert.DeserializeObject, here you need to import Newtonsoft.Json
b.      Using DataTable with JsonConvert.DeserializeObject, here you need to import using System.Data;




Dynamic:
This is a data type, dynamic data type introduced since .Net Framework 4.0. The dynamic data type allows you to perform any operations and will be resolved at run time. It does not require explicit type casting for any operation at run-time, because it identifies the types at run-time only. Dynamic type can be passed as function argument and function also can return object type. Useful when coding using reflection or dynamic language support or with the COM objects, because we require to write less amount of code. [http://www.c-sharpcorner.com/blogs/variable-object-and-dynamic-data-type-in-c-sharp1]

DataTable:
In .NET, it's a class that represents a table of in-memory data.
 


CODE
.ASPX
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="BindJosn2Grid.aspx.cs" Inherits="BindJSON2ASPDOTGridView.BindJosn2Grid" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2>Bind GridView From JSON Object (Converted In to Dynamic Object [dynamic keyword])</h2>
        <asp:GridView ID="grdJSON2Grid" runat="server" BackColor="White" BorderColor="#3399ff"
            BorderStyle="Dotted" BorderWidth="1px" CellPadding="3" GridLines="Both"></asp:GridView>

        <br />
        <br />
         <h2>Bind GridView From JSON Object (Converted In to DataTable Object [DataTable])</h2>
        <asp:GridView ID="grdJSON2Grid2" runat="server" BackColor="White" BorderColor="#3399ff"
            BorderStyle="Dotted" BorderWidth="1px" CellPadding="3" GridLines="Both"></asp:GridView>
    </div>
    </form>
</body>
</html>


.CS
protected void Page_Load(object sender, EventArgs e)
        {
            //Random json string, No fix number of columns or rows and no fix column name.
            string myDynamicJSON = "[{'Member ID':'00012','First Name':'Vicki','Last Name':'Jordan','Registered Email':'vicki.j @tacinc.com.au','Mobile':'03 6332 3800','MailSuburb':'','MailState':'','MailPostcode':'','Engagement':'attended an APNA event in the past and ventured onto our online education portal APNA Online Learning','Group':'Non-member'},{'Member ID':'15072','First Name':'Vicki','Last Name':'Jordan','Registered Email':'vicki.j @tacinc.com.au','Mobile':'03 6332 3800','MailSuburb':'','MailState':'','MailPostcode':'','Engagement':'attended an APNA event in the past and ventured onto our online education portal APNA Online Learning','Group':'Non-member'}]";

            //Using dynamic keyword with JsonConvert.DeserializeObject, here you need to import Newtonsoft.Json
            dynamic myObject = JsonConvert.DeserializeObject(myDynamicJSON);
           
            //Binding gridview from dynamic object
            grdJSON2Grid.DataSource = myObject;
            grdJSON2Grid.DataBind();

            //Using DataTable with JsonConvert.DeserializeObject, here you need to import using System.Data;
            DataTable myObjectDT = JsonConvert.DeserializeObject<DataTable>(myDynamicJSON);

            //Binding gridview from dynamic object
            grdJSON2Grid2.DataSource = myObjectDT;
            grdJSON2Grid2.DataBind();
       
        }




Thursday, October 17, 2013

What different between Var, Object and Dynamic data type in c#? How to use??

The data types (var, object and dynamic) are generally used while you are working
with anonymous type.

Var:
This is a data type, var data type introduced since .Net Framework 3.0. Var datatype is pre-determined at compilation time. We cannot leave it unassigned or having a null value we need to implicitly assign a value to it otherwise we`ll receive an error. Var does not require explicit type casting for any operation at run-time and we cannot change the type of these variables at run-time.

Object:
This is a root data type, object data type introduced since .Net Framework 2.0. Object data type is determined at run time. This is a alias for System.Object and object is the root of all classes (root data type).
In this we need to do boxing and unboxing to retrieve the actual value. The Object does not allow you to perform any operation such as mathematical operation without doing "boxing and unboxing". Its means we require explicit type casting for any operation at run-time. Useful when doesn't have more information about the data type.

Dynamic:
This is a data type, dynamic data type introduced since .Net Framework 4.0. The dynamic data type allows you to perform any operations and will be resolved at run time. It does not require explicit type casting for any operation at run-time, because it identifies the types at run-time only. Dynamic type can be passed as
function argument and function also can return object type. Useful when coding using reflection or dynamic language support or with the COM objects, because we require to write less amount of code.

Example Code:
namespace EDUDOTNET_Var_Object_Dynamic
{
    class Program
    {
        static void Main(string[] args)
        { 
            /// <summary>
            /// Example of var type
            /// </summary>            
            // assign a number value
            var varItem = 100;
            //does not require explicit type casting for any operation at runtime
            varItem = varItem + 100;
            //varItem ="EDUDOTNET";//We cannot change the type of these variables at runtime. 
 
            /// <summary>
            /// Example of object type
            /// </summary>
            // assign a number value
            object objItem = 100;
            //require explicit type casting for any operation at runtime
            objItem = (int)objItem + 100;
            //object type does not allows you to perform any operations 
            //objItem += 100; 
            //[Error: Operator '+=' cannot be applied to operands of type 'object' ana 'int']
            objItem = "EDUDOTNET";
 
            /// <summary>
            /// Example of dynamic type
            /// </summary>
            // assign a number value
            dynamic dynItem = 100;
            //does not require explicit type casting for any operation at runtime
            dynItem = dynItem + 100;
            // dynamic type allows you to perform any operations
            dynItem += 100;
            dynItem = "EDUDOTNET";
        }
    }
}



Tuesday, October 8, 2013

What is a Tuple in C#? When it use? Example of Tuple

Tuple:

Tuple is a generic static class that was added to C# 4.0 and it can hold any amount of elements, and they can be any type we want. So using tuple, we can return multiple values.One great use of tuple might be returning multiple values from a method. It provides an alternative to "ref" or "out" if you have a method that needs to return multiple new objects as part of its response.


A tuple allows you to combine multiple values of possibly different types into a single object without having to create a custom class. This can be useful if you want to write a method that for example returns three related values but you don't want to create a new class.

We can create a Tuple using the Create method. This method provide to 8 overloads, so we can use a maximum of 8 data types with a Tuple. 


Followings are overloads: 


Create(T1)- Tuple of size 1
Create(T1,T2)- Tuple of size 2
Create(T1,T2,T3) – Tuple of size 3
Create(T1,T2,T3,T4) – Tuple of size 4
Create(T1,T2,T3,T4,T5) – Tuple of size 5
Create(T1,T2,T3,T4,T5,T6) – Tuple of size 6
Create(T1,T2,T3,T4,T5,T6,T7) – Tuple of size 7
Create(T1,T2,T3,T4,T5,T6,T7,T8) – Tuple of size 8

Example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; 
namespace EDUDOTNET_TupleDemo
{
    class EDUDOTNET
    {
        static void Main(string[] args)
        {
            //This represents a tuple of size 3 (Create a new 3-tuple, or Triple) with all string type
            var tuple = Tuple.Create<string, string, string>("EDUDOTNET", "IT", "SOLUTION");
            Console.WriteLine(tuple); 
            //This represents a tuple of size 2 (Create a new 2-tuple, or Pair) with int & string type
            var tuple1 = Tuple.Create<int, string>(51, "CTO-EDUDOTNET");
            Console.WriteLine(tuple1);        
            
            //We can also access values of Tuple using ItemN property. Where N point to a particular item in the tuple.
            //This represents a tuple of size 4 (Create a new 4-tuple, or quadruple) with 3 string & 1 int type
            var tuple2 = Tuple.Create<int, string, string, string>(1, "Khumesh", "EDUDOTNET", "IT-Solution");
            Console.WriteLine(tuple2.Item1);
            Console.WriteLine(tuple2.Item2);
            Console.WriteLine(tuple2.Item3);
            Console.WriteLine(tuple2.Item4);
            Console.WriteLine("Enjoying With Tuple.......... :)");
            Console.ReadLine();
        }
    }
}