asp.net - Error passing Session Variable to SQLDataSource -
i'm trying run project.aspx. admin.apsx, when 'view profile' button clicked, supposed create session "project_id" 2 sqldatasources project_id values session("project_id").
i'm getting 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" /> <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" /> <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] [projects] ([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] [projects] ([project_id] = @project_id)"> <selectparameters> <asp:sessionparameter name="project_id" sessionfield="project_id" type="int32" /> </selectparameters> </asp:sqldatasource> </div> <br /> </asp:content> admin.aspx
imports system.data.sqlclient partial class admin inherits system.web.ui.page protected sub btnsearchuser_click(sender object, e eventargs) handles btnsearchuser.click dim conn new sqlconnection("data source=brian-pc\sqlexpress;initial catalog=master_db;integrated security=true") dim searchcomm string = "select username users contains (username, @username)" dim searchsql new sqlcommand conn.open() searchsql = new sqlcommand(searchcomm, conn) searchsql.parameters.addwithvalue("@username", txtusersearch.text) dim datareader 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 object, e eventargs) handles me.load end sub protected sub dropdownlist1_selectedindexchanged(sender object, e eventargs) handles drpprojects.selectedindexchanged dim conn new sqlconnection("data source=brian-pc\sqlexpress;initial catalog=master_db;integrated security=true") dim sqlcomm string = "select project_name projects project_name=@projectname" dim sqlprojname new sqlcommand conn.open() sqlprojname = new sqlcommand(sqlcomm, conn) sqlprojname.parameters.addwithvalue("@projectname", drpprojects.selecteditem.text) dim datareader sqldatareader = sqlprojname.executereader() datareader.read() if datareader.hasrows 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 object, e eventargs) handles btnmanageproj.click multiview1.setactiveview(view2) end sub protected sub btnprojsettings_click(sender object, e eventargs) handles btnprojsettings.click dim conn2 new sqlconnection("data source=brian-pc\sqlexpress;initial catalog=master_db;integrated security=true") dim sqlcomm2 string = "select project_id projects project_name=@projectname" dim sqlprojid new sqlcommand conn2.open() sqlprojid = new sqlcommand(sqlcomm2, conn2) sqlprojid.parameters.addwithvalue("@projectname", drpprojects.selecteditem.text) dim datareader sqldatareader = sqlprojid.executereader() datareader.read() if datareader.hasrows dim userid 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 object, e eventargs) if session("userid") = nothing txtloginuser.visible = true txtloginpass.visible = true else dim conn new sqlconnection("data source=brian-pc\sqlexpress;initial catalog=master_db;integrated security=true") dim useridcomm string = "select name, surname users user_id=@userid" dim sqluserid new sqlcommand conn.open() dim userid string = session("userid") sqluserid = new sqlcommand(useridcomm, conn) sqluserid.parameters.addwithvalue("@userid", userid) dim datareader sqldatareader = sqluserid.executereader() if datareader.hasrows 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 object, e eventargs) dim loginsql new sqlcommand dim logincomm string dim commonfunctions new commonfunctions() dim dec_pass string = commonfunctions.encryptpassword(txtloginpass.text.trim) dim conn new sqlconnection("data source=brian-pc\sqlexpress;initial catalog=master_db;integrated security=true") logincomm = "select user_id users username=@username , password=@password" conn.open() loginsql = new sqlcommand(logincomm, conn) loginsql.parameters.addwithvalue("@username", txtloginuser.text.tostring) loginsql.parameters.addwithvalue("@password", dec_pass) dim dr sqldatareader = loginsql.executereader() dr.read() if dr.hasrows session("userid") = dr("user_id") elseif dr.hasrows = false 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> <asp:textbox id="txtloginuser" runat="server" borderstyle="none" borderwidth="0px" wrap="false"></asp:textbox> <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> <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> </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: 
what happening? wrong code?
i using vs 2012 professional sql-server 2012 express
code 'view profile' button in admin.aspx under btnprojsettings:
protected sub btnprojsettings_click(sender object, e eventargs) handles btnprojsettings.click dim conn2 new sqlconnection("data source=brian-pc\sqlexpress;initial catalog=master_db;integrated security=true") dim sqlcomm2 string = "select project_id projects project_name=@projectname" dim sqlprojid new sqlcommand conn2.open() sqlprojid = new sqlcommand(sqlcomm2, conn2) sqlprojid.parameters.addwithvalue("@projectname", drpprojects.selecteditem.text) dim datareader sqldatareader = sqlprojid.executereader() datareader.read() if datareader.hasrows dim userid string userid = datareader("project_id").tostring session("project_id") = userid server.transfer("project.aspx", true) end if datareader.close() conn2.close() end sub
i believe issue having related trying redirect page during async postback.
in btnprojsettings_click event handler, replace:
server.transfer("project.aspx", true) with:
dim jsredirect string = "window.location.pathname = 'project.aspx';" scriptmanager.registerclientscriptblock(page, page.gettype(), "redirect project.aspx", jsredirect, true) the reason why receiving error because during async postback, asp.net ajax libraries expecting customized message returned, rather standard html code. in turn, when call server.transfer, server will return html code of specified page; hence error seeing.
Comments
Post a Comment