I'm trying to learn how to execute a stored procedure in .net. I'm using a textbook example and yet the code gives me the following error when run:
"An unhandled exception of type 'System.InvalidOperationException' occurred in system.data.dll
Additional information: ExecuteReader: Connection property has not been initialized."
Here is the actual code. Can you tell me what the problem is? Thanks.
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
SqlConnection1.Open()
SqlCommand1.CommandType = CommandType.StoredProcedure
SqlCommand1.CommandText = "OrdersPerCustomer"
If SqlCommand1.Parameters.Count = 0 Then
SqlCommand1.Parameters.Add(New _
System.Data.SqlClient.SqlParameter("@CustomerID", SqlDbType.NChar))
SqlCommand1.Parameters(0).Direction = ParameterDirection.Input
End If
SqlCommand1.Parameters(0).Value = ListBox1.SelectedValue.ToString
Dim SQLReader As System.Data.SqlClient.SqlDataReader
SQLReader = SqlCommand1.ExecuteReader
While SQLReader.Read
txtOrders.Text = SQLReader.Item("Total Orders").ToString
txtAmount.Text = SQLReader.Item("Total Amount").ToString
End While
SqlConnection1.Close()
End Sub
End Class