stylesum2009 0 Newbie Poster

this is products.vb code file

Imports Microsoft.VisualBasic
Imports System.Data.SqlClient



' Use the unique namespace for this application
Namespace Proj06Web
    '
    ' Name the Class
    Public Class Products

        Function GetAllCategories() As SqlDataReader
            ' Get all products as return a dataset to the caller
            ' uses sp_Get_Products stored proc to return all
            ' of the products
            '
            ' Create the connection
            Dim cn As SqlConnection
            cn = New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("Data Source=METALLICA-X\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True"))
            '
            ' Create a SQLCommand object with the stored procedure
            ' that we will be executing

            Dim cmd As SqlCommand = New SqlCommand("sp_Get_Categories", cn)

            cmd.CommandType = Data.CommandType.StoredProcedure
            '
            ' Open the connection 
            cn.Open()

            '
            ' Call the executeReader method to return the DataReader
            ' back to the caller
            Return cmd.ExecuteReader(Data.CommandBehavior.CloseConnection)
        End Function
        Function GetProductsByCategory(ByVal categoryID As Integer) _
        As SqlDataReader
            ' Return all of the products for a particular category
            '
            ' Create the connection
            Dim cn As SqlConnection
            cn = New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("Data Source=METALLICA-X\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True"))
            '
            ' Create a SQLCommand object with the stored procedure
            ' that we will be executing
            Dim cmd As SqlCommand = _
            New SqlCommand("sp_Get_Products_By_Category", cn)
            cmd.CommandType = Data.CommandType.StoredProcedure
            '
            ' Create a new Parameter and pass the CategoryID
            Dim paramCategoryID As SqlParameter = _
            New SqlParameter("@CategoryID", Data.SqlDbType.Int, 4)
            paramCategoryID.Value = categoryID
            cmd.Parameters.Add(paramCategoryID)
            '
            ' Open the connection(here error occurs)
            cn.Open()        
            '
            ' Call the executeReader method to return the DataReader
            ' back to the caller
            Return cmd.ExecuteReader(Data.CommandBehavior.CloseConnection)
        End Function
    End Class
End Namespace

n i have created a Categoriesmenu user control..

<%@ Control Language="VB" AutoEventWireup="false" CodeFile="_CategoriesMenu.ascx.vb"
    Inherits="_CategoriesMenu" %>
<%@ OutputCache Duration="7200" VaryByParam="CategoryID;Selection" %>
<table>
    <tr>
        <td style="width: 624px">
            <asp:DataList ID="DataList1" runat="server" RepeatDirection="Horizontal" SelectedItemStyle-BackColor="#006666"
                Height="16px" BackColor="LemonChiffon" SelectedIndex="0" CellPadding="8" Width="507px">
                <SelectedItemStyle BackColor="#006666"></SelectedItemStyle>
                <SelectedItemTemplate>
                    <asp:HyperLink ID="Hyperlink4" runat="server" NavigateUrl='<%#"productlist.aspx?CategoryID=" &amp; DataBinder.Eval(Container.DataItem, "CategoryID") &amp; "&amp;selection=" &amp; Container.ItemIndex %>'
                        Text='<% DataBinder.Eval(Container.DataItem, "CategoryName") %>' CssClass="selected">
                    </asp:HyperLink>
                </SelectedItemTemplate>
                <ItemTemplate>
                    <asp:HyperLink ID="Hyperlink3" runat="server" NavigateUrl='<%# "productlist.aspx?CategoryID=" &amp; DataBinder.Eval(Container.DataItem, "CategoryID") &amp; "&amp;selection=" &amp; Container.ItemIndex %>'
                        Text='<%# DataBinder.Eval(Container.DataItem, "CategoryName") %>' CssClass="notselected">
                    </asp:HyperLink>
                </ItemTemplate>
            </asp:DataList>
        </td>
    </tr>
    <tr>
        <td>
            <asp:Label ID="Label1" runat="server">Search</asp:Label>
            <asp:TextBox runat="server" ID="SearchText"></asp:TextBox>
        </td>
    </tr>
</table>

m unable to open the connection due to invalid operation exception..i dun wats goin wrong..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.