Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error passing Session Variable to SQLDataSource

I'm trying to run Project.aspx. Admin.apsx, when the 'VIEW PROFILE' button is clicked, is supposed to create a session "project_id" so the two SQLDataSources get the project_id values from the Session("project_id").

I'm getting this error:

Error

Project.aspx

<%@ Page Title="" Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="Project.aspx.vb" Inherits="Project" %>


<%@ Register assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" namespace="System.Web.UI.DataVisualization.Charting" tagprefix="asp" %>



<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <div id="project-image"><asp:Image ID="Image1" runat="server" Height="124px" Width="93px" />
        <asp:FormView ID="frmViewProject" runat="server" DataKeyNames="project_id" DataSourceID="SqlDataSource1" HorizontalAlign="Justify">
            <EditItemTemplate>
                project_id:
                <asp:Label ID="project_idLabel1" runat="server" Text='<%# Eval("project_id") %>' />
                <br />
                project_type:
                <asp:TextBox ID="project_typeTextBox" runat="server" Text='<%# Bind("project_type") %>' />
                <br />
                project_start:
                <asp:TextBox ID="project_startTextBox" runat="server" Text='<%# Bind("project_start") %>' />
                <br />
                project_finish:
                <asp:TextBox ID="project_finishTextBox" runat="server" Text='<%# Bind("project_finish") %>' />
                <br />
                project_duration:
                <asp:TextBox ID="project_durationTextBox" runat="server" Text='<%# Bind("project_duration") %>' />
                <br />
                project_budget:
                <asp:TextBox ID="project_budgetTextBox" runat="server" Text='<%# Bind("project_budget") %>' />
                <br />
                project_cost:
                <asp:TextBox ID="project_costTextBox" runat="server" Text='<%# Bind("project_cost") %>' />
                <br />
                project_name:
                <asp:TextBox ID="project_nameTextBox" runat="server" Text='<%# Bind("project_name") %>' />
                <br />
                <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="Update" />
                &nbsp;<asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" />
            </EditItemTemplate>
            <EditRowStyle BorderStyle="Solid" Font-Bold="False" Font-Names="Verdana" Font-Overline="False" Font-Size="Small" HorizontalAlign="Right" VerticalAlign="Middle" />
            <InsertItemTemplate>
                project_type:
                <asp:TextBox ID="project_typeTextBox" runat="server" Text='<%# Bind("project_type") %>' />
                <br />
                project_start:
                <asp:TextBox ID="project_startTextBox" runat="server" Text='<%# Bind("project_start") %>' />
                <br />
                project_finish:
                <asp:TextBox ID="project_finishTextBox" runat="server" Text='<%# Bind("project_finish") %>' />
                <br />
                project_duration:
                <asp:TextBox ID="project_durationTextBox" runat="server" Text='<%# Bind("project_duration") %>' />
                <br />
                project_budget:
                <asp:TextBox ID="project_budgetTextBox" runat="server" Text='<%# Bind("project_budget") %>' />
                <br />
                project_cost:
                <asp:TextBox ID="project_costTextBox" runat="server" Text='<%# Bind("project_cost") %>' />
                <br />
                project_name:
                <asp:TextBox ID="project_nameTextBox" runat="server" Text='<%# Bind("project_name") %>' />
                <br />
                <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert" Text="Insert" />
                &nbsp;<asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" />
            </InsertItemTemplate>
            <InsertRowStyle HorizontalAlign="Center" VerticalAlign="Middle" />
            <ItemTemplate>
                project_id:
                <asp:Label ID="project_idLabel" runat="server" Text='<%# Eval("project_id") %>' />
                <br />
                project_type:
                <asp:Label ID="project_typeLabel" runat="server" Text='<%# Bind("project_type") %>' />
                <br />
                project_start:
                <asp:Label ID="project_startLabel" runat="server" Text='<%# Bind("project_start") %>' />
                <br />
                project_finish:
                <asp:Label ID="project_finishLabel" runat="server" Text='<%# Bind("project_finish") %>' />
                <br />
                project_duration:
                <asp:Label ID="project_durationLabel" runat="server" Text='<%# Bind("project_duration") %>' />
                <br />
                project_budget:
                <asp:Label ID="project_budgetLabel" runat="server" Text='<%# Bind("project_budget") %>' />
                <br />
                project_cost:
                <asp:Label ID="project_costLabel" runat="server" Text='<%# Bind("project_cost") %>' />
                <br />
                project_name:
                <asp:Label ID="project_nameLabel" runat="server" Text='<%# Bind("project_name") %>' />
                <br />

            </ItemTemplate>
            <RowStyle BorderStyle="None" HorizontalAlign="Left" VerticalAlign="Middle" />
        </asp:FormView>
        <asp:Chart ID="chrtComplete" runat="server" DataMember="DefaultView" DataSourceID="SqlDataSource2" EnableViewState="True">
            <series>
                <asp:Series ChartType="Pie" Legend="Completed" Name="Series1" XValueMember="project_start" YValueMembers="project_finish">
                </asp:Series>
            </series>
            <chartareas>
                <asp:ChartArea Name="ChartArea1">
                </asp:ChartArea>
            </chartareas>
            <Legends>
                <asp:Legend LegendStyle="Row" Name="Completed">
                </asp:Legend>
            </Legends>
            <Titles>
                <asp:Title Name="Project Completion">
                </asp:Title>
            </Titles>
        </asp:Chart>
        <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnStringDb1 %>" SelectCommand="SELECT [project_start], [project_finish] FROM [projects] WHERE ([project_id] = @project_id)">
            <SelectParameters>
                <asp:SessionParameter Name="project_id" SessionField="project_id" Type="Int32" />
            </SelectParameters>
        </asp:SqlDataSource>
    </div>
    <div id="project-info">
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnStringDb1 %>" SelectCommand="SELECT [project_id], [project_type], [project_start], [project_finish], [project_duration], [project_budget], [project_cost], [project_name] FROM [projects] WHERE ([project_id] = @project_id)">
            <SelectParameters>
                <asp:SessionParameter Name="project_id" SessionField="project_id" Type="Int32" />
            </SelectParameters>
        </asp:SqlDataSource>
    </div>
