Click here to Skip to main content
15,893,486 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Actually in my code i am bringing two tables from stored proc and after that i am assigning that two tables to data set
now i want to assign first table to one data table and second table to another data table in my asp.net code. Is it possible.
Posted

Yes it is possible
DataSet can contain More then one table
you can create DataTable object and assign DataSet Table to DataTable object
C#
DataTable dt1=DataSetObj.Tables[0];
DataTable dt2=DataSetObj.Tables[1];
 
Share this answer
 
VB
Public Partial Class WebForm7
    Inherits System.Web.UI.Page
    Public oCn As New System.Data.SqlClient.SqlConnection("Data Source=(local);Initial Catalog=MyDatabase;Uid=sa")

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        fill_data()
    End Sub

    Sub fill_data()
        If oCn.State = ConnectionState.Closed Then
            oCn.Open()
        End If
        Dim dt As New DataTable

        Dim cmd As New SqlClient.SqlCommand("select * from mst_employees", oCn)
        Dim da As New SqlClient.SqlDataAdapter(cmd)
        Dim ds As New DataSet("bpl")
        Dim i As Integer = 0
        dt.Columns.Add(0)
        dt.Columns.Add(1)
        dt.Columns.Add(2)
        dt.Columns.Add(3)
        da.Fill(ds, "bpl")
        If ds.Tables(0).Rows.Count > 0 Then
            While (i <> ds.Tables(0).Rows.Count)
                dt.Rows.Add()
                dt.Rows(i).Item(0) = ds.Tables(0).Rows(i).Item(0).ToString
                dt.Rows(i).Item(1) = ds.Tables(0).Rows(i).Item(1).ToString
                dt.Rows(i).Item(2) = ds.Tables(0).Rows(i).Item(2).ToString
                dt.Rows(i).Item(3) = ds.Tables(0).Rows(i).Item(3).ToString
                i = i + 1
            End While
        End If
        Me.GridView1.DataSource = dt
        Me.GridView1.DataBind()
        oCn.Close()
    End Sub
End Class
 
Share this answer
 
you can access your dataset tables to assign it to anther table like this...


suppose your dataset alias is Ds then you can use something like this...


C#
DataTable dt1 = Ds.Tables[0];
DataTable dt2 = Ds.Tables[1];
 
Share this answer
 
Try this..

C#
DataSet ds = new DataSet();

       DataTable dtTable1 = new DataTable();
       DataTable dtTable2 = new DataTable();


       ds.Tables.Add(dtTable1);
       ds.Tables.Add(dtTable2);


Regards..

Nirav Prabtani

 
Share this answer
 
v2

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