Chapter 16 related question
Hi guys, i am a newbie learning using your book.
as i wanted to make few change to a datagrid i was forced to learn how to write the code associated with it.
however as i wanted to add a toolstrip using code i got lost a bit.
the code i wrote results in a warning an exception
the warning is: Warning 1 'Public Sub New()' in designer-generated type 'db.AccountTypes' should call InitializeComponent method.
and the exception is:
object reference not set to an instance of an object
the code:
'Import Data and SqlClient namespaces
Imports System.Data
Imports System.Data.SqlClient
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Public Class AccountTypes
Inherits System.Windows.Forms.Form
Private toolStripContainer1 As ToolStripContainer
Private toolStrip1 As ToolStrip
Dim objConnection As New SqlConnection _
("server=127.0.0.1;database=db;User Id=sa;Password=pass;")
'Server=myServerAddress;Database=myDataBase;Truste d_Connection=True;
Dim objDataAdapter As New SqlDataAdapter
Dim objDateSet As New DataSet()
' Dim DataGridView1 As String
Private Sub AccountTypes_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Try
' Set the select command properties..
objDataAdapter.SelectCommand = New SqlCommand()
objDataAdapter.SelectCommand.Connection = objConnection
objDataAdapter.SelectCommand.CommandText = "Select AccountTypeID,AccountType,notes From AccountTypes "
objDataAdapter.SelectCommand.CommandType = CommandType.Text
'open the database connection
objConnection.Open()
'fill the dataset with data
objDataAdapter.Fill(objDateSet, "AccountTypes")
'close the database connection
objConnection.Close()
'set the datagrid properties to bind it to the data
' grdAccountTypes.Columns(0).AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCellsExceptHeade r
'
grdAccountTypes.AutoGenerateColumns = True
grdAccountTypes.DataSource = objDateSet
grdAccountTypes.DataMember = "AccountTypes"
grdAccountTypes.Columns(1).AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCellsExceptHeade r
'declare and set the alternating rows style
Dim objAltenateingCellStyle As New DataGridViewCellStyle
objAltenateingCellStyle.BackColor = Color.WhiteSmoke
grdAccountTypes.AlternatingRowsDefaultCellStyle = objAltenateingCellStyle
'change column names and style using the column name
grdAccountTypes.Columns(0).HeaderText = "Accout ID"
grdAccountTypes.Columns(1).HeaderText = "Accout Type"
grdAccountTypes.Columns(2).HeaderText = "Notes"
'clean up
objDataAdapter = Nothing
objConnection = Nothing
Catch es As Exception
MsgBox(es.Message)
End Try
End Sub
Public Sub New()
newsub()
End Sub 'New
<STAThread()> _
Shared Sub Main()
Application.EnableVisualStyles()
Application.Run(New Form1())
End Sub 'Main
Private Sub newsub()
toolStripContainer1 = New System.Windows.Forms.ToolStripContainer()
toolStrip1 = New System.Windows.Forms.ToolStrip()
' Add items to the ToolStrip.
toolStrip1.Items.Add("One")
toolStrip1.Items.Add("Two")
toolStrip1.Items.Add("Three")
' Add the ToolStrip to the top panel of the ToolStripContainer.
toolStripContainer1.TopToolStripPanel.Controls.Add (toolStrip1)
' Add the ToolStripContainer to the form.
Controls.Add(toolStripContainer1)
End Sub 'InitializeComponent
Private Sub BtnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnClose.Click
Close()
End Sub
Private Sub BindingNavigator1_Refre****************ems(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
End Class
|