0

I am facing a problem displaying the records of my table on the visual basic form I have created.

This is my code :

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As    System.EventArgs) Handles Button2.Click
    myconnection = New SqlConnection("server=HOME-PC\SQLEXPRESS;uid=sa;pwd=123;database=college")

    myconnection.Open()
    mycommand = New SqlCommand("SELECT * from demo3)", myconnection)
    Dim mySqlDataAdapter As New SqlDataAdapter(mycommand)
    Dim mydsStudent As New DataSet()

    mySqlDataAdapter.Fill(mydsStudent, "Student")

    ra = mycommand.ExecuteNonQuery()
    MessageBox.Show("Data Displayed" & ra)

    myconnection.Close()
 End Sub
End Class

Note: my database name is "college" , table name is "demo3" . Table contains 2 columns namely name and roll no. How to display the data in those columns on the visual basic form that I have created ?

1 Answer 1

2

You don't need to call execute non query. You can bind the dataset to a DataGridView. Like this

Dim DataGridView1 as new DataGridView()
DataGridView1.DataSource = mydsStudent
'Your table goes here, not sure about the exact propety name, hope it works.
DataGridView1.DisplayMember = "demo3" 
Me.Controls.Add(DataGridView1)
Sign up to request clarification or add additional context in comments.

8 Comments

is it DisplayMemeber or DisplayMember ?
Anuraj : I am getting an error saying DataGridView1 not declared ...how to solve this error ?
@Parth_90 You need to drag and drop DataGridview in the Form. Or you do like try my answer, I modified it.
when i drag and drop datagridview on my form ..do i have to include "me.control.add(datagridview1)" line ?
I m getting error in this line: DataGridView1.DisplayMember = "demo3" ..it says DisplayMember is not a member of DataGridView
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.