简体   繁体   中英

DataGridView Not Showing Up

I'm trying to show a query with my results in a datagridview. My connection works, i just can't seem to get the results to show.I'm throwing an error that says I havent initialized the ExecuteReader:Connection property here is my code My question is: Am i not initializing the ExecuteReader, correctly? I'm new to vb and I'm trying to understand what i'm doing wrong here.

Public Sub Form1_Load(sender as Object, e as EventArgs) Handles MyBase.Load

    connection string = "Data Source" ' I'm not including my data source for the question 
    SqlConnect = New Connection (connectionString)

    SqlConnect.Open()

    strQuery = "Select Book Number"
       "FROM Books"
       "Where ID Code"
    SqlQuery.CommandText = strQuery
    SqlDataReader = SQLQuery.ExecuteReader()
    SqlDataReader.Read()
    SqlQuery.Connection = SqlConnect
    SQLQuery.CommandText = strQuery


    SQlConnect. Close()
End Sub 

To address the specific issue, you're getting an error message that you haven't set the Connection property. Here's your code:

SqlDataReader = SQLQuery.ExecuteReader()
SqlDataReader.Read()
SqlQuery.Connection = SqlConnect

Would you expect to be able to walk through a door and then open it afterwards? I would hope not. So why would you expect to be able to execute a command against a database and then tell it what database connection to use after doing so?

Apart from that, there's nothing in that code related to a DataGridView , so this question has nothing to do with that. Research should show that the usual way to display data in a DataGridView is to populate a DataTable and then bind it to the grid. You need to look into how to populate a DataTable . You can do that with a data reader but you certainly won't be calling read and, if you want to edit and save the data, you should be using a data adapter instead.

put your SqlDataReader = SQLQuery.ExecuteReader() and SqlDataReader.Read() under SqlQuery.Connection = SqlConnect

You should specify the connection first before execuitng your Reader

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM