Data in dataset but not displaying in gridview

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #46
    Originally posted by kjewell23
    How do I call the function inside the button click and do I need to make app code with dataset in it
    I just realized that you do not know how to call a function!
    I'm sorry I glanced over this question before.

    First of all, a function returns something and a sub does not.

    To call a sub or function (a method) from anywhere else in code you simply type its name.

    For example say I have a sub called mySub:
    [code=vbnet]
    Private Sub mySub()
    'do something
    End Sub
    [/code]
    And say I want to call this sub from my button click Sub...all I would do is:
    [code=vbnet]
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArg s) Handles Me.Load
    mySub()
    End Sub
    [/code]

    If I were to call a Function on the other hand, I'd need to store it's output somewhere. So I need to create a variable in the button click Sub that matches the type that the Function returns and then call the Function...

    For Example:
    [code=vbnet]

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArg s) Handles Me.Load
    Dim myNumber As Integer = myFunction()
    'myNumber will be set to 12 after the above line is executed.
    'the number 12 is returned to this sub by the MyFunction function and is stored in the myNumber Integer.
    End Sub


    Private Function MyFunction() As Integer 'The "As Integer" portion of this line indicates what type of value is expected to be returned.
    'This function returns an Integer value
    Return 12
    End Function

    [/code]

    One other thing about Subs and Functions is that you can pass values into them using parameters. Parameters are passed into Subs or Functions through the () part of the Sub or Function..

    Private Function MyFunction(ByVal x As Integer) As Integer

    (the bolded part is where the parameter is passed in)

    A parameter is simply a variable passed into a Sub or Function that is used for some sort of processing within that function....

    [code=vbnet]
    Private Function MyFunction((ByV al x As Integer) As Integer
    'x is a parameter passed into this function.
    'in this case an Integer is expected to be passed into this function
    'x can then be used to do whatever with....

    'I'm going to subtract x from 12 and return that value
    Return 12 - x
    End Function
    [/code]

    This is an example of calling a function that expects a paramter:
    [code=vbnet]
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArg s) Handles Me.Load
    Dim myNumber As Integer = MyFunction(3)
    'In this case myNumber will be set to 9.....
    End Sub

    Private Function MyFunction((ByV al x As Integer) As Integer
    'x is a parameter passed into this function.
    'in this case an Integer is expected to be passed into this function
    'x can then be used to do whatever with....

    'I'm going to subtract x from 12 and return that value
    Return 12 - x
    End Function
    [/code]

    Does this make sense?

    Comment

    • kjewell23
      New Member
      • Sep 2007
      • 49

      #47
      data in dataset but not displaying in gridview

      Thats ok. I kind understand. How do I do mine though. As time goes by their will be alot of order numbers so I'm not for sure how to do it.
      Here is all my code again.
      rry I glanced over this question before.

      [code=vbnet]

      Imports System
      Imports System.Data
      Imports System.Data.Sql Client
      Imports System.Data.Odb c
      Imports System.Data.Dat aSet
      Imports System.Data.Dat aTableCollectio n
      Imports IBM.Data.DB2.iS eries
      Partial Class Parts_Inquiries _OrderDetailInq uiry
      Inherits System.Web.UI.P age
      Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArg s) Handles Me.Load


      'Define variable to pass parm from calling page.
      Dim OrderNumber As String
      OrderNumber = Request.QuerySt ring("ordno")
      lbl_error.Text = ""
      'If part number passed in plug it into text box
      If txtOrderNumber. Text = "" And OrderNumber > "" Then
      txtOrderNumber. Text = OrderNumber
      End If

      'Conditionally return to menu or previous page depending if parm passed in

      exitHyperLink.T ext = " Prev"
      exitHyperLink.N avigateUrl = "~/Parts/Inquiries/OrderInquiry.as px" & txtOrderNumber. Text & ""

      TrackingHyperLi nk.Text = "Tracking"
      TrackingHyperLi nk.NavigateUrl = "~/parts/inquiries/TrackingInquiry .aspx?ordno=" & txtOrderNumber. Text & ""

      exitHyperLink.T ext = "Exit"
      exitHyperLink.N avigateUrl = "~/parts/inquiries/EngineeringInqu iryMenu.aspx"


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

      Dim Order As Integer

      Try
      Order = Integer.Parse(t xtOrderNumber.T ext)

      Catch ex As Exception
      Order = 0
      lbl_error.Text = "Please supply a valid number"
      'End Catch

      End Try
      'Dim mydataset As DataSet = GetData(Order)
      'If (mydataset.Tabl es.Count > 0) Then
      gridview1.DataS ource = GetData(Order)
      gridview1.DataB ind()
      gridview1.Visib le = True
      ' Else
      'txtOrderNumber .Text = "Unable to retrieve data"
      'End If
      End Sub
      [/code]
      [code=vbnet]
      Function GetData(ByVal myOrderNumber As Integer) As Data.DataView
      Dim dr As Data.DataRow
      Dim dt As New Data.DataTable 'the table that will hold the card holders

      dt.Columns.Add( New Data.DataColumn ("First Name", GetType(String) ))
      dt.Columns.Add( New Data.DataColumn ("Last Name", GetType(String) ))

      dr = dt.NewRow
      dr("First Name") = "first row, first name"
      dr("Last Name") = "first row, last name"

      dt.Rows.Add(dr)

      dr = dt.NewRow
      dr("First Name") = "second row, first name"
      dr("Last Name") = "second row, last name"

      dt.Rows.Add(dr)

      Dim dv As New Data.DataView(d t)
      Return dv
      End Function
      [/code]
      Last edited by kjewell23; Sep 13 '07, 03:23 PM. Reason: replace code tags

      Comment

      • kjewell23
        New Member
        • Sep 2007
        • 49

        #48
        data in dataset but not displaying in gridview

        Also don't I have to have my connection string, data adapter, command and my sql statement somewhere in the code where it knows where to go ?
        Last edited by kjewell23; Sep 13 '07, 03:40 PM. Reason: forgot something

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #49
          Originally posted by kjewell23
          Also don't I have to have my connection string, data adapter, command and my sql statement somewhere in the code where it knows where to go ?
          Eventually you will have to connect to the database to retrieve the data from there and display it in the GridView.

          Right now that is not working for you.
          You don't seem to be able to have anything appear in your GridView ever....
          I was suggesting going right down to a level that I know works and then work your way back up to connecting to the database and displaying the data.

          I just wanted to see if your GridView was able to display Any data.
          So I sent you a function that should have displayed 2 rows of data the GridView.

          Are you saying that you didn't actually try this?
          If you have tried it and the GridView showed nothing still...
          I'd be concerned with the way your project is set up.
          It sounds like the project itself is configured incorrectly and might be the cause to your GridView problem......

          But I didn't want to suggest this until we tried something basic.
          Like just displaying 2 hard coded rows in the GridView....thi s way I'd be able to tell if it was as database problem...or a problem with the project.


          I'd strongly suggest trying this before we jump to any conclusions.
          The following is what I think your code should look like while we are trying this out (this way we can easily start putting your database stuff back in):
          [code=vbnet]
          Imports System
          Imports System.Data
          Imports System.Data.Sql Client
          Imports System.Data.Odb c
          Imports System.Data.Dat aSet
          Imports System.Data.Dat aTableCollectio n
          Imports IBM.Data.DB2.iS eries
          Partial Class Parts_Inquiries _OrderDetailInq uiry
          Inherits System.Web.UI.P age
          Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArg s) Handles Me.Load


          'Define variable to pass parm from calling page.
          Dim OrderNumber As String
          OrderNumber = Request.QuerySt ring("ordno")
          lbl_error.Text = ""
          'If part number passed in plug it into text box
          If txtOrderNumber. Text = "" And OrderNumber > "" Then
          txtOrderNumber. Text = OrderNumber
          End If

          'Conditionally return to menu or previous page depending if parm passed in

          exitHyperLink.T ext = " Prev"
          exitHyperLink.N avigateUrl = "~/Parts/Inquiries/OrderInquiry.as px" & txtOrderNumber. Text & ""

          TrackingHyperLi nk.Text = "Tracking"
          TrackingHyperLi nk.NavigateUrl = "~/parts/inquiries/TrackingInquiry .aspx?ordno=" & txtOrderNumber. Text & ""

          exitHyperLink.T ext = "Exit"
          exitHyperLink.N avigateUrl = "~/parts/inquiries/EngineeringInqu iryMenu.aspx"


          End Sub


          Protected Sub btnSubmit_Click (ByVal sender As Object, ByVal e As System.EventArg s) Handles btnSubmit.Click

          Dim Order As Integer

          Try
          Order = Integer.Parse(t xtOrderNumber.T ext)

          Catch ex As Exception
          Order = 0
          lbl_error.Text = "Please supply a valid number"
          'End Catch

          End Try
          'Dim mydataset As DataSet = GetData(Order)
          'If (mydataset.Tabl es.Count > 0) Then
          gridview1.DataS ource = GetData(Order)
          gridview1.DataB ind()
          gridview1.Visib le = True
          ' Else
          'txtOrderNumber .Text = "Unable to retrieve data"
          'End If
          End Sub
          Function GetData(ByVal myOrderNumber As Integer) As Data.DataView
          Dim dr As Data.DataRow
          Dim dt As New Data.DataTable 'the table that will hold the card holders

          dt.Columns.Add( New Data.DataColumn ("First Name", GetType(String) ))
          dt.Columns.Add( New Data.DataColumn ("Last Name", GetType(String) ))

          dr = dt.NewRow
          dr("First Name") = "first row, first name"
          dr("Last Name") = "first row, last name"

          dt.Rows.Add(dr)

          dr = dt.NewRow
          dr("First Name") = "second row, first name"
          dr("Last Name") = "second row, last name"

          dt.Rows.Add(dr)

          Dim dv As New Data.DataView(d t)
          Return dv
          End Function
          'Function GetData(ByVal myOrderNumber As Integer) As DataSet

          ' Dim connectionStrin g = "DRIVER=Cli ent Access ODBC Driver (32-bit);UID=ODBC;P WD=XXXXXX;Syste m=XXX.XXX.XXX.X XX"


          ' Dim myconnection As OdbcConnection
          ' Dim myCommand As OdbcDataAdapter
          ' Dim mydataset As New DataSet

          ' Dim sql As String
          ' 'Dim myOrderNumber As Integer

          ' sql = "select line#, ordno, quano, quana, quans, c2rdt, unitm, prdno, actsp from rmsfiles2.obcop 200 where ordno = " & myOrderNumber.T oString()

          ' Try

          ' myconnection = New OdbcConnection( connectionStrin g)
          ' myCommand = New OdbcDataAdapter (sql, myconnection)

          ' myCommand.Fill( mydataset)
          ' Catch ex As Exception
          ' txtOrderNumber. Text = "Database Error:Unable to fill Dataset"

          ' End Try

          ' Return mydataset
          'End Function

          End Class
          [/code]

          Try this if you haven't already....
          Get back to me with the results.

          -Frinny

          Comment

          • kjewell23
            New Member
            • Sep 2007
            • 49

            #50
            Ok I try it. Nothing comes up when I do a number however when I type in name it comes up with please supply a valid number. What should we do

            Comment

            • Frinavale
              Recognized Expert Expert
              • Oct 2006
              • 9749

              #51
              Originally posted by kjewell23
              Ok I try it. Nothing comes up when I do a number however when I type in name it comes up with please supply a valid number. What should we do
              I suggest starting a new project and learning how to use the GridView object before continuing with what you are doing.

              MSDN has a library on everything you would like to know about .NET.
              See the MSDN article on the GridView Class. Near the bottom there are links to a whole bunch of walkthoughs that will help you learn this object. Farther down in the article there is an example.

              So I suggest starting a new project with just one aspx page in...and a GridView object (and label that you can use to display errors).

              Once you can get data to appear in your GridView, try adding in the database stuff.

              -Frinny

              Comment

              • Frinavale
                Recognized Expert Expert
                • Oct 2006
                • 9749

                #52
                This thread has a section removed from it.
                See the thread on
                'MachineToAppli cation' beyond application level Error
                for the part that has been removed.

                Comment

                Working...