Showing posts with label dim. Show all posts
Showing posts with label dim. 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.

Monday, March 26, 2012

This easy 2 lines in 1 line of code

Dim myDate As Date = Now
todaysdate.text = FormatDateTime(myDate,vblongdate)

cant this be done in 1 line of code?No it cant.
How about todaysdate.text = FormatDateTime(Now,vblongdate). You can eliminate the variable entirely.. of course if you need the variable then you must declare it and initialize it on a seperate line.

I don't know of any languages that would let you declare a variable in a function call. If there are it's probably bad form to do so anyways.
Sure.

FormatDateTime( DateTime.Now, vblongdate )
thx fellas

This drives me crazy

Hello,
I have this:
Select Case Session("culture4web")
Case "pt-PT"
Dim topMenuItems() As String = {"Home", "Conteúdos", "Xtras", " ", "Web", "Company"}
Case "en-GB"
Dim topMenuItems() As String = {"Home", "Conteúdos", "Xtras", " ", "Web", "Company"}
End Select
Call_Function (topMenuItems)
I am having problems because the variable disapears outside of Case Statement.
How can I solve this?
Thanks,
Miguel

Always try and define your variables outside conditional statements...
Dim topMenuItems() As String
Select Case Session("culture4web")
Case "pt-PT"
topMenuItems()= {"Home", "Conteúdos", "Xtras", " ", "Web", "Company"}
Case "en-GB"
topMenuItems() = {"Home", "Conteúdos", "Xtras", " ", "Web", "Company"}
End Select

.NET introduced an extended scoping model, block-level variables are now block-local.
Declare your dependant variables outside theSelect...End Select block:
Dim topMenuItem() As String
Select Case Session("culture4web")
Case "pt-PT"
topMenuItems = {"Home", "Conteúdos", "Xtras", " ", "Web", "Company"}
End Select
Call_Function(topMenuItems)

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. :)