Showing posts with label protected. Show all posts
Showing posts with label protected. Show all posts

Wednesday, March 28, 2012

This code doesnt work with VS2005

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
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.

Thursday, March 22, 2012

thread being aborted?

Protected Sub btnLogon_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim connstring As New SqlConnection("Server=kabunyawan\sqlexpress;Database=ojt;Trusted_Connection=True;")
Dim rs1 As New SqlDataAdapter("select * from wall where uname = '" & txtuname.Text & "' and pword = '" & txtpword.Text & "'", connstring)
Dim ds As New DataSet
Dim tmpStr As String
rs1.Fill(ds, "wall")
Try
tmpStr = ds.Tables("wall").Rows(0).Item("lvl")
If tmpStr = "admin" Then
Response.Redirect("contact.aspx")
Else
Response.Write(tmpStr)
Response.Write("admin")
Response.Write("1111111111111111")
End If
Catch ex As Exception
Response.Cookies("errmsg").Value = "Invalid username/password. Please contact the admins for assisstance."
Response.Redirect("merror.aspx")
End Try
End Sub

i am having error on response.redirect("contact.aspx").. but my other webforms are working..Problem Solved.. that error was caused by the "try-catch" part.. I've read that response.redirect will always give out an error if placed within a try-catch.. now i know!
Now read that Response.Write is a bad thing on ASP.NET pages. :)