How to create a session to display a users Login name - asp.net

I am a bit of a ASP.NET/VB noobie so sorry in advance for any stupid questions.
I am currently creating a ASP.NET application using VB. The application I have had is connected to a local Database and I am able redirect a user to a "Users" page once they have input their login credentials correctly.
What I want to happen is that once they login and are redirected to the "Users" page, there will be a message saying "Welcome" followed by their login name.
I have been able to successfully do this in C# but need to be able to do so in VB as well.
The code that worked in C# is as follows;
**Session["New"] = TextBoxUserName.Text;
Response.Write("Password is correct");
Response.Redirect("Users.aspx");**
I've tried putting this code straight into my VB project but have received the following error;
"Property access must assign to the property or use its value"
I've done some research into this error and can't seem to find any solution to the problem.
Is my syntax incorrect or are sessions used completely different in VB?

I haven't seen your VB Code but it should be:
Session("New") = TextBoxUserName.Text
''Response.Write("Password is correct"); - No point in having this line as the redirect overrides it quickly.
Response.Redirect("Users.aspx")
Note the use of round brackets rather than square brackets for VB, also note the lackof semi colons. VB.NET and C# are completely different syntax wise.

Related

How to change the language of ErrorText based on culture in aspx.cs

I'm new to the world of asp and was given the task to make sure a site can be translated with the click of a button.
So far i was able to make most of the page translate using recourse files but small things like the error messages you get when trying to login is what i'm stuck on.
The error messages for ASPX(Password is required and Username is required) is inside so i cant "call" my resource file.
The "Username or password is incorrect" message is inside my ASPX.CS is inside a else statement and i cant figure out how to "call" the resource file.
Any help will truly be appreciated.
Thanks in advance
The answer is quite simple. All you need to do is remember to convert to string
https://forums.asp.net/t/1930499.aspx?Displaying+messages+in+the+message+box+from+the+resource+files+in+c+

Loosing session variable data from one page to the other in VB.NET

I am a bit new to VB.NET. I have a page that sets 2 session variables and does a redirect to second page. The second pages is at least using one of the session variables. I can tell because on the second page, if the session variable is not correct the user is redirected to an access denied page. The second page also uses the session variable in question. It will read it an fill a gridview based on the value of the variable. I set the variable like so
Session("ID") = Convert.ToInt32(a_value)
and on the second page I retrieve the variable like this
a_page_variable = Session("ID")
What I find strange is that when I run this code in visual studio it works as expected but when I deploy and run it, I get 0 from my session variable instead of the true value of "a_value". I have tried a few things like making sure the data types match up from page to page and trying different ways to retrieve the variable such as
Session("userID")
and
CType(Session.Item("userID"), Int32)
I've also tried to see what is coming in to the second page by using
Response.Write
I also tried to use SQL Profiler to see what kind of call is being made to fill the gridview but I haven't had any luck. The gridview gives me an empty dataset and the Profiler does not detect a call being made from the application. I thought working with session variables was pretty straight forward but obviously, I am missing something.
Thanks for your help,
Billy
One possibility (and the only one that could be guessed at with how little information we have) could be the response.redirect causing the application to terminate due to an exception.
When redirecting, you want to always pass a false, and then call complete request.
Response.Redirect(urlstring, False)
Response.CompleteRequest()
not following these steps can cause exceptions, which may drop session.
Additionally, resolve virtual paths, as some browsers (mobile especially) can see those redirects as new requests entirely, thus generating new session tokens.
Dim urlstring As String
urlstring = Page.ResolveUrl("~/default.aspx")
that said, there are a number of possible causes for this situation.
Application Pool restarts
App Domain restarted
Code changing value unexpectedly
AV tinkering with files
deployed to web farm
With the description provided above, we just don't have enough information to really troubleshoot.
Thank you ADyson, Stephen Wrighton and everyone else who took a stab at helping me figure this out. I was able to find out what was going on by adding code that wrote to a log file on the server. Found the logging code here. I found that I never reached the code that set the session variable and that is the reason it never populated on the second page. I was trying to get the logon name for the user by using Environment.UserName which will return the user name of the person who is currently logged on to the operating system. But what I really wanted to do was get the logon name of the user that was visiting my site. For this I used User.Identity.Name. This works great when you need to know which user from an Active Directory domain is visiting your site.

Send query string parameter from a non-web application

