0

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

2 Answers 2

1

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();
Sign up to request clarification or add additional context in comments.

1 Comment

while i am trying to use datatable it is showing me an error
0

You need to DataBind your GridView after providing the datasource:

GridView1.DataBind();

Comments

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.