Need help with Datasets RRS feed

  • Question


    hi! i’m new to .Net programming so forgive me if my question seem trivial. I’m making a program that generates reports by connecting to a remote db server. I am having problems with the dataset I created. Below is my code:



    Public Class Form1
        Inherits System.Windows.Forms.Form


    #Region " Windows Form Designer generated code "

        Public Sub New()

            'This call is required by the Windows Form Designer.

            '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
                End If
            End If
        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 SqlConnection1 As System.Data.SqlClient.SqlConnection
        Friend WithEvents SqlDataAdapter1 As System.Data.SqlClient.SqlDataAdapter
        Friend WithEvents DataSet11 As Database.DataSet1
        Friend WithEvents Button1 As System.Windows.Forms.Button
        Friend WithEvents SqlSelectCommand1 As System.Data.SqlClient.SqlCommand
        Friend WithEvents DataSet21 As Database.DataSet2
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
            Me.DataGrid1 = New System.Windows.Forms.DataGrid
            Me.DataSet11 = New Database.DataSet1
            Me.SqlConnection1 = New System.Data.SqlClient.SqlConnection
            Me.SqlDataAdapter1 = New System.Data.SqlClient.SqlDataAdapter
            Me.Button1 = New System.Windows.Forms.Button
            Me.SqlSelectCommand1 = New System.Data.SqlClient.SqlCommand
            Me.DataSet21 = New Database.DataSet2
            CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
            CType(Me.DataSet11, System.ComponentModel.ISupportInitialize).BeginInit()
            CType(Me.DataSet21, System.ComponentModel.ISupportInitialize).BeginInit()
            Me.DataGrid1.DataMember = ""
            Me.DataGrid1.DataSource = Me.DataSet21.Temptable
            Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
            Me.DataGrid1.Location = New System.Drawing.Point(40, 160)
            Me.DataGrid1.Name = "DataGrid1"
            Me.DataGrid1.Size = New System.Drawing.Size(208, 80)
            Me.DataGrid1.TabIndex = 0
            Me.DataSet11.DataSetName = "DataSet1"
            Me.DataSet11.Locale = New System.Globalization.CultureInfo("ja-JP")
            Me.SqlConnection1.ConnectionString = "workstation id=""MyPC"";packet size=4096;user id=myuserid;data source=REMOTESERVER;" & _
            "persist security info=False;initial catalog=sourcedb"
            Me.SqlDataAdapter1.SelectCommand = Me.SqlSelectCommand1
            Me.SqlDataAdapter1.TableMappings.AddRange(New System.Data.Common.DataTableMapping() {New System.Data.Common.DataTableMapping("Table", "Temptable", New System.Data.Common.DataColumnMapping() {New System.Data.Common.DataColumnMapping("Field1", "Field1"), New System.Data.Common.DataColumnMapping("Field2", "Field2")})})
            Me.Button1.Location = New System.Drawing.Point(104, 24)
            Me.Button1.Name = "Button1"
            Me.Button1.TabIndex = 1
            Me.Button1.Text = "Button1"
            Me.SqlSelectCommand1.CommandText = "SELECT Field1, Field2 FROM dbo.Temptable WITH (nolock)"
            Me.SqlSelectCommand1.Connection = Me.SqlConnection1
            Me.DataSet21.DataSetName = "DataSet2"
            Me.DataSet21.Locale = New System.Globalization.CultureInfo("ja-JP")
            Me.AutoScaleBaseSize = New System.Drawing.Size(5, 12)
            Me.ClientSize = New System.Drawing.Size(284, 263)
            Me.Name = "Form1"
            Me.Text = "Form1"
            CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
            CType(Me.DataSet11, System.ComponentModel.ISupportInitialize).EndInit()
            CType(Me.DataSet21, System.ComponentModel.ISupportInitialize).EndInit()

        End Sub

    #End Region

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

            SqlDataAdapter1.Fill(DataSet21, "Temptable")
            DataGrid1.CaptionText = "Results"
            DataGrid1.DataSource = SqlDataAdapter1

        End Sub

        Private Sub DataGrid1_Navigate(ByVal sender As System.Object, ByVal ne As System.Windows.Forms.NavigateEventArgs) Handles DataGrid1.Navigate

        End Sub
    End Class


    When I press F5 and try to run the program, I’m getting this error:


    ”An unhandled exception of type ’System.Data.SqlClient.SqlException’ occured in”


    and this code is highlighted in green:




    I would greatly appreciate it if someone can tell me how to fix it and also give me pointers in working with datasets.



    Thanks in advance!

    Saturday, November 3, 2007 7:25 AM