Wednesday, October 24, 2012

Object doesn't support property or method call in consuming JSON object from ASP.NET MVC (IE9)

Recently,
I've followed a simple tutorial regarding consuming JSON object in ASP.NET MVC. The error stated from the title of this post appears in IE9. The original snippet is this:
1:  $(document).ready(  
2:      $.getJSON(  
3:        'http://localhost:6222/home/customerjson',  
4:         function (item) {  
5:           $('#result') //show product name in   
6:            .html('<p>' + item.CurrentCustomer.CustomerName + '</p>');  
7:         }  
8:      )  
9:    );  
The solution is to enclose $.getJSON() in a function() statement based from the modified code below. The popup script just went away.
1:  $(document).ready(function(){  
2:        $.getJSON(  
3:          'http://localhost:6222/home/customerjson',  
4:           function (item) {  
5:             $('#result') //show product name in   
6:              .html('<p>' + item.CurrentCustomer.CustomerName + '</p>');  
7:           }  
8:        )  
9:      });   
Cheers!

Friday, October 12, 2012

Eliminate duplicate rows in a Datatable

Assuming you have a datatable that has merged contents from different datatables and you want to eliminate the datatable with distinct rows, you can use the statement below.
Code:
dt.DefaultView.ToTable(true, new string[] { "id", "name" }); 
Cheers!

Wednesday, October 3, 2012

Column 'column_name' already belongs to another DataTable

In a scenario where I added columns using AddRange(), I encountered an error as specified by the title.
Here's the C# code:
Code:
table1.Columns.AddRange(new DataColumn[] { col1, col2, col3 });  
table2.Columns.AddRange(new DataColumn[] { col1, col2, col3 });
Where col1, col2, col3 are DataColumn objects.
What I did was to initialize columns in the addrange method. But there are other solutions in the forums.
Code:
table1.Columns.AddRange(new DataColumn[] { new DataColumn("ID"), new DataColumn("WEB ID"), new DataColumn("URL") });  
table2.Columns.AddRange(new DataColumn[] { new DataColumn("ID"), new DataColumn("WEB ID"), new DataColumn("URL") });