steviebernstein
Member
- Joined
- Oct 9, 2009
- Messages
- 14
- Programming Experience
- Beginner
Background:
OK, so I have two forms. Form1 is a bunch of user interface objects, Form2 is a datagridview. I want to make it so that when I click a button Page1 the program will store the user information and then create a SQL Query to fill From2.
Problem:
When I click the button the other form is created, but the grid does not fill. The weird thing is that while trouble shooting this I created a button on form 2 that uses all of the same subroutines as the button on form 1, but for some reason the button on form one does not work, while the one on form 2 does.
Code for form1 button (doesn't work):
code for form2 button (works):
Code within the sub:
OK, so I have two forms. Form1 is a bunch of user interface objects, Form2 is a datagridview. I want to make it so that when I click a button Page1 the program will store the user information and then create a SQL Query to fill From2.
Problem:
When I click the button the other form is created, but the grid does not fill. The weird thing is that while trouble shooting this I created a button on form 2 that uses all of the same subroutines as the button on form 1, but for some reason the button on form one does not work, while the one on form 2 does.
Code for form1 button (doesn't work):
VB.NET:
Public Sub btnCompile_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles btnCompile.MouseClick
Dim frmInstance As Form3
frmInstance = New Form3
Dim frmInstance1 As MDIParent1
frmInstance1 = New MDIParent1
MDIParent1.CreateForm3()
Form3.Load_and_Manupulate()
End Sub
code for form2 button (works):
VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Call populateDataGridView1()
End Sub
Code within the sub:
VB.NET:
Public Sub populateDataGridView1()
Dim cn As New OdbcConnection("Dsn=INSPDBP1_ms;uid=" & ID & ";server=INSPDBP1;pwd=" & pass)
Try
Dim SQL As String
'build the SELECT statement
SQL = String.Format("SELECT ........")
Dim cmd As New OdbcCommand(SQL, cn)
'open the connection
cmd.Connection.Open()
'get the DataReader object from command object
Dim rdr As OdbcDataReader = cmd.ExecuteReader()
'check if it has any rows
If rdr.HasRows Then
With Me.DataGridView1
'remove existing rows from grid
.Rows.Clear()
'get the number of columns
Dim ColumnCount As Integer = rdr.FieldCount
'add columns to the grid
For i As Integer = 0 To ColumnCount - 1
.Columns.Add(rdr.GetName(i), rdr.GetName(i))
Next
.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.ColumnHeader
'loop through every row
While rdr.Read
'get all row values into an array
Dim objCells(ColumnCount - 1) As Object
rdr.GetValues(objCells)
'add array as a row to grid
.Rows.Add(objCells)
End While
End With
Else
'display message if no rows found
MessageBox.Show("Not found")
Me.dataGridView1.Rows.Clear()
End If
'clear up the resources
rdr.Close()
Catch ex As Exception
'display if any error occurs
MessageBox.Show("Error: " & ex.Message)
'close the connection if it is still open
If cn.State = ConnectionState.Open Then
cn.Close()
End If
End Try
End Sub
Last edited by a moderator: