Asked by:
Reference to a non-shared member requires an object reference

-
I am creating window forms in Visual basic and each form has different names. As I try to call one form from another form I am getting an error: "Reference to a non-shared member requires an object reference" and I do not know how to resolve this. Below is a copy of one of my forms where I am getting this error.
Imports System.Data.SqlClient Imports System.Data Imports System.Windows.Forms Imports System.Drawing Public Class CustomerRecords Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Me.Close() End Sub Public Sub GetData() Try conn = New SqlConnection(cs) conn.Open() cmd = New SqlCommand("SELECT RTRIM(ID),RTRIM(CustomerID),RTRIM([Name]),RTRIM(Gender), RTRIM(Address),RTRIM(City),RTRIM(State),RTRIM(ZipCode), RTRIM(ContactNo), RTRIM(EmailID),RTRIM(Remarks),Photo from Customer where CustomerType='Regular' order by name", conn) rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection) dgw.Rows.Clear() While (rdr.Read() = True) dgw.Rows.Add(rdr(0), rdr(1), rdr(2), rdr(3), rdr(4), rdr(5), rdr(6), rdr(7), rdr(8), rdr(9), rdr(10), rdr(11)) End While conn.Close() Catch ex As Exception MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End Sub Private Sub frmLogs1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load GetData() End Sub Private Sub btnClose_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click Me.Close() End Sub Private Sub dgw_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dgw.MouseClick Try If dgw.Rows.Count > 0 Then Dim dr As DataGridViewRow = dgw.SelectedRows(0) If lblSet.Text = "Billing" Then Show() Me.Hide() 'Error Starts here. Billings.txtCID.Text = dr.Cells(0).Value.ToString() Billings.txtCustomerID.Text = dr.Cells(1).Value.ToString() Billings.txtCustomerName.Text = dr.Cells(2).Value.ToString() Billings.txtContactNo.Text = dr.Cells(8).Value.ToString() Billings.txtCustomerName.ReadOnly = True Billings.txtContactNo.ReadOnly = True lblSet.Text = "" End If If lblSet.Text = "Quotation" Then Quotation.Show() Me.Hide() Quotation.txtCID.Text = dr.Cells(0).Value.ToString() Quotation.txtCustomerID.Text = dr.Cells(1).Value.ToString() Quotation.txtCustomerName.Text = dr.Cells(2).Value.ToString() Quotation.txtContactNo.Text = dr.Cells(8).Value.ToString() Quotation.txtCustomerName.ReadOnly = True Quotation.txtContactNo.ReadOnly = True lblSet.Text = "" End If End If Catch ex As Exception MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End Sub Private Sub dgw_RowPostPaint(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles dgw.RowPostPaint Dim strRowNumber As String = (e.RowIndex + 1).ToString() Dim size As SizeF = e.Graphics.MeasureString(strRowNumber, Me.Font) If dgw.RowHeadersWidth < Convert.ToInt32((size.Width + 20)) Then dgw.RowHeadersWidth = Convert.ToInt32((size.Width + 20)) End If Dim b As Brush = SystemBrushes.ControlText e.Graphics.DrawString(strRowNumber, Me.Font, b, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y + ((e.RowBounds.Height - size.Height) / 2)) End Sub Private Sub txtCustomerName_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtCustomerName.TextChanged Try conn = New SqlConnection(cs) conn.Open() cmd = New SqlCommand("SELECT RTRIM(ID),RTRIM(CustomerID),RTRIM([Name]),RTRIM(Gender), RTRIM(Address),RTRIM(City),RTRIM(State),RTRIM(ZipCode), RTRIM(ContactNo), RTRIM(EmailID),RTRIM(Remarks),Photo from Customer where CustomerType='Regular' and name like '%" & txtCustomerName.Text & "%' order by name", conn) rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection) dgw.Rows.Clear() While (rdr.Read() = True) dgw.Rows.Add(rdr(0), rdr(1), rdr(2), rdr(3), rdr(4), rdr(5), rdr(6), rdr(7), rdr(8), rdr(9), rdr(10), rdr(11)) End While conn.Close() Catch ex As Exception MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End Sub Private Sub txtCity_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtCity.TextChanged Try conn = New SqlConnection(cs) conn.Open() cmd = New SqlCommand("SELECT RTRIM(ID),RTRIM(CustomerID),RTRIM([Name]),RTRIM(Gender), RTRIM(Address),RTRIM(City),RTRIM(State),RTRIM(ZipCode), RTRIM(ContactNo), RTRIM(EmailID),RTRIM(Remarks),Photo from Customer where CustomerType='Regular' and City like '%" & txtCity.Text & "%' order by city", conn) rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection) dgw.Rows.Clear() While (rdr.Read() = True) dgw.Rows.Add(rdr(0), rdr(1), rdr(2), rdr(3), rdr(4), rdr(5), rdr(6), rdr(7), rdr(8), rdr(9), rdr(10), rdr(11)) End While conn.Close() Catch ex As Exception MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End Sub Sub Reset() txtCustomerName.Text = "" txtContactNo.Text = "" txtCity.Text = "" GetData() End Sub Private Sub btnReset_Click(sender As System.Object, e As System.EventArgs) Handles btnReset.Click Reset() End Sub Private Sub txtContactNo_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtContactNo.TextChanged Try conn = New SqlConnection(cs) conn.Open() cmd = New SqlCommand("SELECT RTRIM(ID),RTRIM(CustomerID),RTRIM([Name]),RTRIM(Gender), RTRIM(Address),RTRIM(City),RTRIM(State),RTRIM(ZipCode), RTRIM(ContactNo), RTRIM(EmailID),RTRIM(Remarks),Photo from Customer where CustomerType='Regular' and ContactNo like '%" & txtContactNo.Text & "%' order by city", conn) rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection) dgw.Rows.Clear() While (rdr.Read() = True) dgw.Rows.Add(rdr(0), rdr(1), rdr(2), rdr(3), rdr(4), rdr(5), rdr(6), rdr(7), rdr(8), rdr(9), rdr(10), rdr(11)) End While conn.Close() Catch ex As Exception MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End Sub Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click Customer.lblUser.Text = lblUser.Text Customer.Reset() Customer.Reset() ShowDialog() End Sub Private Sub frmCustomerRecords1_Load(sender As Object, e As EventArgs) Handles MyBase.Load End Sub End Class
Question
All replies
-
You need to supply this form with an instance of the other form.
When you create an instance of the CustomerRecords form in code, you can supply it with an existing instance of another form. There are a number of ways to do that, from exposing a property on CustomerRecords to creating a constructor method (Sub New) which takes an instance of the other form as a parameter. One way or another you need to supply CustomerRecords with an instance of the Billing and/or Quotations classes.
There are lots of examples of this kind of thing on these forums.
Reed Kimble - "When you do things right, people won't be sure you've done anything at all"
-
Your style of handling a database is very outdated. It means that you have 90% more code than is needed and therefore 90% more chance on an error and need 90% more time to find those errors.
Use dataadapters, linq to sql or the entitityframework.
https://msdn.microsoft.com/en-us/library/bb386876(v=vs.100).aspx
Success
Cor