Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Calling a datatable

    I have created a datatable/tableadapters using the designer and would like to know how I fill/refer to this datatable in code.

    ASP.NET 4 / VB

    Please help.
    Last edited by lestat0109; 07-24-2012 at 02:09 PM.

  • #2
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    96 views and no response? Im a little worried that my being new to ASP.net means I worded my question improperly.

  • #3
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,760
    Thanks
    41
    Thanked 191 Times in 190 Posts
    It's more of a "don't know what you are asking" .... no code offered and do not know what it is you're trying to do...

    loading a datatable with data is done by...

    Code:
    Dim connstring as String = "... connectionstring.com ... "
    Dim _Query as String = "SELECT * FROM tbl_Demo
    
    Dim dt as new DataTable()
    
    Dim conn as new SqlConnection(connstring)
    Dim da as new SqlDataAdapter(_Query,conn)
    
    conn.Open()
    da.Fill(dt)
    conn.Close()
    Refs:
    datatables
    dataadapters

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #4
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by alykins View Post
    It's more of a "don't know what you are asking" .... no code offered and do not know what it is you're trying to do...

    loading a datatable with data is done by...

    Code:
    Dim connstring as String = "... connectionstring.com ... "
    Dim _Query as String = "SELECT * FROM tbl_Demo
    
    Dim dt as new DataTable()
    
    Dim conn as new SqlConnection(connstring)
    Dim da as new SqlDataAdapter(_Query,conn)
    
    conn.Open()
    da.Fill(dt)
    conn.Close()
    Refs:
    datatables
    dataadapters
    Ah, gotcha. Ill elaborate. I have already set the connection string in the dataset designer. I have already created a query in the tableadapter for the information i would like to fill the datatable with. I have a function that requires that I pass a datatable to it and i have no idea how to point that function to the datatable i have created in the designer, rather than recreate the whole thing in code only. (like your example above) The datatable is called dtTest and the tableadapter is called taTest.
    Last edited by lestat0109; 07-25-2012 at 02:47 PM.

  • #5
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,760
    Thanks
    41
    Thanked 191 Times in 190 Posts
    Code:
    private Sub something()
    .....
    Dim dt as new DataTable()
    FillMyTable(dt)
    CallSomething(dt)
    ......
    End Sub
    
    private Sub FillMyTable(ByVal dt as DataTable)
    
      Dim connstring as String = "... connectionstring.com ... "
      Dim _Query as String = "SELECT * FROM tbl_Demo
    
      Dim conn as new SqlConnection(connstring)
      Dim da as new SqlDataAdapter(_Query,conn)
    
      conn.Open()
      da.Fill(dt)
      conn.Close()
      return dt
    End Sub
    
    private Sub CallSomething(ByVal dt as DataTable)
      foreach r as DataRow in dt.Rows
        'do something
      Next
    End Sub

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #6
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by alykins View Post
    Code:
    private Sub something()
    .....
    Dim dt as new DataTable()
    FillMyTable(dt)
    CallSomething(dt)
    ......
    End Sub
    
    private Sub FillMyTable(ByVal dt as DataTable)
    
      Dim connstring as String = "... connectionstring.com ... "
      Dim _Query as String = "SELECT * FROM tbl_Demo
    
      Dim conn as new SqlConnection(connstring)
      Dim da as new SqlDataAdapter(_Query,conn)
    
      conn.Open()
      da.Fill(dt)
      conn.Close()
      return dt
    End Sub
    
    private Sub CallSomething(ByVal dt as DataTable)
      foreach r as DataRow in dt.Rows
        'do something
      Next
    End Sub
    Is there any reason why i cant use the one datatable i have already created?



    how would i reference this dtTest that i have created through code. I want to use it rather than create a new one through code.

  • #7
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,760
    Thanks
    41
    Thanked 191 Times in 190 Posts
    you can use whatever you want... i just was copy/pasting and didn't want to change all references... honestly i have no clue how to do it via "wizard"... I hate wizards... doing it via code is always preferred... I tend to not even use the screen UI for setting properties- I usually set them via code. I have no clue what your code looks like or where that object is created... when you are on the webpage, hit F7 to go to code... you should see your object created in there somewhere. it might be in the designer (don't change things in there unless you know what you're doing- it will break)... the designer is a partial class though...
    Example
    Page.aspx <- asp.net web page with html elements
    Page.aspx.vb <- vb code behind file
    page.aspx.designer.vb <- partial class used to create objects and delegates

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #8
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ok, well, thanks.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •