question

MansoorAhmed-0534 avatar image
0 Votes"
MansoorAhmed-0534 asked surferonwww answered

How To Display JSON Data Into GridView

Dear Experts, I Have This Json Data From Firebase Real Time Data Base.

Could You Please Show Me A Single Example How Can I Display This Data Into GridView C# Without KEY.

 {
     "-MtmDfxQyVwkr7SkHsrg": {
         "amount": "500000",
         "branch_num": "001",
         "machineID": "VB12213C20156",
         "shift": 1,
         "userID": "4245",
         "voucher_date": "2022-01-19T15:05:46",
         "voucher_number": "156202200001"
     },
     "-MtmE1Y54t3G_wLTuICr": {
         "amount": "50000",
         "branch_num": "001",
         "machineID": "VB12213C20156",
         "shift": 1,
         "userID": "4245",
         "voucher_date": "2022-01-19T15:07:22",
         "voucher_number": "156202200002"
     },
     "-MtmEilHXO54Qqai1CGx": {
         "amount": "125000.0",
         "branch_num": "001",
         "machineID": "VB12213C20156",
         "shift": 2,
         "userID": "4245",
         "voucher_date": "2022-01-19T15:10:26",
         "voucher_number": "156202200003"
     },
     "-MtpxcPYxmXumoqATUFi": {
         "amount": "568847.0",
         "branch_num": "001",
         "machineID": "VB12213C20156",
         "shift": 2,
         "userID": "108",
         "voucher_date": "2022-01-20T08:29:36",
         "voucher_number": "156202200004"
     },
     "-MtpxwVLqAOpVaigM7bU": {
         "amount": "568847.0",
         "branch_num": "001",
         "machineID": "VB12213C20156",
         "shift": 2,
         "userID": "108",
         "voucher_date": "2022-01-20T08:31:04",
         "voucher_number": "156202200005"
     },
     "-Mtq-AYxYykwtl0Ff1mH": {
         "amount": "4658000.0",
         "branch_num": "001",
         "machineID": "VB12213C20156",
         "shift": 2,
         "userID": "108",
         "voucher_date": "2022-01-20T08:40:55",
         "voucher_number": "156202200006"
     },
     "-Mtq-bB71DcjDgF0TeMl": {
         "amount": "4658000.0",
         "branch_num": "001",
         "machineID": "VB12213C20156",
         "shift": 2,
         "userID": "108",
         "voucher_date": "2022-01-20T08:42:52",
         "voucher_number": "156202200007"
     },
     "-Mu54ozWi8hO6_lpkaeC": {
         "amount": "100000.0",
         "branch_num": "001",
         "machineID": "VB12213C20156",
         "shift": 1,
         "userID": "7845",
         "voucher_date": "2022-01-23T11:37:47",
         "voucher_number": "156202200008"
     },
     "-Mu55GfMw0puE2185qxT": {
         "amount": "1000000.0",
         "branch_num": "001",
         "machineID": "VB12213C20156",
         "shift": 1,
         "userID": "7845",
         "voucher_date": "2022-01-23T11:41:34",
         "voucher_number": "156202200009"
     },
     "-Mu55hfHxhEHQc-FAUHP": {
         "amount": "1.2000050",
         "branch_num": "001",
         "machineID": "VB12213C20156",
         "shift": 2,
         "userID": "4245",
         "voucher_date": "2022-01-23T11:43:19",
         "voucher_number": "156202200010"
     },
     "-Mu56Qk6Nmfu1phGA-Uh": {
         "amount": "120050.5",
         "branch_num": "001",
         "machineID": "VB12213C20156",
         "shift": 2,
         "userID": "4245",
         "voucher_date": "2022-01-23T11:46:30",
         "voucher_number": "156202200011"
     },
     "-Mu9yUlTL6UdVgUEFQ3Z": {
         "amount": "125000.0",
         "branch_num": "001",
         "machineID": "VB12213C20156",
         "shift": 2,
         "userID": "4245",
         "voucher_date": "2022-01-24T10:18:30",
         "voucher_number": "156202200012"
     },
     "-MuPAXq2OWEiEfOYLOvc": {
         "amount": "784569.0",
         "branch_num": "001",
         "machineID": "VB12213C20156",
         "shift": 1,
         "userID": "7845",
         "voucher_date": "2022-01-27T09:13:27",
         "voucher_number": "156202200013"
     },
     "-MuPMd1HznpTRwNlSDbS": {
         "amount": "4587126.0",
         "branch_num": "005",
         "machineID": "VB12213C20156",
         "shift": 2,
         "userID": "4245",
         "voucher_date": "2022-01-27T09:36:51",
         "voucher_number": "156202200014"
     },
     "-MuPW5cMw4ICK3ZoiHZC": {
         "amount": "5959.0",
         "branch_num": "004",
         "machineID": "VB12213C20156",
         "shift": 1,
         "userID": "7845",
         "voucher_date": "2022-01-27T10:50:48",
         "voucher_number": "156202200015"
     },
     "-MuPYJ53-EGGPzWvszhy": {
         "amount": "568932.0",
         "branch_num": "005",
         "machineID": "VB12213C20156",
         "shift": 1,
         "userID": "7845",
         "voucher_date": "2022-01-27T11:00:35",
         "voucher_number": "156202200016"
     },
     "-MuPh0RFdkXA6Jw5wYxp": {
         "amount": "10000.0",
         "branch_num": "001",
         "machineID": "VB12213C20156",
         "shift": 2,
         "userID": "7",
         "voucher_date": "2022-01-27T11:42:56",
         "voucher_number": "156202200017"
     }
 }