Ok, I've been bugging with this for too long.
I need to call my website from a rough VB.net app. Then only thing I need is to attach a query string parameter to the calling url, so I can distinguish the pages to be shown to different VB app users.
So I want to click a button and launch that website, giving this parameter.
First I was bugging with adding the system.web libraries. Now I can't use Request/Response.QueryString as well.
I tried getting some example help from this post. but as I said before - I cannot make use of Request.QueryString as I cannot import it.
I am stuck here:
Process.Start("http://localhost:56093/WebSite1?id=")
I need to attach a query string parameter to the url and then open the website with that url.
Can someone just give me a sample code for my problem.
Query parameters are parsed by the web server/http handler off the URL you use to call the page. They consist of key and value pairs that come at the end of the URL. Your code is nearly there. Say you needed to pass through the parameters:
ID = 1234
Page = 2
Display = Portrait
Then you'd turn them into a URL like this:
http://localhost:56093/WebSite1?ID=1234&Page=2&Display=Portrait
Therefore in your code you'd have:
Process.Start("http://localhost:56093/WebSite1?ID=1234&Page=2&Display=Portrait");

Passing Arguments to an ASP .NET Page

So I've recently started working with ASP .NET but I am very familiar with other .NET frameworks. The application I'm working on needs to take in an string index as a command line-style argument. Right now in my Page class I have a const string that I'm using as a placeholder. So the question I have is a two parter:
1- How do I set up my applications innerts to recieve an argument that is passed in when the page is opened?
2- How do I pass that argument in to the page (especially while I'm working in VS 2010)?
Much Thanks!
You pass arguments to web pages with request variables (ever hear of POST or GET?). The simplest way is to append the variables to the end of the url like this:
http://localhost/Default.aspx?varname=varvalue&othervarname=othervarvalue
In your codebehind, you can access the request variables with Request.QueryString.
For #2: In your project config on VS2010, to to the 'Web' tab and set the option to specific page with the desired URL: http://localhost/Default.aspx?varname=varvalue&othervarname=othervarvalue

ASP.NET - Strange case of inexplicable 404 error

We are having a very strange problem on one particular web server (we do not have direct access to the web server, only FTP access).
Our ASP.NET application displays a dataset into a standard GridView. One of the columns in the GridView is a basic template column, with a link redirecting to another page - passing few parameters.
One of the parameters is EmployeeName - and the following page uses that parameter to set a label.
ON this particular web server (WEBSERVER1 in this example)... the resulting link generates an error 404 (page not found)
https://WWW.WEBSERVER1.COM/Customer_011B.aspx?WeekEnding=1/21/2012&GUID=n.a.&EmployeeName=Knutson-Haushalter, Kathleen&ReportToName=Mary Jo Eayrs&Assignment_Id=123772
On another web server (WEBSERVER2 in this example)... the resulting link properly opens the page.
http://WWW.WEBSERVER2.COM/Customer_011B.aspx?WeekEnding=1/21/2012&GUID=n.a.&EmployeeName=Knutson-Haushalter, Kathleen&ReportToName=Mary Jo Eayrs&Assignment_Id=123772
(unfortunately the links above are not rendered correctly
Yes, I am aware that WEBSERVER1 is running under SSL - but am not sure why this would make a difference.
Now, we have verified that the page Customer_011B.aspx is indeed present on WEBSERVER1.
Here comes the puzzle:
If we only remove the EmployeeName parameter, the page displays correctly. All database operations are performed correctly, etc. The only "problem" is that the EmployeeName is not reported in the target label.
In other words:
This DOES NOT work and all we get is error 404
https://WWW.WEBSERVER1.COM/Customer_011B.aspx?WeekEnding=1/21/2012&GUID=n.a.&EmployeeName=Knutson-Haushalter, Kathleen&ReportToName=Mary Jo Eayrs&Assignment_Id=123772
This DOES work and we get to the page and we retrieve all the needed data.
https://WWW.WEBSERVER1.COM/Customer_011B.aspx?WeekEnding=1/21/2012&GUID=n.a.&ReportToName=Mary Jo Eayrs&Assignment_Id=123772
Just in case you are wondering, the only parameter needed by our data access layer is that Assignment_Id number.
Also, note that I enclosed the links in double quotes... so that they would render properly...
Use the UrlEncode and UrlDecode to place the parametres on your url. I see that you use spaces and slash and commas. Parametres with slash/space/comma and other invalid url characters maybe cut or change by enabled url filter on one of the iis server.

Resources