Found the following interesting discussion in the Newsgroups:
| Where did my Dataset Go? by:Wayne Wengert | |
| I have a VB App (VSNET 2003) in which, during form load, I create a dataset (ds1) and then populate a datagrid by binding to the dataset. That works fine. The form has a button which, when clicked, initiates an export process. When the button click event opens, the dataset is "Nothing". The dataset is Dim'ed to be available throughout the form. Other variables dim'ed at that same point are fine and can be used within the button click event. What is closing my dataset? Wayne | |
| Reply: by:Anonymous | |
| Web app or windows app? could we a code example? B | |
| Reply: by:Wayne Wengert | |
| Sorry - It is a Windows app. Portion of code follows: ==================================== Public Class Form1 Inherits System.Windows.Forms.Form Dim cn As SqlConnection Dim strSelectedRegionalID = "Denver" Dim strSQL As String Dim ds1 As DataSet Dim ShowSets As DataRelation #Region " Windows Form Designer generated code " Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call End Sub 'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub 'Required by the Windows Form Designer Private components As System.ComponentModel.IContainer 'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid Friend WithEvents btnXMLExport As System.Windows.Forms.Button Friend WithEvents SaveFileDialog1 As System.Windows.Forms.SaveFileDialog Friend WithEvents DataGrid2 As System.Windows.Forms.DataGrid <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.DataGrid1 = New System.Windows.Forms.DataGrid Me.btnXMLExport = New System.Windows.Forms.Button Me.SaveFileDialog1 = New System.Windows.Forms.SaveFileDialog Me.DataGrid2 = New System.Windows.Forms.DataGrid CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit() CType(Me.DataGrid2, System.ComponentModel.ISupportInitialize).BeginInit() Me.SuspendLayout() ' 'DataGrid1 ' Me.DataGrid1.DataMember = "" Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText Me.DataGrid1.Location = New System.Drawing.Point(12, 39) Me.DataGrid1.Name = "DataGrid1" Me.DataGrid1.Size = New System.Drawing.Size(472, 160) Me.DataGrid1.TabIndex = 0 ' 'btnXMLExport ' Me.btnXMLExport.Location = New System.Drawing.Point(186, 429) Me.btnXMLExport.Name = "btnXMLExport" Me.btnXMLExport.Size = New System.Drawing.Size(104, 23) Me.btnXMLExport.TabIndex = 1 Me.btnXMLExport.Text = "Export to XML" ' 'DataGrid2 ' Me.DataGrid2.DataMember = "" Me.DataGrid2.HeaderForeColor = System.Drawing.SystemColors.ControlText Me.DataGrid2.Location = New System.Drawing.Point(12, 239) Me.DataGrid2.Name = "DataGrid2" Me.DataGrid2.Size = New System.Drawing.Size(472, 146) Me.DataGrid2.TabIndex = 2 ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(493, 457) Me.Controls.Add(Me.DataGrid2) Me.Controls.Add(Me.btnXMLExport) Me.Controls.Add(Me.DataGrid1) Me.Name = "Form1" Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen Me.Text = "Form1" CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit() CType(Me.DataGrid2, System.ComponentModel.ISupportInitialize).EndInit() Me.ResumeLayout(False) End Sub #End Region Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Load the data .......................... DataGrid1.DataSource = ds1 DataGrid1.DataMember = "CGShows" '<= ds1 is fine here /'Wait for user to Click Button Private Sub btnXMLExport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXMLExport.Click Dim filename As String = "myXmlDoc.xml". Dim myFileStream As New System.IO.FileStream(filename, System.IO.FileMode.Create) ds1.WriteXml(myFileStream, XmlWriteMode.WriteSchema) ' <= ds1 is "Nothing End Sub | |
Posted by Xander Zelders

0 Comments:
Post a Comment
<< Home