 |
| ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

September 5th, 2007, 10:08 AM
|
|
Authorized User
|
|
Join Date: Sep 2007
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
simple MSSQL queries and VB.NET
Hello-
First time poster, with a simple question that I just can't seem to find any answers to online...
I am trying to run regular mssql (sql server) queries with a page_load event in a regular aspx. vb codebehind file. I can run the query fine on a Page_Load() function if I put it directly in the aspx file, but this is not what I need to have happen, as there are other things being accomplished by the Page_Load() function in the aspx. vb file and I don't want to scrap those.
Here's the function I have that works in the aspx file...
Code:
<script runat="server">
Page_Load()
Dim dbconn,sql,dbcomm,dbread
dbconn = New SqlConnection( "server=serveraddy.net;uid=user_id;pwd=password;database=test_storefrontdb")
dbconn.Open()
dbcomm = New SqlCommand ( "SELECT * FROM Products INNER JOIN ProductCategory ON ProductCategory.ProductID = Products.uid WHERE (ProductCategory.CategoryID=1424) AND (Products.IsActive=1)", dbconn )
dbread=dbcomm.ExecuteReader()
featured.DataSource=dbread
featured.DataBind()
dbread.Close()
dbconn.Close()
End Sub
</script>
Is there any way that can write this same thing in the Page_Load event in the VB codebehind file? I've tried to Import System.Data.SqlClient and do it that way, but it doesn't seem to recognize this.
Thanks in advance,
blu
|
|

September 5th, 2007, 10:23 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
|
|
hi there.. this should be done fairly easy on the page_load event... you said this code doesn't work??
besides that it miss all the declarations, this should work.. also this could be done easily with a dataadapter/datatable...
HTH
Gonzalo
================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
I know that CVS was evil, and now i got the proof:
http://worsethanfailure.com/Articles...-Hate-You.aspx
================================================== =========
|
|

September 5th, 2007, 10:34 AM
|
|
Authorized User
|
|
Join Date: Sep 2007
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi gbianchi-
This function works just fine, if it's put directly into the default.aspx file. But I want to put it in the same page_load event that exists in the default.aspx. vb file. At the top of default.aspx is this:
Code:
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="default.aspx.vb" TargetSchema="http://schemas.microsoft.com/intellisense/nav4-0" Debug="true"%>
In the default.aspx. vb file exists a Page_Load function that seems to conflict with the one in the default.aspx file. I either need a way to consolidate both functions into one, or another method for running an ASP function as soon as the page is loaded.
The DataList is called further down in the default.aspx file as such:
Code:
<asp:DataList RepeatColumns="3" RepeatDirection="Horizontal" id="featured" runat="server">
<ItemTemplate>
<%#Container.DataItem("Name")%>
</ItemTemplate>
</asp:DataList>
Thanks,
blu
|
|

September 5th, 2007, 10:55 AM
|
|
Authorized User
|
|
Join Date: Sep 2007
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
dparsons-
The two Page_Load() function are doing two different things. And I need both of them. Is there any other way to run an ASP function as a page is loaded in the aspx file?
Thanks,
blu
|
|

September 5th, 2007, 10:58 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
|
|
wouldn't be better (for design purposes) to have all in the code behind file??
can you send what you have in the code behind page_load event?? probably you couldn't add this because you have coding errors
like not defining the type of objects in the dim...
HTH
Gonzalo
================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
I know that CVS was evil, and now i got the proof:
http://worsethanfailure.com/Articles...-Hate-You.aspx
================================================== =========
|
|

September 5th, 2007, 11:35 AM
|
|
Wrox Author
|
|
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
|
|
I think you are trying to make .NET code fit the Classic ASP Model. First, no, you can not have to page load events regardless of where they appear. Second, yes it is quite possible to execute code on the .aspx page in a similar manner to classic asp BUT if you want to access variables in your code behind file from your .aspx page, they must be declared with a protection level of at least Protected.
Also, declare your variables. In the above code example you are declaring everything as an object as opposed to a type. For example:
Dim dbconn as New SqlConnection("server=serveraddy.net;uid=user_id;p wd=password;database=test_storefrontdb")
dbconn is now of type System.Data.SqlClient.SqlConnection as opposed to System.Object
================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
Technical Editor for:
Professional Search Engine Optimization with ASP.NET
Professional IIS 7 and ASP.NET Integrated Programming
================================================== =========
|
|

September 5th, 2007, 01:22 PM
|
|
Authorized User
|
|
Join Date: Sep 2007
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Okay, I'm trying to rethink this and work it all into the ASP.NET model of keeping a separate master and content file... Below is what I've got so far. It compiles perfectly (no errors), but when I try to run it, an error is thrown.
default.aspx
------------
Code:
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="default.aspx.vb" Inherits="StoreFront.StoreFront.CustSignIn" TargetSchema="http://schemas.microsoft.com/intellisense/nav4-0" Debug="true"%>
....
<asp:DataList RepeatColumns="3" RepeatDirection="Horizontal" id="featured" runat="server">
<ItemTemplate>
<%#Container.DataItem("Name")%>
</ItemTemplate>
</asp:DataList>
default.aspx. vb:
----------------
Code:
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Public Class CustSignIn
Inherits CWebPage
Protected WithEvents FeaturedItems As System.Web.UI.WebControls.DataList
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim dbconn As New SqlConnection( "server=serveraddy.net;uid=user_id;pwd=password;database=test_storefrontdb" )
dbconn.Open()
Dim dbcomm As New SqlCommand( "SELECT * FROM Products INNER JOIN ProductCategory ON ProductCategory.ProductID = Products.uid WHERE (ProductCategory.CategoryID=1424) AND (Products.IsActive=1)", dbconn )
Dim dbread As SqlDataReader = dbcomm.ExecuteReader()
FeaturedItems.DataSource = dbread
FeaturedItems.DataBind()
dbread.Close()
dbconn.Close()
...
Try
SetPageTitle = m_objMessages.GetXMLMessage("default.aspx", "PageTitle", "Title")
SetDesign(PageTable, PageSubTable, PageCell, ErrorAlignment, MessageAlignment)
Catch ex As Exception
Session("DetailError") = "Class CustSignIn Error=" & ex.Message
Response.Redirect(StoreFrontConfiguration.SiteURL & "errors.aspx")
End Try
If (Request.QueryString("SignOut") <> "") Then
m_objCustomer.UpdateSignIn(False)
Session("Customer") = Nothing
Session("XMLShoppingCart") = Nothing
m_objXMLCart = Nothing
m_objCustomer = Nothing
Session("OrderHistory") = Nothing
Response.Redirect("default.aspx")
End If
End Sub
...
|
|

September 5th, 2007, 01:31 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
|
|
what error are you receiving now.???
HTH
Gonzalo
================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
I know that CVS was evil, and now i got the proof:
http://worsethanfailure.com/Articles...-Hate-You.aspx
================================================== =========
|
|

September 5th, 2007, 01:39 PM
|
|
Authorized User
|
|
Join Date: Sep 2007
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
It is just a typical nondescript error: "An error has occurred."
I didn't think it was worth mentioning. Debugging is really impossible based on the kinds of errors I am getting.
|
|
 |