Hello,
I'm not new to SQL Server, but definitely new to VB.NET and need assistance populating a DataGrid. I've constructed an app with a connection string and all works fine. I know this by purposely passing the same code I would using Query Analyzer to my app and having no errors, but datagrid never populates. However, when purposely passing the wrong code through my app I get the same errors I normally would in QA.
I'm assuming my DataSource should show "none" in DataGrid1 properties as I've defined them in my connection string? Do I also need to drag a DataView1 to my Form from Server Explorer?
My code is simple and I'm obviously missing something:
Imports System.Data.SqlClient 'First statement at top of form
Private Sub mnuExecute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuExecute.Click
Dim myConnectionString As String = _
"Workstation ID=XXXXXXX;Packet Size=4096;Integrated Security=SSPI;Data Source=(local);Persist Security Info=false;Initial Catalog=Master"
Dim mySqlConnection As SqlConnection = New SqlConnection
mySqlConnection.ConnectionString = myConnectionString
Dim mySqlCommand As SqlCommand = New SqlCommand(txtSql.Text, mySqlConnection)
Dim myReader As SqlDataReader
Try
mySqlConnection.Open()
myReader = mySqlCommand.ExecuteReader(CommandBehavior.CloseConnection)
myReader.Read()
Catch ex As Exception
MsgBox(ex.Message)
Finally
If Not myReader Is Nothing Then
myReader.Close()
End If
End Try
End Sub
Should the connection code by placed in the Form_Load Event?
Your help is greatly appreciated.
Thanks,
David
I'm not new to SQL Server, but definitely new to VB.NET and need assistance populating a DataGrid. I've constructed an app with a connection string and all works fine. I know this by purposely passing the same code I would using Query Analyzer to my app and having no errors, but datagrid never populates. However, when purposely passing the wrong code through my app I get the same errors I normally would in QA.
I'm assuming my DataSource should show "none" in DataGrid1 properties as I've defined them in my connection string? Do I also need to drag a DataView1 to my Form from Server Explorer?
My code is simple and I'm obviously missing something:
Imports System.Data.SqlClient 'First statement at top of form
Private Sub mnuExecute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuExecute.Click
Dim myConnectionString As String = _
"Workstation ID=XXXXXXX;Packet Size=4096;Integrated Security=SSPI;Data Source=(local);Persist Security Info=false;Initial Catalog=Master"
Dim mySqlConnection As SqlConnection = New SqlConnection
mySqlConnection.ConnectionString = myConnectionString
Dim mySqlCommand As SqlCommand = New SqlCommand(txtSql.Text, mySqlConnection)
Dim myReader As SqlDataReader
Try
mySqlConnection.Open()
myReader = mySqlCommand.ExecuteReader(CommandBehavior.CloseConnection)
myReader.Read()
Catch ex As Exception
MsgBox(ex.Message)
Finally
If Not myReader Is Nothing Then
myReader.Close()
End If
End Try
End Sub
Should the connection code by placed in the Form_Load Event?
Your help is greatly appreciated.
Thanks,
David