<pre lang="vb">Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click Dim sqlConn As New SqlConnection Dim sqlCmd As New SqlClient.SqlCommand Dim sqlReader As SqlDataReader If TextBox1.Text = "" Then MsgBox("A centre code needs to be provided...") End If If TextBox1.Text <> "" Then 'Telling the system the location of the database. sqlConn.ConnectionString = "server=servername;Initial Catalog=dbname;Trusted_Connection=yes" 'Here we are opening the connection to the database. sqlConn.Open() MsgBox("CONNECTION OPEN..." & sqlConn.ConnectionString) 'This is to say that sqlCmd is a stored procedure. sqlCmd.CommandType = System.Data.CommandType.StoredProcedure 'This is creating the command to execute the stored procedure based on the information given in the connection string. sqlCmd = sqlConn.CreateCommand 'The command is triggered to execute the stored procedure which grabs all information for the specific centre. sqlCmd.CommandText = "exec GetAllInformation '" & TextBox1.Text & "' " MsgBox("PROCEDURE EXECUTED...") sqlReader = sqlCmd.ExecuteReader() If (sqlReader.HasRows) Then While (sqlReader.Read()) 'This grabs the details for the specific centre from the database using the data reader. GridView1.DataSource = sqlReader GridView1.DataBind() End While Else MsgBox("THERE IS NO DATA TO SELECT...") End If MsgBox("INFORMATION GRABBED...") 'This is closing the connection to the database once we have finished with it. sqlConn.Close() End If End Sub
If (sqlReader.HasRows) Then GridView1.DataSource = sqlReader GridView1.DataBind()
Using Reader As SqlClient.SqlDataReader = sqlCmd .ExecuteReader() Dim dt As New DataTable dt.Load(Reader) If dt.Rows.Count > 0 Then DataGridView1.DataSource = dt End If End Using
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)