Display DataSet (DataTable) all Row value in single TextBox using C# and VB.Net in ASP.Net

Last Reply 7 months ago By dharmendr

Posted 7 months ago

How to get the dataset row values in single textbox  based on condition?

id nAME DEPT
1 SUJAY IT
2 KANNAN CSE
3 SEKAR ECE
4 SHIVA MECH

It should run like this but using loop condition for every id

I want to get the id of each row in textbox using looping condition

You are viewing reply posted by: dharmendr 7 months ago.
Posted 7 months ago

Hi SUJAYS,

Check this example. Now please take its reference and correct your code.

HTML

<asp:TextBox runat="server" ID="txtIds" />

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    System.Data.DataTable dt = new System.Data.DataTable();
    dt.Columns.AddRange(new System.Data.DataColumn[] { 
                        new System.Data.DataColumn("Id", typeof(int)),
                        new System.Data.DataColumn("Name", typeof(string)),
                        new System.Data.DataColumn("Dept",typeof(string)) });
    dt.Rows.Add(1, "SUJAY", "IT");
    dt.Rows.Add(2, "KANNAN", "CSE");
    dt.Rows.Add(3, "SEKAR", "ECE");
    dt.Rows.Add(4, "SHIVA", "MECH");
    System.Data.DataSet ds = new System.Data.DataSet();
    ds.Tables.Add(dt);

    string ids = "";
    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
    {
        ids += ds.Tables[0].Rows[i]["Id"].ToString() + ",";
    }
    ids = ids.TrimEnd(',');
    txtIds.Text = ids;
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    Dim dt As System.Data.DataTable = New System.Data.DataTable()
    dt.Columns.AddRange(New System.Data.DataColumn() {
                        New System.Data.DataColumn("Id", GetType(Integer)),
                        New System.Data.DataColumn("Name", GetType(String)),
                        New System.Data.DataColumn("Dept", GetType(String))})
    dt.Rows.Add(1, "SUJAY", "IT")
    dt.Rows.Add(2, "KANNAN", "CSE")
    dt.Rows.Add(3, "SEKAR", "ECE")
    dt.Rows.Add(4, "SHIVA", "MECH")
    Dim ds As System.Data.DataSet = New System.Data.DataSet()
    ds.Tables.Add(dt)
    Dim ids As String = ""
    For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
        ids += ds.Tables(0).Rows(i)("Id").ToString() & ","
    Next
    ids = ids.TrimEnd(","c)
    txtIds.Text = ids
End Sub

Screenshot