user635545
user635545

Reputation: 111

Not able to fetch data from sql database in asp.net web application using vb

I am using the below code to fetch data from SQL, I am not getting any error but code is not working on button click

Dim strSQL As String = String.Empty
strSQL = "SELECT * from jhg"
Using connection As New SqlConnection         (ConfigurationManager.ConnectionStrings("xyz").ConnectionString)
    Dim command As New SqlCommand(strSQL, connection)
    connection.Open()
    reader As SqlDataReader = command.ExecuteReader()
    While reader.Read()
        GridView1.DataSource = reader
    End While
'end connection and using close

Upvotes: 0

Views: 74

Answers (2)

Madhawas
Madhawas

Reputation: 391

I think you have to modify your code as follows,

Dim strSQL As String = String.Empty
strSQL = "SELECT * from jhg"
Using connection As New SqlConnection         (ConfigurationManager.ConnectionStrings("xyz").ConnectionString)
    Dim command As New SqlCommand(strSQL, connection)
    connection.Open()
    reader As SqlDataReader = command.ExecuteReader()

    DataTable dt = new DataTable();
    dt.Load(reader);
    GridView1.DataSource = dt;
    GridView1.DataBind();

Upvotes: 1

Chris L
Chris L

Reputation: 2292

You need to DataBind your GridView after providing the datasource:

GridView1.DataBind();

Upvotes: 0

Related Questions