-6
Public Class Form3
    Inherits Windows.Forms.Form
    Public F1 As Form1
    Dim S1 As String = F1.TextBox2.Text


    Public Sub New()
        InitializeComponent()
        BindGrid()
    End Sub
    Private Sub bindgrid()


        Dim con As New SqlConnection
        '/ Dim cmd As New SqlCommand
        con.ConnectionString = "server=hcl-pc\SQLEXPRESS;Initial Catalog=UBGB_HRMS;Integrated Security=True"


        ''/Dim S2 As String

        '/ S1 = F1.TextBox1.Text
        '/ S2 = F1.TextBox2.Text

        ''/If String.IsNullOrEmpty(S1) Then
        '/Return
        '/ End If
        Using cmd As New SqlCommand("SELECT *FROM emp_personal_details where emp_adhaar=s1", con)
            cmd.CommandType = CommandType.Text
            Using sda As New SqlDataAdapter(cmd)
                Using dt As New DataTable()
                    sda.Fill(dt)
                    DataGridView1.DataSource = dt
                End Using
            End Using
        End Using

    End Sub
    Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick

    End Sub
End Class

I am using multiple windows form and assigning the value of form1 in form3. Form1 is required for the variable s1. While debugging, I am getting the error:

null reference was unhanded

Unihedron
  • 10,902
  • 13
  • 62
  • 72

1 Answers1

2

I think the problem is that you are assigning the variable F1 a type, but not instantiating it. Try changing your declaration to:

Public F1 As New Form1 

However, this will create a new, blank form1, which won't have anything in its textbox (probably) which I am assuming is not what you want to do. You really want to declare the variable F1 somewhere else so that you can refer to the current instance of it from form3.

Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
Tim
  • 120
  • 1
  • 3
  • 8
  • @Error 404 Please ***do not*** make question edits if you have no idea what you're talking about; in particular your edit comprised wholly of surrounding a variable in the keyboard formatting tags. – AStopher Dec 12 '14 at 16:41
  • @Tim thnx bro i want to assign the value of textbox of form1 which have been already created jn variable of form3 which is s1.... and there only i am getting the error – ritesh kumar Dec 12 '14 at 17:22
  • @Tim bro now the null reference problem is out ,but when i am assigning a value it is not being stored what to do plz suggest – ritesh kumar Dec 16 '14 at 05:47
  • You need to put the line public f1 as new form1 some point before you load form 2. (at project startup). Without seeing more of your project it is difficult to advise you. – Tim Dec 16 '14 at 10:32