Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Add datatable data with comma separated values in string

Jul 6, 2012
Introduction:

Here I will explain how to add or bind datatable column data with comma separated values in asp.net.

Description:

In previous article I explained about self join in SQL server, Joins in SQL Server and many more articles relating to SQL Server. Now I will explain how to add datatable data with comma separated values in asp.net.

If we want to bind the datatable column data with comma operator we need to write syntax like this

C# Code


DataTable dt = new DataTable();
string str = string.Empty;
for (int i = 0; i < dt.Rows.Count; i++)
{
str = str + dt.Rows[i]["UserName"].ToString();
str += (i < dt.Rows.Count-1) ? "," : string.Empty;
}
VB.NET Code


Dim dt As New DataTable()
Dim str As String = String.Empty
For i As Integer = 0 To dt.Rows.Count - 1
str = str & dt.Rows(i)("UserName").ToString()
str += If((i < dt.Rows.Count - 1), ",", String.Empty)
Next
Now I will explain this with one with simple example for that first design your aspx page like this


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Bind or Add Datatable data with Comma Separated values</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblUserName" runat="server" />
</div>
</form>
</body>
</html>
Now open code behind page and add the following namespace references


using System;
using System.Data;
using System.Data.SqlClient;
After add namespaces write the following code in code behind

C# code


protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
BindData();
}
}
private void BindData()
{
SqlConnection con = new SqlConnection("Data Source=SureshDasari;Initial Catalog=MySampleDB;Integrated Security=true");
con.Open();
SqlCommand cmd = new SqlCommand("SELECT UserName FROM UserDetails",con);
SqlDataAdapter da=new SqlDataAdapter(cmd);
DataSet ds=new DataSet();
DataTable dt=new DataTable();
da.Fill(ds);
dt = ds.Tables[0];
string str = string.Empty;
for (int i = 0; i < dt.Rows.Count; i++)
{
str = str + dt.Rows[i]["UserName"].ToString();
str += (i < dt.Rows.Count-1) ? "," : string.Empty;
}
lblUserName.Text = str;
}
VB.NET Code

Imports System.Data
Imports System.Data.SqlClient
Partial Class Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

If Not IsPostBack Then
BindData()
End If
End Sub
Private Sub BindData()
Dim con As New SqlConnection("Data Source=SureshDasari;Initial Catalog=MySampleDB;Integrated Security=true")
con.Open()
Dim cmd As New SqlCommand("SELECT UserName FROM UserDetails", con)
Dim da As New SqlDataAdapter(cmd)
Dim ds As New DataSet()
Dim dt As New DataTable()
da.Fill(ds)
dt = ds.Tables(0)
Dim str As String = String.Empty
For i As Integer = 0 To dt.Rows.Count - 1
str = str & dt.Rows(i)("UserName").ToString()
str += If((i < dt.Rows.Count - 1), ",", String.Empty)
Next
lblUserName.Text = str
End Sub
End Class
 If we run above code we will get output like this

Output


If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

2 comments :

Unknown said...

Add datatable data with space separated values in string.

my code is:-
string collection = "";
for (int i = 0; i < CheckBoxList3.Items.Count; i++)
{
if (CheckBoxList3.Items[i].Selected)
{
collection += CheckBoxList3.Items[i].Value + "Collection,";
}
}
collection = collection.TrimEnd(',');
cmd.Parameters.AddWithValue("@selectcollection", collection);
Response.Write(collection);

checkbox checked value and collection string added but space like... prashant space collection

plz help me

Anas Ali said...

Thank you for this wonderful article. I have been in the last two days looking for this solution Thank you again

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.