I am facing a problem displaying the result of my “select *” query on the form . Here is my code:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
myconnection = New SqlConnection("server=PARTH-PC\SQLEXPRESS;uid=sa;pwd=parth;database=college")
myconnection.Open()
mycommand = New SqlCommand("SELECT * from [demo3]",myconnection)
Dim mySqlDataAdapter As New SqlDataAdapter(mycommand)
Dim mydsStudent As New DataSet()
DataGridView2.DataSource = mydsStudent
Me.Controls.Add(DataGridView2)
ra = mycommand.ExecuteNonQuery()
MessageBox.Show("Data displayed" & ra)
myconnection.Close()
End Sub
But when I write this code and run my form I am not able to see any records present in my database tables being displayed on my form. I have inserted a DataGridView on my form . What changes should I do to my code ? Can Anyone help me ?
Thanks in advance
When you are working with a DataAdapter, you need to call the
Fill()method for it to fill aDataSet/DataTable. After you initialize theDataSet, make this call:Also, take out your
ExecuteNonQuery()call.EDIT: Try something like this.