?
Solved

Datagridview fails on form load evemt

Posted on 2009-07-03
5
Medium Priority
?
319 Views
Last Modified: 2013-11-08
Hi All

I am having a bit of a weird issue trying to create a form that displays only a datagridview. I am using VB,net 2009

I created a sub routine to run when the forms new function is called, I pass this function thre parameters which are then fed into a stored procedure which will in turn populate the dataset and i hope that datagrid eventually.

The problem is that when I try and load the data from the dataset into the data grid i get an error saying that the object refrence is not set to an instance of the object. I am confused as the code for the datagridview is identical (as far as i can see) to another sub routing which works perfectly.

Any ideas on this one?

incidentally I get the same error when i put the dataset code in the form load event and if i put the binding code in the same routine as the dataset load sub

thanks in advance
Form opening code:
    Private Sub EURb_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EURb.Click
        Dim d1 As Date = DatePicker.Value.AddMonths(-1).AddDays(-DatePicker.Value.Day)
        Try
            If Not IsNothing(f3) Then
                If Not f3.IsDisposed Then
                    f3.WindowState = FormWindowState.Normal  ' Optional
                    f3.BringToFront()  '  Optional
                Else
                     f3 = New HistoryDisplay("EUR", "CASH", d1)
                     f3.Show()
                End If
            Else
                f3 = New HistoryDisplay("EUR", "CASH", d1)
                f3.Show()
             End If
        Catch ex As Exception
            MsgBox(ex.Message, , "Load Report")
        End Try
    End Sub
 
The new() subroutine:
Sub New(ByVal CCY As String, ByVal Table As String, ByVal Date2 As Date)
        'CCY1 = CCY
        'Table1 = Table
        'Date1 = Date2
        Dim CONNSTR As String = "integrated security=SSPI;data source='CFPD8-CA\SQLEXPRESS';initial catalog=CFltd"
        Dim cn As New SqlConnection(ConnStr)
         datrades = New SqlDataAdapter("CashLadderGetFullDetails", cn)
        datrades.SelectCommand.CommandType = CommandType.StoredProcedure
        datrades.SelectCommand.Parameters.AddWithValue("@Date", Date2.Date)
        datrades.SelectCommand.Parameters.AddWithValue("@CCY", CCY)
        Try
            cn.Open()
            datrades.Fill(dsTrades, "Trades")
        Catch ex As Exception
            MsgBox(ex.Message, , "Loading Data")
        Finally
            cn.Close()
        End Try
    End Sub
 
   Private Sub HistoryDisplay_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
         DataGrid1.DataSource = dsTrades
        DataGrid1.DataMember = "Trades"
    End Sub

Open in new window

0
Comment
Question by:ChrisAllison
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 23

Accepted Solution

by:
Jens Fiederer earned 501 total points
ID: 24812751
I suspect at the time the form's constructor is called, its controls have not been created yet and can't be populated.

You SHOULD be able to bind when the form is loaded.

0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 498 total points
ID: 24815364
Did you set dsTrades to a new object before you passed it to Fill() (either in the constructor or statically)?
// Statically
Dim dsTrades As New DataSet()  // Initialized
 
Sub New(ByVal CCY As String, ByVal Table As String, ByVal Date2 As Date)
...
 
// Constructor
Dim dsTrades As DataSet()  // declared-only
 
Sub New(ByVal CCY As String, ByVal Table As String, ByVal Date2 As Date)
    dsTrades = new DataSet()

Open in new window

0
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 501 total points
ID: 24821159
Place your code inside Load or Shown event of the form instead of new. Your code inside new will be executed before ALL other code in the form hence there are no controls created yet and i think at that the stage the form itself is not created yet.
0
 

Author Closing Comment

by:ChrisAllison
ID: 31599520
thanks for the comments chaps, I got around the problem by placing the code in the load event. My issue with that origianlly was passing data to the stored procedure to dictate what data I load.

the method i attempted to try did work in another 2008 project, which has puzzled me, at least my workaround works!
thanks for the help guys
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question