Click here to Skip to main content
15,888,283 members
Home / Discussions / ASP.NET
   

ASP.NET

 
AnswerRe: Starting Carrier Pin
Richard MacCutchan5-Oct-11 9:39
mveRichard MacCutchan5-Oct-11 9:39 
AnswerRe: Starting Carrier Pin
JP_Rocks6-Oct-11 1:08
JP_Rocks6-Oct-11 1:08 
AnswerRe: Starting Carrier PinPopular
Dalek Dave6-Oct-11 22:28
professionalDalek Dave6-Oct-11 22:28 
QuestionASP.NET Routing Pin
Dominick Marciano4-Oct-11 14:46
professionalDominick Marciano4-Oct-11 14:46 
AnswerRe: ASP.NET Routing Pin
Parwej Ahamad4-Oct-11 17:54
professionalParwej Ahamad4-Oct-11 17:54 
GeneralRe: ASP.NET Routing Pin
Dominick Marciano5-Oct-11 11:08
professionalDominick Marciano5-Oct-11 11:08 
GeneralRe: ASP.NET Routing Pin
Parwej Ahamad5-Oct-11 19:13
professionalParwej Ahamad5-Oct-11 19:13 
GeneralRe: ASP.NET Routing Pin
Dominick Marciano6-Oct-11 14:26
professionalDominick Marciano6-Oct-11 14:26 
In my Global.asax:

VB
Imports System.Web.Routing

Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
        RegisterRoutes(RouteTable.Routes)
End Sub


Sub RegisterRoutes(ByVal routes As RouteCollection)
        routes.Add("View Product", New Route("Products/{*ProductName}", New ProductRouteHandler()))
End Sub


In a class file ProductRouteHandler:

VB
Imports System.Data.SqlClient
Imports System.Web.Routing

Public Class ProductRouteHandler
    Implements IRouteHandler

#Region "Variables"
    Dim connectionString As String = ConfigurationManager.ConnectionStrings("SQLMembership").ConnectionString
    Dim myConnection As New SqlConnection(connectionString)
#End Region

Public Function GetHttpHandler(ByVal requestContext As System.Web.Routing.RequestContext) As System.Web.IHttpHandler Implements System.Web.Routing.IRouteHandler.GetHttpHandler
        Dim productName As String = requestContext.RouteData.Values("ProductName").ToString

        If String.IsNullOrEmpty(productName) Then
            Return CType(System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath("~/Shopping/PageNotFound.aspx", GetType(Page)), Page)
        End If

        Dim selectSQL As String = "SELECT ProductID FROM Products WHERE ProductShortName='" & productName & "'"
        Dim selectCommand As SqlDataAdapter = New SqlDataAdapter(selectSQL, myConnection)
        Dim ds As New DataSet

        selectCommand.Fill(ds)

        If ds.Tables(0).Rows.Count > 0 Then
            HttpContext.Current.Items("ProductID") = ds.Tables(0).Rows(0).Item("ProductID").ToString
            Return CType(System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath("~/Shopping/ProductDetails.aspx", GetType(Page)), Page)
        Else
            Return CType(System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath("~/Shopping/PageNotFound.aspx", GetType(Page)), Page)
        End If
    End Function
End Class


Then the section of the web page that is having trouble is in the ProductDetails.aspx page. I have only included the full text of the form view control:

VB
<asp:FormView ID="fvProduct" runat="server" DataKeyNames="ProductID" DataSourceID="ProductDataSource">
        <ItemTemplate>
            <div id="ContentHead" class="ContentHead" runat="server">
                <%#Eval("ProductName")%></div>
            <br />
            <table style="border: 0px;">
                <tr>
                    <td style="vertical-align: top;">
                        <section>
                            <div id="section_articleOneIdentifier">
                            </div>
                            <article id="article_one">
                                <div id="gallery">
                                    <asp:PlaceHolder ID="ImageLightBox" runat="server" />
                                </div>
                            </article>
                        </section>
                        <%-- <img src='../Product_Images/<%#Eval("ProductImageName") %>' width="100" height="75"
                            alt='<%#Eval("ProductName") %> Image' />--%>
                    </td>
                    <td style="vertical-align: top;">
                        <%#Eval("ProductLongDescription")%>
                        <br />
                    </td>
                </tr>
            </table>
            <div style="padding-left: 250px; text-align: right">
                Your Price:  <%#Eval("ProductPrice", "{0:c}")%>
                <br />
            </div>
            <div style="padding-left: 400px; text-align: right;">
                <input type="number" id="itemCount" value="1" min="1" max="999" required="true" onblur="checkItemCount();"
                    onchange="revertColors();" />
                    <br />
                <a style="border: 0 none white" onmouseover="setCursorPointer();" onmouseout="setCursorNormal();">
                    <img id="imgAddToCart" src="Images/AddToCart.png" runat="server" alt="Add To Cart"
                        style="border-width: 0" onclick="process();" /></a>
                        <div style="vertical-align:top;text-align:right;padding-right:10px;">
                <asp:Label ID="lblStock" runat="server" Font-Size="10px" Font-Bold="true" Text="" />
                </div>
                <br />
                <br />
            </div>
        </ItemTemplate>
    </asp:FormView>


