Showing posts with label sub. Show all posts
Showing posts with label sub. 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 is the asp.net code

Sub BindDropBox()
conPos.Open()
oSevenIssuesCMD = New SqlCommand("SevenIssues", conPos)
oSevenIssuesCMD.CommandType = CommandType.StoredProcedure
oSevenIssuesCMD.Parameters.Add(New SqlParameter("@dotnet.itags.org.productName", SqlDbType.Va
rChar, 50))
oSevenIssuesCMD.Parameters("@dotnet.itags.org.productName").Value = "TESTPRODUCT"
oSevenIssuesReader = oSevenIssuesCMD.ExecuteReader
DropDownList1().DataSource = oSevenIssuesReader
DropDownList1.DataTextField = "Cover_Date"
DropDownList1.DataValueField = "Cover_Date"
DropDownList1.DataBind()
conPos.Close()
End Sub
This is the error
Server Error in '/TEST_application' Application.
----
--
Runtime Error
Description: An application error occurred on the server. The current custom
error settings for this application prevent the details of the application
error from being viewed remotely (for security reasons). It could, however,
be viewed by browsers runni
ng on the local server machine.
Details: To enable the details of this specific error message to be viewable
on remote machines, please create a <customErrors> tag within a "web.config
" configuration file located in the root directory of the current web applic
ation. This <customErrors>
tag should then have its "mode" attribute set to "Off".
<!-- Web.Config Configuration File --><configuration><system.web><customErro
rs mode="Off"/></system.web></configuration>
Notes: The current error page you are seeing can be replaced by a custom err
or page by modifying the "defaultRedirect" attribute of the application's <c
ustomErrors> configuration tag to point to a custom error page URL.
<!-- Web.Config Configuration File --><configuration><system.web><customErro
rs mode="RemoteOnly" defaultRedirect="mycustompage.htm"/></system.web></conf
iguration>All that error message says, is that you are not allowed to see the actual
error due to your Web.config settings. Change your settings to see the
actual error, as it could be happening anywhere, not necessarily in the
snippet you provided.
"chris" <anonymous@.discussions.microsoft.com> wrote in message
news:0715C212-605A-4162-904F-CBBD5021B896@.microsoft.com...
> Sub BindDropBox()
> conPos.Open()
>
> oSevenIssuesCMD = New SqlCommand("SevenIssues", conPos)
>
> oSevenIssuesCMD.CommandType = CommandType.StoredProcedure
>
> oSevenIssuesCMD.Parameters.Add(New SqlParameter("@.productName",
SqlDbType.VarChar, 50))
> oSevenIssuesCMD.Parameters("@.productName").Value = "TESTPRODUCT"
>
> oSevenIssuesReader = oSevenIssuesCMD.ExecuteReader
>
> DropDownList1().DataSource = oSevenIssuesReader
> DropDownList1.DataTextField = "Cover_Date"
> DropDownList1.DataValueField = "Cover_Date"
>
> DropDownList1.DataBind()
>
> conPos.Close()
>
> End Sub
>
> This is the error
>
> Server Error in '/TEST_application' Application.
> ----
--
> Runtime Error
> Description: An application error occurred on the server. The current
custom error settings for this application prevent the details of the
application error from being viewed remotely (for security reasons). It
could, however, be viewed by browsers running on the local server machine.
> Details: To enable the details of this specific error message to be
viewable on remote machines, please create a <customErrors> tag within a
"web.config" configuration file located in the root directory of the current
web application. This <customErrors> tag should then have its "mode"
attribute set to "Off".
>
> <!-- Web.Config Configuration
File --><configuration><system.web><customErrors
mode="Off"/></system.web></configuration>
>
> Notes: The current error page you are seeing can be replaced by a custom
error page by modifying the "defaultRedirect" attribute of the application's
<customErrors> configuration tag to point to a custom error page URL.
>
> <!-- Web.Config Configuration
File --><configuration><system.web><customErrors mode="RemoteOnly"
defaultRedirect="mycustompage.htm"/></system.web></configuration>
change it to what?
Did you read the error message? It said to change the mode attribute to
'Off'.
Alternatively, it said you could view the real error by running the page
from the server itself.
"chris" <anonymous@.discussions.microsoft.com> wrote in message
news:C9D4F429-9192-4F60-8812-8FFEDA62FBD1@.microsoft.com...
> change it to what?

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

Thread doesnt do anything

I have a Sub called SendMessages() that is supposed to do some database work and send an email message in my web app.

Now if I call it directly, it does what it should. I've now been trying to place this Sub into a thread of its own..

i.e

Dim sendMessageThread as New Thread(AddressOf SendMessages)
sendMessageThread.Start()

But the page ends up doing nothing...

Any ideas?Is your SendMessages sub within the same class or in another class?
Have you stepped through your code to see if the thread is entering the sub?
It is in the same class. I'm not using VS.NET so I can't do any stepping...

The thread is being called from Page_Load()
Post the code.
As far as it seems, the Sub ReceiveMessages is not being called at all!!!
Here is my code

Sub Page_Load()
Dim NewThread As Thread = New Thread(AddressOf ReceiveMessages)

NewThread.Priority = ThreadPriority.Lowest
NewThread.Start()
End Sub

Public Sub ReceiveMessages()
' Code here to connect to pop3 server and loop through messages
End Sub