&nbsp;
    <br />
</asp:Content>

Admin.aspx

Imports System.Data.SqlClient

Partial Class Admin
    Inherits System.Web.UI.Page

    Protected Sub btnSearchUser_Click(sender As Object, e As EventArgs) Handles btnSearchUser.Click

        Dim conn As New SqlConnection("Data Source=BRIAN-PC\SQLEXPRESS;Initial Catalog=master_db;Integrated Security=True")
        Dim searchComm As String = "SELECT username FROM users WHERE CONTAINS (username, @username)"

        Dim searchSQL As New SqlCommand

        conn.Open()

        searchSQL = New SqlCommand(searchComm, conn)
        searchSQL.Parameters.AddWithValue("@username", txtUserSearch.Text)

        Dim datareader As SqlDataReader = searchSQL.ExecuteReader()

        While datareader.Read

            lstUsers.Items.Add(datareader.Item("username"))

        End While



        datareader.Close()
        conn.Close()

    End Sub

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

    End Sub

    Protected Sub DropDownList1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles drpProjects.SelectedIndexChanged



        Dim conn As New SqlConnection("Data Source=BRIAN-PC\SQLEXPRESS;Initial Catalog=master_db;Integrated Security=True")
        Dim sqlComm As String = "SELECT project_name FROM projects WHERE project_name=@projectname"
        Dim sqlProjname As New SqlCommand

        conn.Open()

        sqlProjname = New SqlCommand(sqlComm, conn)
        sqlProjname.Parameters.AddWithValue("@projectname", drpProjects.SelectedItem.Text)
        Dim datareader As SqlDataReader = sqlProjname.ExecuteReader()
        datareader.Read()


        If datareader.HasRows Then

            lblProjName.Text = datareader("project_name").ToString()
        End If

        datareader.Close()
        conn.Close()

        lblProjName.Visible = True
        grdProjDetails.Visible = True







    End Sub

    Protected Sub btnManageProj_Click(sender As Object, e As EventArgs) Handles btnManageProj.Click

        MultiView1.SetActiveView(View2)


    End Sub

    Protected Sub btnProjSettings_Click(sender As Object, e As EventArgs) Handles btnProjSettings.Click

        Dim conn2 As New SqlConnection("Data Source=BRIAN-PC\SQLEXPRESS;Initial Catalog=master_db;Integrated Security=True")
        Dim sqlComm2 As String = "SELECT project_id FROM projects WHERE project_name=@projectname"
        Dim sqlProjID As New SqlCommand

        conn2.Open()

        sqlProjID = New SqlCommand(sqlComm2, conn2)
        sqlProjID.Parameters.AddWithValue("@projectname", drpProjects.SelectedItem.Text)
        Dim datareader As SqlDataReader = sqlProjID.ExecuteReader()
        datareader.Read()


        If datareader.HasRows Then

            Dim UserID As String
            UserID = datareader("project_id").ToString

            Session("project_id") = UserID
            Server.Transfer("Project.aspx", True)

        End If

        datareader.Close()
        conn2.Close()



    End Sub