dotnet-aspnet-webforms
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

YijingSun-MSFT avatar image
0 Votes"
YijingSun-MSFT answered

Hi @MansoorAhmed-0534 ,
According to your description,you can not deserialize your json directly to a datatable. Your data sits deep inside the hierachical structure, so you have to do a bit more work to convert this:

 public DataTable jsonDataDiplay()
 {
     StreamReader sr = new StreamReader(Server.MapPath("TrainServiceAlerts.json"));
     string json = sr.ReadToEnd();
     dynamic table = JsonConvert.DeserializeObject(json);
     DataTable newTable = new DataTable();
     newTable.Columns.Add("amount", typeof(string));
     newTable.Columns.Add("branch_num", typeof(string));
     newTable.Columns.Add("machineID", typeof(string));
     newTable.Columns.Add("shift", typeof(string));
     newTable.Columns.Add("userID", typeof(string));
     newTable.Columns.Add("voucher_date", typeof(string));
     newTable.Columns.Add("voucher_number", typeof(string));
    
     foreach (var row in table.value.data)
     {
         newTable.Rows.Add(row.amount, row.branch_num, row.machineID, row.shift,row.userID,row.voucher_date,row.voucher_number);
     }
     return newTable;
 }
 GridView.DataSource = newTable ;
 GridView.DataBind();

Best regards,
Yijing Sun


If the answer is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our  documentation  to enable e-mail notifications if you want to receive the related email notification for this thread.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MansoorAhmed-0534 avatar image
0 Votes"
MansoorAhmed-0534 answered YijingSun-MSFT edited

