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;
}
|
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
|
<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>
|
using System;
using System.Data;
using System.Data.SqlClient;
|
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;
}
|
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 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 Email
|
|||
|
|
Subscribe by RSS
Subscribe by Email
0 comments :