End Class

MasterPage.aspx

<%@ Master Language="VB" CodeFile="MasterPage.master.vb" Inherits="MasterPage" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<script runat="server">



    Protected Sub Page_Load(sender As Object, e As EventArgs)

        If Session("userid") = Nothing Then
            txtLoginUser.Visible = True
            txtLoginPass.Visible = True

        Else


            Dim conn As New SqlConnection("Data Source=BRIAN-PC\SQLEXPRESS;Initial Catalog=master_db;Integrated Security=True")
            Dim useridComm As String = "SELECT name, surname FROM users WHERE user_id=@userid"
            Dim sqlUserID As New SqlCommand

            conn.Open()

            Dim userid As String = Session("userid")

            sqlUserID = New SqlCommand(useridComm, conn)
            sqlUserID.Parameters.AddWithValue("@userid", userid)
            Dim datareader As SqlDataReader = sqlUserID.ExecuteReader()
            If datareader.HasRows Then

                FormsAuthentication.RedirectFromLoginPage(datareader("user_id"), True)
                lblLoggedIn.Text = datareader("name").ToString() & " " & datareader("surname").ToString()

            End If
            datareader.Close()
            conn.Close()

        End If
    End Sub

   Protected Sub Button1_Click(sender As Object, e As EventArgs)

        Dim loginSQL As New SqlCommand
        Dim loginComm As String

        Dim CommonFunctions As New CommonFunctions()
        Dim dec_pass As String = CommonFunctions.EncryptPassword(txtLoginPass.Text.Trim)

        Dim conn As New SqlConnection("Data Source=BRIAN-PC\SQLEXPRESS;Initial Catalog=master_db;Integrated Security=True")


        loginComm = "SELECT user_id FROM users WHERE username=@username and password=@password"

        conn.Open()


        loginSQL = New SqlCommand(loginComm, conn)
        loginSQL.Parameters.AddWithValue("@username", txtLoginUser.Text.ToString)
        loginSQL.Parameters.AddWithValue("@password", dec_pass)
        Dim dr As SqlDataReader = loginSQL.ExecuteReader()
        dr.Read()


        If dr.HasRows Then
            Session("userid") = dr("user_id")
        ElseIf dr.HasRows = False Then

            lblRegister.ForeColor = Drawing.Color.Red
            lblRegister.Text = "Incorrect Username/Password."
        End If
        dr.Close()
        conn.Close()

    End Sub





</script>



<head>
    <meta charset="utf-8" />
    <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <title></title>
    <meta name="keywords" content="" />
    <meta name="description" content="" />
    <link rel="stylesheet" href="style.css" type="text/css" media="screen, projection" />
</head>

<body>

    <form id="form1" runat="server">

<div id="wrapper">

    <header id="header">
        <strong>Header:</strong> Mobile CMS

        </header>


<section id="login">

    <div id="login-form">

         <p>

                    <asp:Label ID="lblUsername" runat="server" Font-Bold="True" Text="U:"></asp:Label>
