-1

I'm making a program that generates SQL Server code to use it in my VB.NET program.

I have this first form that contains the connection like you see in picture below:

1

The connection works 100%, but in the second form I have two DataGridViews, one for tables and one for fields.

So when I click on any table of DataGridView1 => DataGridView2 show it fields:

2

When I click on DataGridView1 to get the value of ComboBox from Form1 to use it in Form2 I have the following error:

Failed to connect to server.

Code:

Dim frm As New Form2
prd.ServerConnection = New ServerConnection(frm.ComboServer.Text) ' here the error
prd.DGVField(MetroGridTables, MetroGridField)

I use Form1 to make connection and Form2 to make operation.

Bugs
  • 4,491
  • 9
  • 32
  • 41
  • You're saying the connection is stored in `form1`. Yet you declare `frm` as `Form2` and try to read the connection from it. Not only it's the wrong form, it's also most likely empty because it's just been created. Becuase of its confusing nature, I'm not sure what this question is a duplicate of. Among candidates would be http://stackoverflow.com/q/33765071/11683 and http://stackoverflow.com/q/22078219/11683. – GSerg Mar 20 '17 at 12:55
  • yes the connection is stored in form1 the problem just in form2 when i try call the connection server from form1 using this code 'prd.ServerConnection = New ServerConnection(frm.ComboServer.Text) – Ouakala Abdelaaziz Mar 20 '17 at 13:11
  • 1
    You should consider storing the connection outside the forms completely. You're making it difficult to access. – Bugs Mar 20 '17 at 16:24
  • how that !!!! can you explain – Ouakala Abdelaaziz Mar 20 '17 at 17:54

2 Answers2

1

This question is a bit confusing, but this is how I would pass a variable from one form to another.

Create a class /w variable.

Public Class Variables

    Public Shared Property imavariable As String
        Get
            Return m_imavariable 
        End Get
        Set(value As String)
            m_imavariable = value
        End Set
    End Property
    Private Shared m_imavariable As String

End Class

Set the variable from form 1... variables.imavariable = string Read the variable from form2.... string = variables.imavariable

TonyW
  • 766
  • 8
  • 17
1

The simplest way to pass a value from one form to another is to implement the New method on the form you want to pass the value to:

Form1:

Public Class Form1

    Private Sub btnPass_Click(sender As Object, e As EventArgs) Handles btnPass.Click

        Dim form As New Form2(TextBox1.Text)
        form.Show()

    End Sub

End Class

Form2:

Public Class Form2

    Public Sub New(ByVal value As String)

        ' This call is required by the designer.
        InitializeComponent()

        Label1.Text = value

    End Sub

End Class

Screenshot:

enter image description here

Bugs
  • 4,491
  • 9
  • 32
  • 41