Click here to Skip to main content
15,902,939 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
hi i want to
retrive multiple records from one table in one label,
bind all cities from tbl_city table in one label.

tbl_cities
----------
ahmedabad
surat
bhavnagar
rajkot
delhi
mumbai
chennai
----------
label1.text

i want all cities in label1.text using datatable
Posted

Iterate through all the rows like the following :
C#
Label1.Text = string.Empty;
foreach(DataRow dr in dt_cities.Rows)
{
    Label1.Text += dr[0].ToString() +"\n";
}

-KR
 
Share this answer
 
Do ur work in sql server itself

SQL
select

 maskdetail = STUFF((
          SELECT ',' + p.Name
          FROM dbo.tbl_cities p

          FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, '')



label1.text = dt.rows[0][0].tostring();


Hope this helps
 
Share this answer
 
v3
Hi,
U can do it like this
C#
lblcity.Text=string.Empty;
        lblcity.Text += "All Cities"+"\n";
        lblcity.Text += "------------"+"\n";
        foreach (DataRow dr in dt.Rows)
        {
            lblcity.Text+=dr.ItemArray[0]+"\n";
        }
 
Share this answer
 
If you want to take data from datatable then follow Solution1


but if you want to get data from the SQL server and directly put it into label then follow the below snippet of SQL Query

Declare @strCity VARCHAR(MAX)
SELECT @strCity=COALESCE(@strCity + ',','') + City from Table1
PRINT @strCity


Put this code in your stored procedure and return it using out parameter.
 
Share this answer
 
Try this sample


C#
DataTable dt = new DataTable();
        dt.Columns.Add("City", typeof(string));
        dt.Rows.Add("ahmedabad");
        dt.Rows.Add("surat");
        dt.Rows.Add("bhavnagar");
        dt.Rows.Add("rajkot");
        dt.Rows.Add("delhi");

        string cities = "";
        string splitter = ",";
        foreach (DataRow row in dt.Rows)  
            cities += row["City"] + splitter;
        cities = cities.Trim(new char[] { ','});

        Label1.Text = cities;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900