Results 1 to 15 of 15

Thread: Datagrid not filling up [RESOLVED]

  1. #1

    Thread Starter
    Fanatic Member drpcken's Avatar
    Join Date
    Apr 2004
    Location
    devenv
    Posts
    591

    Datagrid not filling up [RESOLVED]

    Usually I create my dataset, dataadapter manually in Visual Studio, but in this case, I need the dataadapter.SelectCommand to give me only rows that one column matches the value of a textbox on the webform. But it wouldn't let me do that, so I tried manualy coding the dataadapter, and dataset.

    But when the page loads, there is no data in the datagrid. Am I misssing something??

    Thanks again, here's the code. No errors on load, just no data in datagrid1. It shows the column headings. Pay special attention to the selectcommand of the dataadapter.


    Private dsItems As New DataSet()
    Private daItems As New SqlDataAdapter()


    Private Sub LoadLineItems()

    If SqlConnection1.State = ConnectionState.Closed Then SqlConnection1.Open()

    daItems.SelectCommand = New SqlCommand("SELECT ItemNumber, Price, Quantity from dbLineItems where RecID = '" + txtRecId.Text + "'", SqlConnection1)
    daItems.Fill(dsItems)
    DataGrid1.DataSource = dsItems
    DataGrid1.DataBind()
    SqlConnection1.Close()

    End Sub
    Last edited by drpcken; Jul 1st, 2004 at 08:21 AM.

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    The legendary CyberHawke once told me:

    VB Code:
    1. 'you could do it this way
    2.  
    3. Dim dt As DataTable
    4. dt = ds.Tables(0)
    5. DataGrid1.DataSource = dt
    6.  
    7.  
    8.  
    9. 'or this way
    10.  
    11. DataGrid1.DataSource = ds
    12. DataGrid1.DataMember = "Table"

    And then DataBind() it.

    HTH.

  3. #3

    Thread Starter
    Fanatic Member drpcken's Avatar
    Join Date
    Apr 2004
    Location
    devenv
    Posts
    591
    Originally posted by mendhak
    The legendary CyberHawke once told me:

    VB Code:
    1. 'you could do it this way
    2.  
    3. Dim dt As DataTable
    4. dt = ds.Tables(0)
    5. DataGrid1.DataSource = dt

    Ok kew, here's the code I made from that:

    Private Sub LoadLineItems()
    Dim dt As DataTable

    If SqlConnection1.State = ConnectionState.Closed Then SqlConnection1.Open()

    daItems.SelectCommand = New SqlCommand("SELECT ItemNumber, Price, Quantity from dbLineItems where RecID = '" + RecId + "'", SqlConnection1)
    daItems.Fill(dsItems)
    dt = dsItems.Tables(0)
    DataGrid1.DataSource = dt
    DataGrid1.DataBind()
    SqlConnection1.Close()


    End Sub



    But still nothing in my datagrid. For some reason, I think it has something to do with the SelectCommand.

  4. #4

    Thread Starter
    Fanatic Member drpcken's Avatar
    Join Date
    Apr 2004
    Location
    devenv
    Posts
    591
    hmmmm
    ok I just changed the select command to just a normal query, and it STILL isn't filling my datagrid. So its not the selectCommand

  5. #5
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    VB Code:
    1. Private Sub LoadLineItems()
    2.         Dim dt As DataTable
    3.  
    4.         If SqlConnection1.State = ConnectionState.Closed Then SqlConnection1.Open()
    5.  
    6.         daItems.SelectCommand = New SqlCommand("SELECT ItemNumber, Price, Quantity from dbLineItems where RecID = '" + RecId + "'", SqlConnection1)
    7.         daItems.Fill(dsItems)
    8.         dt = dsItems.Tables(0)
    9.         DataGrid1.DataSource = dt
    10.  
    11.         SqlConnection1.Close()
    12.  
    13.  
    14.     End Sub

    Now? I removed the DataBind() line...

  6. #6
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    I hope you have the daItems, dsItems, connections etc. all declared.

  7. #7
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Let me also show you an example I quickly wrote write now to see if I was going wrong anywhere:

    VB Code:
    1. Dim dt As DataTable
    2.         Dim ds As New DataSet
    3.  
    4.  
    5.         Dim strConn2 As String
    6.  
    7.         strConn2 = "Data Source=(local);" & _
    8.                             "Initial Catalog=NorthWind;" & _
    9.                             "User ID=sa;Password=;"
    10.         Dim sqlconnection1 As New SqlConnection(strConn2)
    11.  
    12.  
    13.         sqlconnection1.Open()
    14.  
    15.         Dim da As New SqlDataAdapter
    16.         da.SelectCommand = New SqlCommand("SELECT * FROM Products", sqlconnection1)
    17.  
    18.         da.Fill(ds)
    19.         dt = ds.Tables(0)
    20.         DataGrid1.DataSource = dt
    21.  
    22.  
    23.  
    24.  
    25.         'DataGrid1.DataBind()  
    26.         'Had to comment this out...
    27.         SqlConnection1.Close()

  8. #8

    Thread Starter
    Fanatic Member drpcken's Avatar
    Join Date
    Apr 2004
    Location
    devenv
    Posts
    591
    hmm still no go and yes i declared all my variables. Here's more code, I just can't seem to get this...

    Private VendorID, RecId, Desc, DateNeeded, Reason, Clinic As String
    Private Const CONN_STRING = "server=(local);uid=***;pwd=****;initial catalog=****"
    Private sqlconn As SqlConnection
    Public dsItems As New DataSet()
    Public daItems As New SqlDataAdapter()



    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    If Not IsPostBack Then
    VendorID = Session("VendorId")
    RecId = Session("RecId")
    Desc = Session("Desc")
    DateNeeded = Session("DateNeeded")
    Reason = Session("reason")
    Clinic = Session("Clinic")

    txtRecId.Text = RecId
    txtDescription.Text = Desc
    txtDateNeeded.Text = DateNeeded
    txtReason.Text = Reason
    ddlUserClinic.SelectedItem.Text = Clinic
    LoadLineItems()
    End If
    If sqlconn.State <> ConnectionState.Open Then sqlconn.Open()
    End Sub

    Private Sub LoadLineItems()
    Dim dt As DataTable

    SqlConnection1.Open()

    daItems.SelectCommand = New SqlCommand("SELECT ItemNumber, Price, Quantity from dbLineItems", SqlConnection1)
    daItems.Fill(dsItems)
    dt = dsItems.Tables(0)
    DataGrid1.DataSource = dt
    'DataGrid1.DataBind()
    SqlConnection1.Close()


    End Sub


    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
    Dim oRec As New Recquisition()

    Try
    With oRec
    .VendorId = VendorID
    .Description = txtDescription.Text
    .DateNeeded = txtDateNeeded.Text.ToString
    .Reason = txtReason.Text
    .RecqusitionId = txtRecId.Text
    .Clinic = ddlUserClinic.SelectedItem.Text
    .Save()
    End With
    Finally
    oRec = Nothing
    End Try
    End Sub

  9. #9

    Thread Starter
    Fanatic Member drpcken's Avatar
    Join Date
    Apr 2004
    Location
    devenv
    Posts
    591
    and i keep getting an Object reference not set to an instance of an object error pointing back to this line


    da.SelectCommand = New SqlCommand("Select * from dbLineItems where RecID = '" + TextBox1.Text + "'", sqlconn)

  10. #10
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Originally posted by drpcken


    da.SelectCommand = New SqlCommand("Select * from dbLineItems where RecID = '" + TextBox1.Text + "'", sqlconn)
    Shouldn't that be daItems?

    Edit: never mind. Still checking.

  11. #11
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Yes, check to see if you have da instead of daItems.

    Also, look at this part of your code:

    VB Code:
    1. Private Const CONN_STRING = "server=(local);uid=***;pwd=****;initial catalog=****"
    2. Private sqlconn As SqlConnection
    3.  
    4. 'and later on:
    5.  
    6.  If sqlconn.State <> ConnectionState.Open Then sqlconn.Open()

    Nowhere have you used CONN_STRING when opening up the SQL Connection!

  12. #12

    Thread Starter
    Fanatic Member drpcken's Avatar
    Join Date
    Apr 2004
    Location
    devenv
    Posts
    591
    Ok I think I got it WIth your help of course!! The CONN_STRING was an extra connection i had in there, SqlConnection1 was in the designer. The da was correct, but i was testing another form to see if i could recreate the error, so you were right insaying daItems was the correct adapter. I finally got the data to bind. With this...


    Private Sub LoadLineItems()
    sqlconn.ConnectionString = "server=(local);uid=sa;pwd=****;initial catalog=t***"
    Dim cmdLoad As New SqlCommand("select * from dbLineItems where RecId='" + txtRecId.Text + "'", sqlconn)
    Dim daItems As New SqlDataAdapter(cmdLoad)

    Try
    sqlconn.Open()
    daItems.Fill(dsItems, "dbLineItems")
    DataGrid1.DataSource = dsItems.Tables(0)
    DataGrid1.DataBind()
    sqlconn.Close()
    Finally
    'To do after binding
    End Try
    End Sub


    I think I wasn't specifiying the member in my .Fill

    Thanks for your help. Probably won't be the last time i'll need it though

    You are the man!

  13. #13
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Yeah, I'm da frogg

  14. #14

    Thread Starter
    Fanatic Member drpcken's Avatar
    Join Date
    Apr 2004
    Location
    devenv
    Posts
    591
    Thanks once again!!

  15. #15
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170


    Now edit your first post and add [Resolved].

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