Results 1 to 5 of 5

Thread: [RESOLVED] Populating datagrid with columns with multiple tables.

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2000
    Location
    Minnesota
    Posts
    830

    [RESOLVED] Populating datagrid with columns with multiple tables.

    Using ASP.NET (vb), I have two tables and joining on ID. I am having some issues populating my datagrid with a dataset.

    Here is what I have so far:
    VB Code:
    1. Dim conPubs as SqlConnection
    2. Dim dsSearch1 as DataSet
    3. Dim dsSearch2 as DataSet
    4. Dim adSearch1 as SqlDataAdapter
    5. Dim adSearch2 as SqlDataAdapter
    6. Dim iRecordsFound as Integer
    7. Dim sSearch as String
    8.  
    9. conPubs = New SqlConnection(...)
    10.  
    11. sSearch = txtSearch.Text("'", "''")
    12. If sSearch.Length > 0 Then
    13.  sSQL1 = "SELECT PRD_ID, BRAND, MODEL FROM t_products "
    14.  sSQL1 &= "WHERE BRAND like '%" & sSearch & "' "
    15.  sSQL1 &= "ORDER BY BRAND"
    16.  
    17.  sSQL2 = "SELECT RATING, RECOMMEND FROM t_details"
    18. Else
    19.  sSQL1 = "SELECT PRD_ID, BRAND, MODEL FROM t_products "
    20.  sSQL1 &= "ORDER BY BRAND"
    21.  
    22.  sSQL2 = "SELECT RATING, RECOMMEND FROM t_details"
    23. End If
    24.  
    25. adSearch1 = New SqlDataAdapter(sSQL1, conPubs)
    26. adSearch2 = New SqlDataAdapter(sSQL2, conPubs)
    27.  
    28. dsSearch1 = New DataSet()
    29. dsSearch2 = New DataSet()
    30.  
    31. adSearch1.Fill(dsSearch1, "t_products")
    32. adSearch2.Fill(dsSearch2, "t_products")
    33.  
    34. Dim pk1(0) as DataColumn
    35. Dim pk2(0) as DataColumn
    36.  
    37. pk1(0) = dsSearch1.Tables(0).Columns("PRD_ID")
    38. dsSearch1.Tables(0).PrimaryKey = pk1
    39.  
    40. pk2(0) = dsSearch2.Tables(0).Columns("PRD_ID")
    41. dsSearch2.Tables(0).PrimaryKey = pk2
    42.  
    43. dsSearch1.Merge(dsSearch2, false, MissingSchemaAction.Add)
    44.  
    45. conPubs.Open()
    46. iRecordsFound = dsSearch1.Tables("t_products").Rows.Count.ToString()
    47. lblRowCount.Text = iRecordsFound
    48.  
    49. datagridOutput.DataSource = dsSearch1
    50. datagridOutput.DataBind()
    51. conPubs.Close()

    The error I get now is:
    System.ArgumentNullException: 'column' argument cannot be null. Parameter name: column

    Any help would be great or better way of doing this is welcome as well.
    Thanks.
    Last edited by lleemon; Oct 6th, 2005 at 11:37 AM.

  2. #2
    Fanatic Member -TPM-'s Avatar
    Join Date
    Jul 2005
    Posts
    850

    Re: Populating datagrid with columns with multiple tables.

    Probably because sSQL2 isn't selecting PRD_ID.
    Is there a reason your not joining the tables in your sql statment?
    TPM

    Add yourself to the VBForums Frappr Map!!

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2000
    Location
    Minnesota
    Posts
    830

    Re: Populating datagrid with columns with multiple tables.

    Yeah, your right. I needed the PRD_ID in the sSQL2. I don't get errors but my datagrid says I have over 6 pages (of 25) but I can only see 2.

    You suggestion why not putting the sql all in one. I did try this way but could not get to work properly. This is how I would like to do it but couldn't get my code to work.

    Any ideas?

  4. #4
    Fanatic Member -TPM-'s Avatar
    Join Date
    Jul 2005
    Posts
    850

    Re: Populating datagrid with columns with multiple tables.

    Where you using a JOIN or just WHERE X=Y?
    TPM

    Add yourself to the VBForums Frappr Map!!

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2000
    Location
    Minnesota
    Posts
    830

    Re: Populating datagrid with columns with multiple tables.

    TPM - Using x = y.

    Update: I tried again and worked so not sure what I did differently but not complaining. This is what I have:
    VB Code:
    1. Dim conPubs as SqlConnection
    2.     Dim cmdSearch as SqlCommand
    3.     Dim dsSearch as DataSet
    4.     Dim SadSearch as SqlDataAdapter
    5.     Dim sSQL as String
    6.     Dim iRecordsFound as Integer
    7.     Dim sSearch as String
    8.            
    9.     'Open connection with connection object
    10.     conPubs = New SqlConnection(System.Configuration.ConfigurationSettings.AppSettings("MS_SQL_CONN"))
    11.    
    12.     sSearch = txtSearch.Text.Replace("'", "''")
    13.     sSearch = txtSearch.Text.Replace(";", vbNullString)
    14.    
    15.     'form sql
    16.     If sSearch.Length > 0 Then
    17.         sSQL = "SELECT p.PRD_ID, BRAND, MODEL, NAME, RECOMMENDED_CD FROM products p, products_details1 pd "
    18.         sSQL &= "WHERE p.PRD_ID = pd.PRD_ID "
    19.         sSQL &= "AND BRAND Like '%" & sSearch & "%' OR PMODEL Like '%" & sSearch & "%' "
    20.         sSQL &= "ORDER BY BRAND, MODEL"
    21.     Else
    22.         sSQL = "SELECT p.PRD_ID, BRAND, MODEL, NAME, RECOMMENDED_CD FROM products p, products_details1 pd WHERE p.PRD_ID = pd.PRD_ID ORDER BY BRAND"
    23.     End If
    24.    
    25.     'To execute sql statement and provide active connection
    26.     SadSearch = New SqlDataAdapter(sSQL, conPubs)
    27.    
    28.     'Create instance of dataset object
    29.     dsSearch = New DataSet()
    30.    
    31.     'fill datagrid
    32.     SadSearch.Fill(dsSearch, "t_products")
    33.    
    34.     conPubs.Open()
    35.     iRecordsFound = dsSearch.Tables("t_products").Rows.Count.ToString()
    36.     lblRowCount.Text = iRecordsFound
    37.    
    38.     dgrdResults.DataSource = dsSearch
    39.     dgrdResults.DataBind()
    40.     conPubs.Close()

    Do you see any advantage to using the JOIN?
    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
  •  



Click Here to Expand Forum to Full Width