Dim MyConn As New SqlConnection("Server=localhost;UID=sa;PWD=;database=Northwind")
MyConn.Open()
Dim StrSelect As String = "Select * FROM Customers"
Dim SqlKu As SqlCommand = New SqlCommand(StrSelect, MyConn)
Dim MyReader As SqlDataReader
MyReader = SqlKu.ExecuteReader()
DataList1.DataSource = MyReader
DataList1.DataBind()
MyReader.Close()
MyConn.Close()
End Sub
There's nothing error occur.. just blank, please help me to fix it.
Is it with VS2005 we can write codes manually just like we do with VS2003?
Thanks.
.NET Framework 2.0 has a lot of new controls that cut down on the amount of code you have to write. Two things I recommend in the code you provided are to open the connection as late as possible and to use try / finally or try / catch / finally in your code.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim MyConn As SqlConnection = Nothing
Dim MyReader As SqlDataReader = Nothing
Try
MyConn = New SqlConnection("Server=localhost;UID=sa;PWD=;database=Northwind")
Dim StrSelect As String = "Select * FROM Customers"
Dim SqlKu As New SqlCommand(StrSelect, MyConn)
MyConn.Open()
MyReader = SqlKu.ExecuteReader()
DataList1.DataSource = MyReader
DataList1.DataBind()
Finally
If Not MyReader Is Nothing Then
If Not MyReader.IsClosed Then
MyReader.Close()
End If
End If
If Not MyConn Is Nothing Then
If MyConn.State = ConnectionState.Open Then
MyConn.Close()
End If
End If
End Try
End Sub
If the above code doesn't work, then there is some issue with your connection to the database or the query itself.
HTH,
Ryan
Hi,
another interesting read is this:Open() late, Close() early
Grz, Kris.
0 comments:
Post a Comment