Save (Insert) database records into Array using C# and VB.Net in ASP.Net

Last Reply 7 months ago By dharmendr

Posted 7 months ago

my table data is 

usid     clgid

3          5  

5          6

3           7

3           10   

i want to usid=3  clgid in array with space seprate ======================

dsddsds.Tables.Add(obj.select1("Select * from fav_collage where user_id='" + Request.Cookies["user_login"].Value + "'"));
foreach (DataRow cr in dsddsds.Tables[0].Rows)
{ 
    string[] whislist=cr["clgid"].ToString(); 
    right_hidden.Value = String.Join(" ", whislist);
}

but string error show please 

Posted 7 months ago

Hi ps222,

Use ArrayList and insert the clgid to it.

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

Namespaces

C#

using System.Data;
using System.Collections;

VB.Net

Imports System.Data

Code

C#

protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[] { new DataColumn("usid", typeof(int)),
                            new DataColumn("clgid", typeof(int)) });
        dt.Rows.Add(3, 5);
        dt.Rows.Add(5, 6);
        dt.Rows.Add(3, 7);
        dt.Rows.Add(3, 10);
        DataSet dsddsds = new DataSet();
        dsddsds.Tables.Add(dt.Select("usid=" + 3).CopyToDataTable());
        ArrayList whislist = new ArrayList();
        foreach (DataRow cr in dsddsds.Tables[0].Rows)
        {
            whislist.Add(cr["clgid"].ToString());
        }

        Response.Write(String.Join(" ", whislist.ToArray()));
    }

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    Dim dt As DataTable = New DataTable()
    dt.Columns.AddRange(New DataColumn() {New DataColumn("usid", GetType(Integer)), New DataColumn("clgid", GetType(Integer))})
    dt.Rows.Add(3, 5)
    dt.Rows.Add(5, 6)
    dt.Rows.Add(3, 7)
    dt.Rows.Add(3, 10)
    Dim dsddsds As DataSet = New DataSet()
    dsddsds.Tables.Add(dt.[Select]("usid=" & 3).CopyToDataTable())
    Dim whislist As ArrayList = New ArrayList()
    For Each cr As DataRow In dsddsds.Tables(0).Rows
        whislist.Add(cr("clgid").ToString())
    Next
    Response.Write(String.Join(" ", whislist.ToArray()))
End Sub

Output

5 7 10