Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DataGrid not populating 1

Status
Not open for further replies.

Idokyoku2

Technical User
May 16, 2004
57
US
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
 
You've showed the code for opening a connection and navigating to the first record in the table. Where do you populate the datagrid? Are you populating the datagrid with code or using the GUI?
 
Well, being such a newbie to VB I'd like to populate the datagrid with code. Would you be mind pointing me in the right direction, a jump start of sample code?

Thanks for the reply.
David
 
If you use a DataTable (e.g. faq855-5662) you can use the DataSource property to bind the data to the DataGrid rather than actually looping through the rows (like the above example shows). e.g.
Code:
DataGrid1.DataSource = MyDataTable

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Ca8msm,
Terrific FAQ, Rated it a 10!

As I'd mentioned earlier, I am a newbie to VB and greatly appreciate your time. You've earned a STAR!


Hey Beef,
I did search all of what you've suggested, but apparently not well enough.

Thanks,
David
 
ca8msm,
I just tweaked a little code per the FAQ and things are working perfect. I'm so stinkin' pleased, your getting another star. If it weren't for the code sample you provided, this would not have worked for me.

Thanks a million!

David
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top