ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Handle Database Null values in jQuery DataTable Plugin in ASP.Net<p>Hi&nbsp;manionasp,</p> <p>Add <span style="text-decoration: underline;">defaultContent</span> in the options while initializing the DataTable plugin.</p> <p>Check this example. Now please take its reference and correct your code.</p> <p><strong><span style="text-decoration: underline;">Database</span></strong></p> <p>For this example I have used of Northwind database that you can download using the link given below.</p> <p><a href="https://www.aspsnippets.com/Articles/Install-Microsoft-Northwind-and-Pubs-Sample-databases-in-SQL-Server-Management-Studio.aspx">Download Northwind Database</a></p> <p><strong><span style="text-decoration: underline;">HTML</span></strong></p> <pre class="brush: html">&lt;div class=&#34;container&#34;&gt; &lt;asp:GridView ID=&#34;GridView1&#34; runat=&#34;server&#34; AutoGenerateColumns=&#34;false&#34; ClientIDMode=&#34;Static&#34;&gt; &lt;Columns&gt; &lt;asp:TemplateField HeaderText=&#34;ID&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label runat=&#34;server&#34; Text=&#39;&lt;%#Eval(&#34;CustomerID&#34;) %&gt;&#39;&gt; &lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;Name&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label runat=&#34;server&#34; Text=&#39;&lt;%#Eval(&#34;ContactName&#34;) %&gt;&#39;&gt; &lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;Region&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label runat=&#34;server&#34; Text=&#39;&lt;%#Eval(&#34;Region&#34;) %&gt;&#39;&gt; &lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;/Columns&gt; &lt;EmptyDataTemplate&gt; No Record Available &lt;/EmptyDataTemplate&gt; &lt;/asp:GridView&gt; &lt;/div&gt; &lt;script type=&#34;text/javascript&#34; src=&#34;https://code.jquery.com/jquery-1.10.2.js&#34;&gt;&lt;/script&gt; &lt;link rel=&#34;stylesheet&#34; href=&#34;https://cdn.datatables.net/1.10.12/css/jquery.dataTables.min.css&#34; /&gt; &lt;link rel=&#34;stylesheet&#34; href=&#34;https://cdn.datatables.net/buttons/1.2.2/css/buttons.dataTables.min.css&#34; /&gt; &lt;script src=&#34;http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.10.2.min.js&#34; type=&#34;text/javascript&#34;&gt;&lt;/script&gt; &lt;script type=&#34;text/javascript&#34; src=&#34;https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js&#34;&gt;&lt;/script&gt; &lt;script type=&#34;text/javascript&#34; src=&#34;https://cdn.datatables.net/buttons/1.2.2/js/dataTables.buttons.min.js&#34;&gt;&lt;/script&gt; &lt;script type=&#34;text/javascript&#34; src=&#34;https://cdnjs.cloudflare.com/ajax/libs/jszip/2.5.0/jszip.min.js&#34;&gt;&lt;/script&gt; &lt;script type=&#34;text/javascript&#34; src=&#34;https://cdn.rawgit.com/bpampuch/pdfmake/0.1.18/build/pdfmake.min.js&#34;&gt;&lt;/script&gt; &lt;script type=&#34;text/javascript&#34; src=&#34;https://cdn.rawgit.com/bpampuch/pdfmake/0.1.18/build/vfs_fonts.js&#34;&gt;&lt;/script&gt; &lt;script type=&#34;text/javascript&#34; src=&#34;https://cdn.datatables.net/buttons/1.2.2/js/buttons.html5.min.js&#34;&gt;&lt;/script&gt; &lt;script type=&#34;text/javascript&#34;&gt; $(document).ready(function () { $.ajax({ type: &#34;POST&#34;, url: &#39;&lt;%=ResolveUrl(&#34;Default.aspx/GetCustomers&#34;) %&gt;&#39;, dataType: &#34;json&#34;, data: {}, contentType: &#34;application/json&#34;, success: function (data) { displayTable(data.d); }, error: function (result) { alert(&#34;Error login&#34;); } }); }); function displayTable(data) { $(&#39;#GridView1&#39;).prepend($(&#34;&lt;thead&gt;&lt;/thead&gt;&#34;).append($(&#39;#GridView1&#39;).find(&#34;tr:first&#34;))).DataTable({ data: JSON.parse(data), &#34;columns&#34;: [ { &#34;data&#34;: &#34;CustomerID&#34;, &#34;defaultContent&#34;: &#34;&lt;b&gt;N/A&lt;/b&gt;&#34; }, { &#34;data&#34;: &#34;ContactName&#34;, &#34;defaultContent&#34;: &#34;&lt;b&gt;N/A&lt;/b&gt;&#34; }, { &#34;data&#34;: &#34;Region&#34;, &#34;defaultContent&#34;: &#34;&lt;b&gt;N/A&lt;/b&gt;&#34; } ] }); } &lt;/script&gt;</pre> <p><strong><span style="text-decoration: underline;">Namespaces</span></strong></p> <pre class="brush: csharp">using System.Configuration; using System.Data.SqlClient; using System.Data; using Newtonsoft.Json; using System.Web.Services;</pre> <p><strong><span style="text-decoration: underline;">Code</span></strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { DataTable dt = GetData().Clone(); dt.Rows.Add(); GridView1.DataSource = dt; GridView1.DataBind(); } } [WebMethod(EnableSession = true)] public static string GetCustomers() { string JSONString = string.Empty; JSONString = JsonConvert.SerializeObject(GetData()); return JSONString; } private static DataTable GetData() { string conString = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; string query = &#34;SELECT CustomerID,ContactName,Region FROM Customers&#34;; SqlCommand cmd = new SqlCommand(query); using (SqlConnection con = new SqlConnection(conString)) { using (SqlDataAdapter sda = new SqlDataAdapter()) { cmd.Connection = con; sda.SelectCommand = cmd; using (DataTable dt = new DataTable()) { sda.Fill(dt); return dt; } } } }</pre> <p><strong><span style="text-decoration: underline;">Screenshot</span></strong></p> <p><strong><span style="text-decoration: underline;"><img src="https://imgur.com/wJ4TKQ3.jpg" alt="" width="472" height="454" /></span></strong></p>https://www.aspforums.net:443/Threads/147615/Handle-Database-Null-values-in-jQuery-DataTable-Plugin-in-ASPNet/https://www.aspforums.net:443/Threads/147615/Handle-Database-Null-values-in-jQuery-DataTable-Plugin-in-ASPNet/Fri, 26 Oct 2018 04:01:08 GMT