Results 1 to 15 of 15

Thread: Inheritance problem??

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2003
    Location
    Europe, Belgium
    Posts
    84

    Inheritance problem??

    Hi again,

    I'm having a problem with inheritance (i suppose). I have a form (frmsearch) in which the user can type some keywords and based on the input I create an SQL-string for an oledbdataadapter. With this dataadapter I fill a dataset.

    Everything works fine, but now I want to display the results (the dataset) in a datagrid in another form (frmShow). I don't know how to do this. I tried to inherit the frmsearch in frmShow but this shows also all the controls in frmShow and this is not what I want (it is also not possible to delete them).

    Should I use another strategy? If so, can you help me with some VB.NET code to accomplish this?

    Many thanks

    Tom

  2. #2
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681
    You could just pass the dataset (ByRef) to the new form for display. You could add a parameter to the new constructor of the new for to take the dataset.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Oct 2003
    Location
    Europe, Belgium
    Posts
    84
    Thanks crazycoder,

    Any suggestion for the corresponding VB.NET code?? I'm new to VB.NET programming

    Greetz

    tom

  4. #4
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681
    In your new form you would have:
    VB Code:
    1. Dim dsMyDataset As Dataset
    2.  
    3. Public Sub New(ByRef dsDataset As DataSet)
    4.         MyBase.New()
    5.  
    6.         'This call is required by the Windows Form Designer.
    7.         InitializeComponent()
    8.  
    9.         'Add any initialization after the InitializeComponent() call
    10.         dsMyDataset = dsDataset
    11. End Sub

    Then you can use the dsMyDataset in your form whereever you need it.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Oct 2003
    Location
    Europe, Belgium
    Posts
    84
    Thanks,

    But I still don't get it. I hope I'm not asking too stupid questions.

    If I have to write this code in the new form, where is the dataset called? Don't I need a 'property'?

    Thanks,

    Tom

  6. #6
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681
    You stated that you are populating your dataset (using an adapter) in your first form. So if you have your dataset already, then you can just pass it to the next form and use it. Alternatively, you could populate the dataset in the second form, but then you would have to pass all your parameters that the user selected into the second form.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Oct 2003
    Location
    Europe, Belgium
    Posts
    84
    VBCrazycoder,

    You are correct, I stated that I created the dataset (dsSearch) in the first form and I have to pass it to the second (or called form).

    But what I don't understand is that by putting the following code

    --------------------------------------------------------------------------------
    Dim dsMyDataset As Dataset

    Public Sub New(ByRef dsDataset As DataSet)
    MyBase.New()

    'This call is required by the Windows Form Designer.
    InitializeComponent()

    'Add any initialization after the InitializeComponent() call
    dsMyDataset = dsDataset
    End Sub


    In the called form, you can pass the dataset from the first form to the second form. Does it have something todo with 'initialize'?

    I'm stuck.


    Tom

  8. #8
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681
    All you are doing is passing a reference of the dataset to the second form, and storing it in a local variable so you can use it in code later on, like in the load event.

    This is the same as passing a variable to a method.
    VB Code:
    1. Public Sub SomeMethod(ByVal myValue As Integer)
    2. ....
    3. End Sub

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Oct 2003
    Location
    Europe, Belgium
    Posts
    84
    I have tried it, but it still doesn't work.

    The dataset in the first form is called datasetsearch.

    The code for the second form is:

    Public Class FormWeergaveZoek
    Inherits Windows.Forms.Form

    Dim dsMyDataset As DataSet
    Public Sub New(ByRef Datasetzoeken11 As DataSet)
    MyBase.New()

    'This call is required by the Windows Form Designer.
    InitializeComponent()

    'Add any initialization after the InitializeComponent() call
    dsMyDataset = Datasetzoeken11
    End Sub


    Private Sub FormWeergaveZoek_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Me.DataGrid1.DataMember = "tblCD"
    Me.DataGrid1.DataSource = Me.dsMyDataset

    End Sub
    End Class


    The datagrid in the second form is not filled up with the required dataset?

    Do you know why?

    Tnaks

    TOm

  10. #10
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681
    make sure you are populating the dataset in the first form. You can verify that you have data by doing a dsMyDataset.Tables(0).Rows.Count() - (i think that is the right code - and I think you can substitute the 0 for the table name if needed).

    Also, I usually do:
    VB Code:
    1. Me.DataGrid1.DataSource = Me.dsMyDataset.Tables(0)

    and not set the datamember explicitly.

    Also, I am assuming that the first form is still open when you call the second form. If you are closing the first form then you may need to pass the dataset by value.

    Try these suggestions...

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Oct 2003
    Location
    Europe, Belgium
    Posts
    84
    thanks VBcrazycoder,

    I tried your suggestions:

    -I checked if the dataset is filled in the first form: it is because i have integrated a datagrid based on the dataset in the first form and this datagrid gets filled.

    -The first form is still open when the second one is displayed

    -I adapted the code to fill the datagrid as you suggested

    But when I compile and run the program I get the following error when the second form opens:

    "An unhandled exception of type 'System.NullReferenceException' occurred in Zoekprogramma.exe Additional information: Object reference not set to an instance of an object."


    Can you help me please?

    Thanks, i appreciate your help

  12. #12
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    IN the first form this is how you should be passing the dataset to the second:
    VB Code:
    1. 'this would be in the first form where it opens the 2nd
    2. 'this is where it actually gets passed in the constructor or 'New' statement
    3. Dim frm As New FormWeergaveZoek(datasetsearch)
    4. frm.Show()'or ShowDialog

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Oct 2003
    Location
    Europe, Belgium
    Posts
    84
    Thanks Edneis

    Your suggestion works, I just have to adapt the datagrid in the second form a little bit because it doesn't directly shows the information (the user still has to click on the +sign).

    Thank you very much

  14. #14
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    To solve the 'have to click the +' problem then just use one of the tables in the dataset as the datasource instead of the dataset itself.

    DataGrid1.DataSource=datasetSearch.Tables(0)

  15. #15
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681
    Or set the AllowNavigation property of the grid to false - but you still have to set the datasource to the specific table.

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