Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 5th, 2007, 10:08 AM
Authorized User
 
Join Date: Sep 2007
Location: , , .
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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

Reply With Quote
  #2 (permalink)  
Old September 5th, 2007, 10:23 AM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

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
================================================== =========
Reply With Quote
  #3 (permalink)  
Old September 5th, 2007, 10:34 AM
Authorized User
 
Join Date: Sep 2007
Location: , , .
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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


Reply With Quote
  #4 (permalink)  
Old September 5th, 2007, 10:47 AM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Hmm. If i understand you correctly you have a Page_Load() Event on your aspx page and also a Page_Load() event on your .VB page. If this is the case, remove the Page_Load() event from the .aspx page.

================================================== =========
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

================================================== =========
Reply With Quote
  #5 (permalink)  
Old September 5th, 2007, 10:55 AM
Authorized User
 
Join Date: Sep 2007
Location: , , .
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

Reply With Quote
  #6 (permalink)  
Old September 5th, 2007, 10:58 AM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

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
================================================== =========
Reply With Quote
  #7 (permalink)  
Old September 5th, 2007, 11:35 AM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

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

================================================== =========
Reply With Quote
  #8 (permalink)  
Old September 5th, 2007, 01:22 PM
Authorized User
 
Join Date: Sep 2007
Location: , , .
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

...
Reply With Quote
  #9 (permalink)  
Old September 5th, 2007, 01:31 PM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

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
================================================== =========
Reply With Quote
  #10 (permalink)  
Old September 5th, 2007, 01:39 PM
Authorized User
 
Join Date: Sep 2007
Location: , , .
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
simple report and crystal report in vb.net saket123 .NET Framework 2.0 0 August 13th, 2008 06:55 AM
Indian Rs Format simple Function for vb.net s.karthik General .NET 0 March 24th, 2008 06:27 AM
MSSQL 2005 and MSSQL 2000 tiredcat Visual Basic 2005 Basics 0 April 9th, 2007 12:56 AM
Simple ASP.NET message box with VB janicen General .NET 1 February 24th, 2005 03:31 AM
VB MSSQL database REmote connectivity srichary Pro VB Databases 0 November 28th, 2004 06:33 AM



All times are GMT -4. The time now is 04:48 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.