&nbsp;<asp:TextBox ID="txtLoginUser" runat="server" BorderStyle="None" BorderWidth="0px" Wrap="False"></asp:TextBox>
&nbsp;
                    <asp:Label ID="lblUsername0" runat="server" Font-Bold="True" Text="P:"></asp:Label>
                    <asp:TextBox ID="txtLoginPass" runat="server" BorderStyle="None" BorderWidth="0px" TextMode="Password" Wrap="False"></asp:TextBox>
&nbsp;
                    <asp:Button ID="btnLogin" runat="server" BorderStyle="None" OnClick="Button1_Click" Text="Login" />

                </p>

                <p>

                    <asp:Label ID="lblRegister" runat="server" Font-Bold="True" Font-Underline="True" ForeColor="#0000CC" Text="Register"></asp:Label>

                    <asp:Label ID="lblLoggedIn" runat="server"></asp:Label>

                </p>
         <p>

                    &nbsp;</p>



    </div>

</section>

<div class="navigation-bar">
       <ul class="navigation-menu">

            <li><a href="#" class="home">Home</a></li>
            <li><a href="#" class="mainsettings">Settings</a></li>
            <li><a href="#" class="profile">Profile</a>

                <ul>
                    <li><a href="#" class="messages">Messages</a></li>
                    <li><a href="#" class="settings">Profile Settings</a></li>
                </ul>

            </li>
            <li><a href="#" class="uploads">Uploads</a></li>
            <li><a href="#" class="documents">Media</a>


                <ul>
                    <li><a href="#" class="docs">Documents</a></li>
                    <li><a href="#" class="others">Others</a></li>
                </ul>

            </li>

            <li><a href="#" class="projects">Projects</a>


                <ul>
                    <li><a href="#" class="yprojects">Your Projects</a></li>
                    <li><a href="#" class="otherprojects">Other Projects</a></li>
                </ul>

            </li>

        </ul>

    </div>


    <section id="middle">

        <div id="container">
            <div id="content">
                <div>
                    <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">

                    </asp:ContentPlaceHolder>
                </div>
        </div>
        </div>



    </section>

    <footer id="footer">
        <strong>Footer:</strong> adsfdsgfds
    </footer>

</div>

    </form>

</body>
</html>

Screenshot: Manage Project

What is happening? What is wrong with the code?

I am using VS 2012 Professional with SQL-Server 2012 Express

Code for the 'View Profile' button is in Admin.aspx under btnProjSettings:

  Protected Sub btnProjSettings_Click(sender As Object, e As EventArgs) Handles btnProjSettings.Click

        Dim conn2 As New SqlConnection("Data Source=BRIAN-PC\SQLEXPRESS;Initial Catalog=master_db;Integrated Security=True")
        Dim sqlComm2 As String = "SELECT project_id FROM projects WHERE project_name=@projectname"
        Dim sqlProjID As New SqlCommand

        conn2.Open()

        sqlProjID = New SqlCommand(sqlComm2, conn2)
        sqlProjID.Parameters.AddWithValue("@projectname", drpProjects.SelectedItem.Text)
        Dim datareader As SqlDataReader = sqlProjID.ExecuteReader()
        datareader.Read()


        If datareader.HasRows Then

            Dim UserID As String
            UserID = datareader("project_id").ToString

            Session("project_id") = UserID
            Server.Transfer("Project.aspx", True)

        End If

        datareader.Close()
        conn2.Close()



    End Sub
like image 766
Brian Avatar asked Nov 03 '22 00:11

Brian


1 Answers

I believe the issue you are having is related to trying to redirect to another page during an Async PostBack.

In your btnProjSettings_Click event handler, replace:

Server.Transfer("Project.aspx", True)

with:

Dim jsRedirect As String = "window.location.pathname = 'Project.aspx';"
ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "Redirect to Project.aspx", jsRedirect, true)

The reason why you are receiving that error is because during an Async PostBack, the ASP.NET Ajax libraries are expecting a customized message to be returned, rather than standard HTML code. In turn, when you call Server.Transfer, the server will return the HTML code of the specified page; hence the error you are seeing.

like image 53
Jesse Avatar answered Nov 12 '22 02:11

Jesse