Is There Any Way That Can Achieve Something Like That Using URL Not A File From Server.

  **StreamReader sr = new StreamReader(Server.MapPath("https://receipt-voucher-default-rtdb.firebaseio.com/receipts.json));**

      StreamReader sr = new StreamReader(Server.MapPath("TrainServiceAlerts.json"));





· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @MansoorAhmed-0534 ,
You need to use WebClient to read the url:

 WebClient client = new WebClient();
 Stream stream = client.OpenRead("http://localhost:81/c/");
 StreamReader reader = new StreamReader(stream);
 string str= reader.ReadToEnd();

Best regards,
Yijing Sun

0 Votes 0 ·

This Error Comes After Using The Above Code


 Server Error in '/' Application.
 Cannot perform runtime binding on a null reference
 Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
    
 Exception Details: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Cannot perform runtime binding on a null reference
    
 Source Error:
    
    
 Line 37:       newTable.Columns.Add("voucher_number", typeof(string));
 Line 38: 
 Line 39:       foreach (var row in table.value.data)
 Line 40:       {
 Line 41:         newTable.Rows.Add(row.amount, row.branch_num,
0 Votes 0 ·

Hi @MansoorAhmed-0534 ,
I suggest you need to debug and break point at these codes. Your problem is that you have a null value. Do you have voucher_number nodes?
Best regards,
Yijing Sun

0 Votes 0 ·
JoseZero-8614 avatar image
0 Votes"
JoseZero-8614 answered MansoorAhmed-0534 commented

@MansoorAhmed-0534
WebClient provides an simple way to get a string from URL, sample should return your external IP address like {"ip":"aaa.bbb.ccc.ddd"}
system.net.webclient

 System.Net.WebClient MyClient = new System.Net.WebClient();
 string MyString = null;
 try
 {
   MyString = MyClient.DownloadString("https://api.ipify.org/?format=json");
 }
 catch (System.Net.WebException ex)
 {
   MyString = "Error: " + ex.Message;
 }


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

This Error Comes After Using The Above Code


 Server Error in '/' Application.
 Cannot perform runtime binding on a null reference
 Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
    
 Exception Details: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Cannot perform runtime binding on a null reference
    
 Source Error:
    
    
 Line 37:       newTable.Columns.Add("voucher_number", typeof(string));
 Line 38: 
 Line 39:       foreach (var row in table.value.data)
 Line 40:       {
 Line 41:         newTable.Rows.Add(row.amount, row.branch_num,
0 Votes 0 ·
MansoorAhmed-0534 avatar image
0 Votes"
MansoorAhmed-0534 answered vigneshs-5589 published
 using Newtonsoft.Json;
 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Net;
 using System.Web;
 using System.Web.UI;
 using System.Web.UI.WebControls;
    
 namespace _5M_Solution
 {
   public partial class Harqat_Maqbozat_Grid : System.Web.UI.Page
   {
     protected void Page_Load(object sender, EventArgs e)
     {
       if (!this.IsPostBack)
       {
         this.GetJsonData();
       }
     }
    
     public void GetJsonData()
     {
       ServicePointManager.Expect100Continue = true;
       ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
       string json = (new WebClient()).DownloadString("https://receipt-voucher-default-rtdb.firebaseio.com/receipts.json");
       Dictionary<string, object> values = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
       List<Receipt> receipts = new List<Receipt>();
       foreach (var entry in values)
       {
         receipts.Add(JsonConvert.DeserializeObject<Receipt>(entry.Value.ToString()));
       }
       gvDetails.DataSource = receipts;
       gvDetails.DataBind();
     }
    
     public class Receipt
     {
       public string amount { get; set; }
       public string branch_num { get; set; }
       public string machineID { get; set; }
       public string shift { get; set; }
       public string userID { get; set; }
       public string voucher_date { get; set; }
       public string voucher_number { get; set; }
     }
    
     public void ExportExcel()
     {
       try
       {
         Response.Clear();
         Response.Buffer = true;
         Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "5M_MaqbozatDetails.xls"));
         Response.ContentType = "application/ms-excel";
         Response.Charset = "";
         this.EnableViewState = false;
         System.IO.StringWriter writer = new System.IO.StringWriter();
         System.Web.UI.HtmlTextWriter html = new System.Web.UI.HtmlTextWriter(writer);
         GetJsonData();
         //dl_JsonDetails.DataBind();
         gvDetails.RenderControl(html);
         Response.Write(writer.ToString());
         Response.Flush();
         Response.End();
       }
       catch (Exception ex)
       {
       }
     }
    
     protected void btn_ExportExcel_Click(object sender, EventArgs e)
     {
       ExportExcel();
     }
    
     protected void gvDetails_PageIndexChanging(object sender, GridViewPageEventArgs e)
     {
       gvDetails.PageIndex = e.NewPageIndex;
       this.GetJsonData();
     }
    
     protected void dd_SearchByBranchNo_SelectedIndexChanged(object sender, EventArgs e)
     {
         
     }
   }
 }
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @MansoorAhmed-0534 ,
So,do you have solved your problems? What's wrong with your above error messages? Which lines have the errors?
Best regards,
Yijing Sun

0 Votes 0 ·

Yes Finally It Has Been Resolved. But I Am Not The One Who Solved It. There Is Another Forum Where I Post The Question And They solve This Problem. Thanks For Your Help Also.

0 Votes 0 ·
vigneshs-5589 avatar image vigneshs-5589 MansoorAhmed-0534 ·

hi @MansoorAhmed-0534 could pls tell In which Forum !

0 Votes 0 ·
surferonwww avatar image
0 Votes"
surferonwww answered

I suggest that you use the DynamicJson to deserialize the JSON string and to obtain the data source of List<T> which can be shown on the GridView.

226396-dynamicjson.jpg

A sample code:

 using System;
 using System.Collections.Generic;
 using System.IO;
 using Codeplex.Data;
    
 namespace DynamicJsonSample
 {
     internal class Program
     {
         static void Main(string[] args)
         {
             string filepath = @"C:\Users\surfe\Documents\Visual Studio 2022\CsProject\DynamicJson\DynamicJson\JsonFile.json";
             string jsonText = "";
             List<Item> list = new List<Item>();
    
             using (StreamReader sr = File.OpenText(filepath))
             {
                 jsonText = sr.ReadToEnd();
             }
    
             var json = DynamicJson.Parse(jsonText);
    
             foreach (KeyValuePair<string, dynamic> kvp in json)
             {
                 Item item = new Item
                 {
                     amount = kvp.Value.amount,
                     branch_num = kvp.Value.branch_num,
                     machineID = kvp.Value.machineID,
                     shift = (int)kvp.Value.shift,
                     userID = kvp.Value.userID,
                     voucher_date = DateTime.Parse(kvp.Value.voucher_date),
                     voucher_number = kvp.Value.voucher_number
                 };
                 list.Add(item);
             }
         }
     }
    
     public class Item
     {
         public string amount { get; set; }
         public string branch_num { get; set; }
         public string machineID { get; set; }
         public int shift { get; set; }
         public string userID { get; set; }
         public DateTime voucher_date { get; set; }
         public string voucher_number { get; set; }
     }


Result:

226409-result.jpg





dynamicjson.jpg (32.0 KiB)
result.jpg (201.6 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.