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.Replace comma separated id with values from Database in C# and VB.Net in ASP.Net<p>Hi&nbsp;chetan,</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;asp:GridView runat=&#34;server&#34; ID=&#34;gvEmployees&#34; AutoGenerateColumns=&#34;false&#34;&gt; &lt;Columns&gt; &lt;asp:BoundField DataField=&#34;Id&#34; HeaderText=&#34;Id&#34; /&gt; &lt;asp:BoundField DataField=&#34;Companyname&#34; HeaderText=&#34;Company Name&#34; /&gt; &lt;asp:BoundField DataField=&#34;Datalist&#34; HeaderText=&#34;Exhibitorlist&#34; /&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt;</pre> <p><strong><span style="text-decoration: underline;">Namespaces</span></strong></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Configuration; using System.Data; using System.Data.SqlClient;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Configuration Imports System.Data Imports System.Data.SqlClient</pre> <p><strong><span style="text-decoration: underline;">Code</span></strong></p> <p><strong>C#</strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[3] { new DataColumn(&#34;Id&#34;, typeof(int)), new DataColumn(&#34;Companyname&#34;, typeof(string)), new DataColumn(&#34;Datalist&#34;,typeof(string)) }); dt.Rows.Add(1, &#34;abc&#34;, &#34;1,2,3&#34;); dt.Rows.Add(2, &#34;def&#34;, &#34;4,5,7&#34;); dt.Rows.Add(3, &#34;ghi&#34;, &#34;&#34;); dt.Rows.Add(4, &#34;jkl&#34;, &#34;8,10,11&#34;); for (int i = 0; i &lt; dt.Rows.Count; i++) { string coun = dt.Rows[i][&#34;Datalist&#34;].ToString(); if (coun == &#34;&#34; || coun == null) { dt.Rows[i][&#34;Datalist&#34;] = 0; } else { string conString = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; SqlConnection con = new SqlConnection(conString); string query = &#34;SELECT STUFF((SELECT N&#39;,&#39; + FirstName FROM Employees WHERE EmployeeID in(&#34; + coun + &#34;) FOR XML PATH(&#39;&#39;),TYPE).value(&#39;text()[1]&#39;,&#39;NVARCHAR(MAX)&#39;),1,1,N&#39;&#39;) Exhibitorlist&#34;; SqlCommand cmd = new SqlCommand(query, con); con.Open(); dt.Rows[i][&#34;Datalist&#34;] = cmd.ExecuteScalar().ToString(); con.Close(); } } gvEmployees.DataSource = dt; gvEmployees.DataBind(); } }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load If Not Me.IsPostBack Then Dim dt As DataTable = New DataTable() dt.Columns.AddRange(New DataColumn(2) {New DataColumn(&#34;Id&#34;, GetType(Integer)), New DataColumn(&#34;Companyname&#34;, GetType(String)), New DataColumn(&#34;Datalist&#34;, GetType(String))}) dt.Rows.Add(1, &#34;abc&#34;, &#34;1,2,3&#34;) dt.Rows.Add(2, &#34;def&#34;, &#34;4,5,7&#34;) dt.Rows.Add(3, &#34;ghi&#34;, &#34;&#34;) dt.Rows.Add(4, &#34;jkl&#34;, &#34;8,10,11&#34;) For i As Integer = 0 To dt.Rows.Count - 1 Dim coun As String = dt.Rows(i)(&#34;Datalist&#34;).ToString() If coun = &#34;&#34; OrElse coun Is Nothing Then dt.Rows(i)(&#34;Datalist&#34;) = 0 Else Dim conString As String = ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString Dim con As SqlConnection = New SqlConnection(conString) Dim query As String = &#34;SELECT STUFF((SELECT N&#39;,&#39; + FirstName FROM Employees WHERE EmployeeID in(&#34; &amp; coun &amp; &#34;) FOR XML PATH(&#39;&#39;),TYPE).value(&#39;text()[1]&#39;,&#39;NVARCHAR(MAX)&#39;),1,1,N&#39;&#39;) Exhibitorlist&#34; Dim cmd As SqlCommand = New SqlCommand(query, con) con.Open() dt.Rows(i)(&#34;Datalist&#34;) = cmd.ExecuteScalar().ToString() con.Close() End If Next gvEmployees.DataSource = dt gvEmployees.DataBind() End If End Sub</pre> <p><strong><span style="text-decoration: underline;">Screenshot</span></strong></p> <p><strong><span style="text-decoration: underline;"><img src="https://i.imgur.com/Ow0IlRs.jpg" alt="" width="344" height="222" /></span></strong></p>https://www.aspforums.net:443/Threads/425244/Replace-comma-separated-id-with-values-from-Database-in-C-and-VBNet-in-ASPNet/https://www.aspforums.net:443/Threads/425244/Replace-comma-separated-id-with-values-from-Database-in-C-and-VBNet-in-ASPNet/Fri, 14 Dec 2018 04:22:42 GMT