locked
votefavorite I want to modify the following code so i bind the datatable to sqldatasorce RRS feed

  • Question

  • User803642803 posted

    I want to modify the following code so i bind the datatable to sqldatasorce

    Private Function GetTable() As DataTable 'You can store a DataTable in the session state Dim table As DataTable = TryCast(Session("Table"), DataTable) If table Is Nothing Then table = New DataTable()

            Dim colid As DataColumn = table.Columns.Add("ID", GetType(Int32))
            Dim nameid As DataColumn = table.Columns.Add("Name", GetType(String))
            table.PrimaryKey = New DataColumn() { colid }
            colid.ReadOnly = True
    
            For i As Integer = 0 To 22
                Dim aRow As DataRow = table.NewRow()
                aRow("ID") = i
                aRow("Name") = String.Format("Name{0}", i)
    
                table.Rows.Add(aRow)
            Next i
            Session("Table") = table
        End If
        Return table
    End Function
    Tuesday, September 20, 2016 6:19 PM

Answers

All replies

  • User-821857111 posted

    I want to modify the following code so i bind the datatable to sqldatasorce

    You can't "bind" a datatable to a sqldatasource. SqlDataSources are used to generate datatables. Do you mean that you want to bind the datatable to a data control? If so, you just need to set the DataSource property of the control to the GetTable() method:

    MyGridview.DataSource = GetTable();



    Wednesday, September 21, 2016 8:08 AM
  • User803642803 posted

    <br>
    Thank you. Your your help&lt;br&gt;<br>
    I want to replace datatabe with sqldatasorce in the code so i got the records from database and use it in session table &lt;br&gt;<br>
    Thank you&lt;br&gt;<br>
    &lt;br&gt;<br>
    Wednesday, September 21, 2016 8:24 PM
  • User36583972 posted

    Hi Ayman Qadah,

    I want to replace datatabe with sqldatasorce in the code so i got the records from database

    You can refer the following tutorial.

    Querying Data with the SqlDataSource Control (VB):

    http://www.asp.net/web-forms/overview/data-access/accessing-the-database-directly-from-an-aspnet-page/querying-data-with-the-sqldatasource-control-vb

    use it in session table

    You want to convert a SqlDataSource to a DataTable, then store in a session?

    How do I convert a SqlDataSource to a DataTable:

    http://forums.asp.net/t/1952396.aspx?How+do+I+convert+a+SqlDataSource+to+a+DataTable+

    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 26, 2016 6:56 AM