Data in dataset but not displaying in gridview

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #16
    Originally posted by kjewell23
    Wel their are certain numbers that has alot of pages so I was going to do sorting and paging. Can a panel do that?
    No no, gridview is the right object for displaying your data, it's the strings used in Response.Write( ) that should go into some temporary ASP:Label or other text-displaying control.

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #17
      Originally posted by kjewell23
      the response write is for debugging but if I take that out nothing shows up
      Well we know that you have filled your DataSet correctly...and that it's just a matter of getting that data to show in your GridView.

      Take a look at MSDN's GridView.DataBi nd Method


      The other thing I would try would be:
      [code=vbnet]
      1.
      Protected Sub btnSubmit_Click (ByVal sender As Object, ByVal e As System.EventArg s) Handles btnSubmit.Click
      Dim myconnection As OdbcConnection
      Dim myCommand As OdbcDataAdapter
      Dim mydataset As New DataSet
      Dim gvResults As DataTable
      Dim loop1 As Integer
      Dim numrows As Integer
      Dim mysql As String
      Dim Order As Integer
      Order = CInt(txtOrderNu mber.Text)
      mysql = "select line#, ordno, quano, quana, quans, c2rdt, unitm, prdno, actsp from rmsfiles2.obcop 200 where ordno = " & Order
      myconnection = New OdbcConnection( "DRIVER=Cli ent Access ODBC Driver (32-bit);UID=ODBC;P WD=XXXXXX;Syste m=xxx.xxx.xxx.x xx")
      myCommand = New OdbcDataAdapter (mysql, myconnection)
      myCommand.Fill( mydataset, "OBCOP200")

      gvResults = New DataTable
      gvResults = mydataset.Table s(0)
      numrows = gvResults.Rows. Count

      If numrows = 0 Then
      txtOrderNumber. Text = "Order#" & txtOrderNumber. Text & " invalid order number"

      Else
      txtOrderNumber. Text = ""
      gridview1.DataS ource = new Data.DataView(g vResults) 'mydataset.Tabl es("OBCOP200"). DefaultView
      gridview1.DataB ind()
      gridview1.Visib le = True

      End If[/code]

      When ever I'm dealing with GridViews I end up looking them up on MSDN...it's been a while since I've used them.

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #18
        Originally posted by Plater
        From what I've read, Response.Write( ) just injects whatever you put into the current section of the buffer. Which could be inside one of your html elements.
        I thought it was supposed to work similar to javascripts' Document.Write( ), but I've ehard of it doing funny things with the sending buffer from people.

        Response.Write does do funny things.
        I sometimes use it when I'm lazy and just want to debug something quickly..but where the output ends up could end up before your html for the page.

        You should use a label or something to write debugging stuff...just some of us are lazy ;)

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #19
          Originally posted by kjewell23
          i did that and it still displays but not in gridview. It shows that I have three rows in the part where if numrows and on down . When I debug it it shows system. dataset where the fill command is and then it shows mydataset.Table s = {System.Data.Da taTableCollecti on} at gvResults = (where the datatable is)
          then it has the number of rows in if then and then it has the number that i typed inside the text box also in the else then it through the loop 23 times before displaying it. and then finally it shows at gridview1.DataS ource = Nothing
          after the if statements ends when its highlighted then after highlightended is gone is shows mydataset = {System.Data.Da taSet}

          so does that mean anything to you

          When you are debugging...you will move from line to line....the line of code is not executed until you move to the next line in debugging.

          That is why the DataSource is nothing when its highlighted...a nd then is set when it's moved to the next line.

          Please remember that you are not setting the DataSource of the GridView in your loop....you set it afterwards....

          Comment

          • kjewell23
            New Member
            • Sep 2007
            • 49

            #20
            when i do the debug it still says nothing in the gridview where it states gridview1.datas ource = new data.dataview(g vResults)

            Originally posted by Frinavale
            Well we know that you have filled your DataSet correctly...and that it's just a matter of getting that data to show in your GridView.

            Take a look at MSDN's GridView.DataBi nd Method


            The other thing I would try would be:
            [code=vbnet]
            1.
            Protected Sub btnSubmit_Click (ByVal sender As Object, ByVal e As System.EventArg s) Handles btnSubmit.Click
            Dim myconnection As OdbcConnection
            Dim myCommand As OdbcDataAdapter
            Dim mydataset As New DataSet
            Dim gvResults As DataTable
            Dim loop1 As Integer
            Dim numrows As Integer
            Dim mysql As String
            Dim Order As Integer
            Order = CInt(txtOrderNu mber.Text)
            mysql = "select line#, ordno, quano, quana, quans, c2rdt, unitm, prdno, actsp from rmsfiles2.obcop 200 where ordno = " & Order
            myconnection = New OdbcConnection( "DRIVER=Cli ent Access ODBC Driver (32-bit);UID=ODBC;P WD=XXXXXX;Syste m=xxx.xxx.xxx.x xx")
            myCommand = New OdbcDataAdapter (mysql, myconnection)
            myCommand.Fill( mydataset, "OBCOP200")

            gvResults = New DataTable
            gvResults = mydataset.Table s(0)
            numrows = gvResults.Rows. Count

            If numrows = 0 Then
            txtOrderNumber. Text = "Order#" & txtOrderNumber. Text & " invalid order number"

            Else
            txtOrderNumber. Text = ""
            gridview1.DataS ource = new Data.DataView(g vResults) 'mydataset.Tabl es("OBCOP200"). DefaultView
            gridview1.DataB ind()
            gridview1.Visib le = True

            End If[/code]

            When ever I'm dealing with GridViews I end up looking them up on MSDN...it's been a while since I've used them.

            Comment

            • Frinavale
              Recognized Expert Expert
              • Oct 2006
              • 9749

              #21
              I suggest following the example from MSDN only instead of using the Microsoft SQL Database code, use your AS400 database code:
              [code=vbnet]
              Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

              ' This example uses Microsoft SQL Server and connects
              ' to the Northwind sample database. The data source needs
              ' to be bound to the GridView control only when the
              ' page is first loaded. Thereafter, the values are
              ' stored in view state.
              If Not IsPostBack Then

              ' Declare the query string.
              Dim queryString As String = _
              "Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"

              ' Run the query and bind the resulting DataSet
              ' to the GridView control.
              Dim ds As DataSet = GetData(querySt ring)
              If (ds.Tables.Coun t > 0) Then

              AuthorsGridView .DataSource = ds
              AuthorsGridView .DataBind()

              Else

              Message.Text = "Unable to connect to the database."

              End If

              End If

              End Sub

              Function GetData(ByVal queryString As String) As DataSet

              ' Retrieve the connection string stored in the Web.config file.
              Dim connectionStrin g As String = ConfigurationMa nager.Connectio nStrings("North WindConnectionS tring").Connect ionString

              Dim ds As New DataSet()

              Try

              ' Connect to the database and run the query.
              Dim connection As New SqlConnection(c onnectionString )
              Dim adapter As New SqlDataAdapter( queryString, Connection)

              ' Fill the DataSet.
              Adapter.Fill(ds )


              Catch ex As Exception

              ' The connection failed. Display an error message.
              Message.Text = "Unable to connect to the database."

              End Try

              Return ds

              End Function
              [/code]

              Comment

              • kjewell23
                New Member
                • Sep 2007
                • 49

                #22
                when i do the debug it still says nothing in the gridview where it states gridview1.datas ource = new data.dataview(g vResults)

                Comment

                • kjewell23
                  New Member
                  • Sep 2007
                  • 49

                  #23
                  Do I need to make an app code then within a dataset with my data that I need because right now the getdata is underlined. Also How do I call the function into the submit button?
                  Last edited by Frinavale; Sep 11 '07, 07:30 PM. Reason: Removed quote to make thread visible

                  Comment

                  • kjewell23
                    New Member
                    • Sep 2007
                    • 49

                    #24
                    How do I call the function inside the button click and do I need to make app code with dataset in it
                    Last edited by Frinavale; Sep 11 '07, 07:33 PM. Reason: Removed quote to make thread visible

                    Comment

                    • Frinavale
                      Recognized Expert Expert
                      • Oct 2006
                      • 9749

                      #25
                      Originally posted by kjewell23
                      Do I need to make an app code then within a dataset with my data that I need because right now the getdata is underlined.
                      No you don't...
                      The reason GetData is underlined is because you probably didn't create that function.

                      In the example the GetData gets the DataSet and returns it.
                      Did you create this Function?

                      You do not need to make another VB file to hold this code....if that's what you're asking.

                      Could you please post what your code looks like now?

                      Originally posted by kjewell23
                      Also How do I call the function into the submit button?
                      It's the same sort of thing...
                      In the Sub that handles the button click you create a data source for the GridView and then Bind the GridView to that data source.

                      Comment

                      • kjewell23
                        New Member
                        • Sep 2007
                        • 49

                        #26
                        page load code
                        [code=vbnet]
                        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArg s) Handles Me.Load

                        Dim Order As Integer
                        Order = CInt(txtOrderNu mber.Text)

                        If Not IsPostBack Then
                        Dim mysql As String = "select ordno, quano, quana, actsp from rmsfiles2.obcop 200 where = " & Order

                        Dim mydataset As DataSet = GetData(mysql)
                        If (mydataset > 0) Then
                        gridview1.DataS ource = mydataset
                        gridview1.DataB ind()
                        Else
                        txtOrderNumber. Text = " unable to connect to the database"

                        End If
                        End If
                        [/code]
                        button code
                        [code=vbnet]
                        Protected Sub btnSubmit_Click (ByVal sender As Object, ByVal e As System.EventArg s) Handles btnSubmit.Click

                        Dim connectionStrin g = "DRIVER=Cli ent Access ODBC Driver (32-bit);UID=ODBC;P WD=xxxxxxxxxx;S ystem=XXX.XXX.X XX.XXX"


                        Dim myconnection As OdbcConnection
                        Dim myCommand As OdbcDataAdapter
                        Dim mydataset As New DataSet
                        ' Dim gvResults As DataTable
                        'Dim loop1 As Integer
                        ' Dim numrows As Integer
                        Dim sql As String
                        Dim Order As Integer
                        Order = CInt(txtOrderNu mber.Text)
                        sql = "select line#, ordno, quano, quana, quans, c2rdt, unitm, prdno, actsp from rmsfiles2.obcop 200 where ordno = " & Order
                        Try

                        myconnection = New OdbcConnection( "DRIVER=Cli ent Access ODBC Driver (32-bit);UID=ODBC;P WD=xxxxxxxxxx;S ystem=XXX.XXX.X XX.XXX")

                        myCommand = New OdbcDataAdapter (sql, myconnection)

                        myCommand.Fill( mydataset)
                        Catch ex As Exception
                        txtOrderNumber. Text = "invalid order number"

                        End Try

                        Return
                        gridview1.DataS ource = mydataset
                        gridview1.DataB ind()
                        gridview1.Visib le = True[/code]

                        here is my function code:
                        [code=vbnet]
                        Function GetData(ByVal mysql As String) As DataSet
                        Dim connectionStrin g = "DRIVER=Cli ent Access ODBC Driver (32-bit);UID=ODBC;P WD=xxxxxxxxxx;S ystem=XXX.XXX.X XX.XXX"


                        Dim myconnection As OdbcConnection
                        Dim myCommand As OdbcDataAdapter
                        Dim mydataset As New DataSet
                        ' Dim gvResults As DataTable
                        'Dim loop1 As Integer
                        ' Dim numrows As Integer
                        Dim sql As String
                        Dim Order As Integer
                        Order = CInt(txtOrderNu mber.Text)
                        sql = "select line#, ordno, quano, quana, quans, c2rdt, unitm, prdno, actsp from rmsfiles2.obcop 200 where ordno = " & Order
                        Try

                        myconnection = New OdbcConnection( "DRIVER=Cli ent Access ODBC Driver (32-bit);UID=ODBC;P WD=xxxxxxxxxx;S ystem=XXX.XXX.X XX.XXX")

                        myCommand = New OdbcDataAdapter (sql, myconnection)

                        myCommand.Fill( mydataset)
                        Catch ex As Exception
                        txtOrderNumber. Text = "invalid order number"

                        End Try

                        Return mydataset
                        End Function

                        [/code]
                        Originally posted by Frinavale
                        No you don't...
                        The reason GetData is underlined is because you probably didn't create that function.

                        In the example the GetData gets the DataSet and returns it.
                        Did you create this Function?

                        You do not need to make another VB file to hold this code....if that's what you're asking.

                        Could you please post what your code looks like now?



                        It's the same sort of thing...
                        In the Sub that handles the button click you create a data source for the GridView and then Bind the GridView to that data source.
                        Last edited by Frinavale; Sep 12 '07, 01:32 PM. Reason: Added [code] tags to make more legible & removed IP and Passwords

                        Comment

                        • kjewell23
                          New Member
                          • Sep 2007
                          • 49

                          #27
                          This part right here is underlined like its wrong
                          If (mydataset > 0) Then
                          Originally posted by Frinavale
                          No you don't...
                          The reason GetData is underlined is because you probably didn't create that function.

                          In the example the GetData gets the DataSet and returns it.
                          Did you create this Function?

                          You do not need to make another VB file to hold this code....if that's what you're asking.

                          Could you please post what your code looks like now?



                          It's the same sort of thing...
                          In the Sub that handles the button click you create a data source for the GridView and then Bind the GridView to that data source.

                          Comment

                          • nanku71
                            New Member
                            • Sep 2007
                            • 4

                            #28
                            Originally posted by kjewell23
                            This part right here is underlined like its wrong
                            If (mydataset > 0) Then
                            yes that is wrong. .

                            you have to check the rows count to be > 0

                            it would be

                            If (mydataset.tabl es(0).rows.coun t > 0) then


                            hope this helps.

                            samir

                            Comment

                            • kjewell23
                              New Member
                              • Sep 2007
                              • 49

                              #29
                              Now its says under the function getData code is the order = cint(txtOrderNu mber.text) is invalidcastexce ption was unhandled by user code and conversion from string to type integer is not valid

                              What should I do here?

                              Comment

                              • Frinavale
                                Recognized Expert Expert
                                • Oct 2006
                                • 9749

                                #30
                                [code=vbnet]
                                Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArg s) Handles Me.Load

                                Dim Order As Integer
                                Order = CInt(txtOrderNu mber.Text)

                                If Not IsPostBack Then
                                Dim mysql As String = "select ordno, quano, quana, actsp from rmsfiles2.obcop 200 where = " & Order

                                Dim mydataset As DataSet = GetData(mysql)
                                If (mydataset > 0) Then
                                gridview1.DataS ource = mydataset
                                gridview1.DataB ind()
                                Else
                                txtOrderNumber. Text = " unable to connect to the database"

                                End If
                                End If
                                [/code]

                                There is no need to grab the DataSet in the Page Load method because your user has probably not supplied an OrderNumber until they hit the button....

                                This is probably why you're getting that exception about casting...you'r e probably trying to cast an empty string into an integer.


                                The whole point to making the GetData function is so that you can get the DataSet from anywhere in code and not have to retype everything again.


                                [code=vbnet]
                                Protected Sub btnSubmit_Click (ByVal sender As Object, ByVal e As System.EventArg s) Handles btnSubmit.Click

                                Dim connectionStrin g = "DRIVER=Cli ent Access ODBC Driver (32-bit);UID=ODBC;P WD=xxxxxxxxxx;S ystem=XXX.XXX.X XX.XXX"


                                Dim myconnection As OdbcConnection
                                Dim myCommand As OdbcDataAdapter
                                Dim mydataset As New DataSet
                                ' Dim gvResults As DataTable
                                'Dim loop1 As Integer
                                ' Dim numrows As Integer
                                Dim sql As String
                                Dim Order As Integer
                                Order = CInt(txtOrderNu mber.Text)
                                sql = "select line#, ordno, quano, quana, quans, c2rdt, unitm, prdno, actsp from rmsfiles2.obcop 200 where ordno = " & Order
                                Try

                                myconnection = New OdbcConnection( "DRIVER=Cli ent Access ODBC Driver (32-bit);UID=ODBC;P WD=xxxxxxxxxx;S ystem=XXX.XXX.X XX.XXX")

                                myCommand = New OdbcDataAdapter (sql, myconnection)

                                myCommand.Fill( mydataset)
                                Catch ex As Exception
                                txtOrderNumber. Text = "invalid order number"

                                End Try

                                Return
                                gridview1.DataS ource = mydataset
                                gridview1.DataB ind()
                                gridview1.Visib le = True[/code]

                                Why do you have a Return on line 28 in this section of code?

                                I'm going to repeat myself here but...the whole point to the GetData Function is so that you don't have to retype the call to the database to get the DataSet.

                                I think you should change your GetData function to:

                                Function GetData(ByVal mysql As String, ByVal myOrderNumber As Integer)

                                This way you can check to make sure that the user entered an order number before trying to use it in the database call.

                                [code=vbnet]
                                Function GetData(ByVal mysql As String) As DataSet
                                Dim connectionStrin g = "DRIVER=Cli ent Access ODBC Driver (32-bit);UID=ODBC;P WD=xxxxxxxxxx;S ystem=XXX.XXX.X XX.XXX"


                                Dim myconnection As OdbcConnection
                                Dim myCommand As OdbcDataAdapter
                                Dim mydataset As New DataSet
                                ' Dim gvResults As DataTable
                                'Dim loop1 As Integer
                                ' Dim numrows As Integer
                                Dim sql As String
                                Dim Order As Integer
                                Order = CInt(txtOrderNu mber.Text)
                                sql = "select line#, ordno, quano, quana, quans, c2rdt, unitm, prdno, actsp from rmsfiles2.obcop 200 where ordno = " & Order
                                Try

                                myconnection = New OdbcConnection( "DRIVER=Cli ent Access ODBC Driver (32-bit);UID=ODBC;P WD=xxxxxxxxxx;S ystem=XXX.XXX.X XX.XXX")

                                myCommand = New OdbcDataAdapter (sql, myconnection)

                                myCommand.Fill( mydataset)
                                Catch ex As Exception
                                txtOrderNumber. Text = "invalid order number"

                                End Try

                                Return mydataset
                                End Function

                                [/code]

                                This is just a reminder: Please do not post the IP and Password to your database. This is sensitive information that should not be publicly shared. I have edited it out, but please don't post this information...t his is for the safety of your own computer :)

                                Comment

                                Working...