And in the code behind of ProductDetails.aspx that is causing the error is:

VB
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If String.IsNullOrEmpty(Request.QueryString("productID")) Then
            productID = CType(HttpContext.Current.Items("ProductID"), String)
        Else
            productID = Request.QueryString("productID")
        End If
        DetermineStock()
        CreateLightBox()
End Sub

Private Sub DetermineStock()
        Dim selectSQL As String = "SELECT TotalStock FROM Products WHERE ProductID='" & productID & "'"
        Dim selectCommand As SqlDataAdapter = New SqlDataAdapter(selectSQL, myConnection)
        Dim ds As New DataSet

        selectCommand.Fill(ds)

        Dim totalStock As Integer = CInt(ds.Tables(0).Rows(0).Item("TotalStock"))
        Dim StockLabel As Label = DirectCast(fvProduct.FindControl("lblStock"), Label)

        If totalStock > 0 Then
            StockLabel.Text = "In Stock"
            StockLabel.ForeColor = Drawing.Color.Green
        Else
            StockLabel.Text = "Backordered"
            StockLabel.ForeColor = Drawing.Color.Red
        End If
End Sub


In the user arrives at this page using the direct link www.mywebsite.com/Shopping/ProductDetails.aspx?id=ProductID then everything works fine. However if they come to this page from the routing class by visiting a page such as www.mywebsite.com/Products/ProductName then in the DetermineStock method, the line that finds the control lblStock returns nothing and therefore I cannot set the text or color of the label. However, if they follow the direct link that has the product id (and therefore the routing class was not used) the same line that finds the control lblStock in the form view returns the correct control and I can set the text and color appropriately.
GeneralRe: ASP.NET Routing Pin
Parwej Ahamad6-Oct-11 18:26
professionalParwej Ahamad6-Oct-11 18:26 
GeneralRe: ASP.NET Routing Pin
Dominick Marciano7-Oct-11 16:21
professionalDominick Marciano7-Oct-11 16:21 
QuestionAsp.net and uploading large files Pin
Ahmad Safwat4-Oct-11 14:30
Ahmad Safwat4-Oct-11 14:30 
AnswerRe: Asp.net and uploading large files Pin
uspatel4-Oct-11 21:31
professionaluspatel4-Oct-11 21:31 
GeneralRe: Asp.net and uploading large files Pin
Ahmad Safwat5-Oct-11 2:55
Ahmad Safwat5-Oct-11 2:55 
AnswerRe: Asp.net and uploading large files Pin
Ravindra Bisen5-Oct-11 6:41
Ravindra Bisen5-Oct-11 6:41 
GeneralRe: Asp.net and uploading large files Pin
Ahmad Safwat5-Oct-11 11:28
Ahmad Safwat5-Oct-11 11:28 
Questionload , change and save xml file as appropriate encoding Pin
oujeboland4-Oct-11 13:02
oujeboland4-Oct-11 13:02 
Question"Microsoft JScript runtime error: Object expected" using Telerik MVC controls Pin
jboyd1114-Oct-11 5:54
jboyd1114-Oct-11 5:54 
QuestionSet the NavigateUrl of a ImageMap in Repeater Pin
Farhad Eft3-Oct-11 23:53
Farhad Eft3-Oct-11 23:53 
AnswerRe: Set the NavigateUrl of a ImageMap in Repeater Pin
Parwej Ahamad4-Oct-11 12:01
professionalParwej Ahamad4-Oct-11 12:01 
QuestionTCP/IP Client Server Chat Application in ASP.Net Pin
nitin_ion3-Oct-11 23:11
nitin_ion3-Oct-11 23:11 
AnswerRe: TCP/IP Client Server Chat Application in ASP.Net Pin
Parwej Ahamad4-Oct-11 12:06
professionalParwej Ahamad4-Oct-11 12:06 
QuestionAlternate for MasterPage Pin
Ramkumar_S3-Oct-11 22:20
Ramkumar_S3-Oct-11 22:20 
Questionsending email on particular Date & Time Pin
prasanna2043-Oct-11 21:47
prasanna2043-Oct-11 21:47 
AnswerRe: sending email on particular Date & Time Pin
Anurag Gandhi4-Oct-11 1:50
professionalAnurag Gandhi4-Oct-11 1:50 
AnswerRe: sending email on particular Date & Time Pin
Morgs Morgan4-Oct-11 5:06
Morgs Morgan4-Oct-11 5:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.