ASP.net how to use sessions? - asp.net

I need to assign my variable to session. I tried this:
string name = string.Empty
Session["N"] = name;
and it won't work.
Error 1 Invalid token '[' in class, struct, or interface member declaration
Error 2 Invalid token '"N"' in class, struct, or interface member declaration
Error 3 Identifier expected
Where I'm wrong?
I'm using ASP.net in Visual Studio 2008.
Error 1 Invalid token '[' in class, struct, or interface member declaration
Error 2 Invalid token '"N"' in class, struct, or interface member declaration
Error 3 Identifier expected

missing ; end of first line.
string name = string.Empty;
Session["N"] = name;
string test= Session["N"].ToString();//Catch Your session

There is nothing wrong with that code (except a missing semicolon, as Shree Khanal pointed out, but that can't be the issue, right?).
As long as the code is in the page class, the Session property is available. If you have the code in a different class, you don't have the Session property available, then you need to get it from the current context:
HttpContext.Current.Session["N"] = name;
When reading the value from the session collection, the type is Object, not String, so you need to cast it:
string name = Session["N"] as string;
Using the as keyword means that you can attempt to read the value even if it would not exist, or if it happens to be set to a different data type. In that case you will get a null reference back.

Related

why does the compiler complain about missing ctor of WebSocketHandler?

I'm trying to use websocket in my project.
to do so, I installed the package Microsoft Asp.Net SignalR, which consists of WebSocketHandler abstract class.
i defined a class inheriting WebSocketHandler, but then the compiler complains:
'Microsoft.AspNet.SignalR.WebSockets.WebSocketHandler' does not contain a constructor that takes 0 arguments'.
It seems wierd to me, because the definitioin of WebSocketHandler ctor gets a nullable value, which means the ctor could get no parameter,
the definition looks like this:
protected WebSocketHandler(int? maxIncomingMessageSize);
can anybody tell me what the problem is?
thanks.
It seems wierd to me, because the definitioin of WebSocketHandler ctor gets a nullable value, which means the ctor could get no parameter
No, it doesn't. There's a big difference between receiving a null value for a nullable type, and not receiving a value at all.
If the parameter were optional, that would be a different matter - but it's not. You have to supply an argument convertible to int? in the call. If you want to provide the null value for int?, do so:
var handler = new WebSocketHandler(null);
Or if you want to avoid accidentally using any other single-parameter constructor definitions which may be applicable with a null literal as the argument, you could use:
var handler = new WebSocketHandler((int?) null);
Or:
var handler = new WebSocketHandler(default(int?));
protected member is accessible by derived class instances and there's nothing special about it. Nothing special in the class itself, either # WebSocketHandler.cs.
It just mens you need to pass in a nullable type, it does not mean it can't get any arguments.
int? maxIncomingMessageSize = 0;
var socket = new WebSocketHandler(maxIncomingMessageSize);
In your derived class you could/should define a "constructor that takes 0 arguments".
public class MyHandler : WebSocketHandler
{
// not mandatory
public MyHandler()
:this(null)
{}
// mandatory
public MyHandler(int? maxIncomingMessageSize)
:base(maxIncomingMessageSize)
{}
}

ASP.NET WebService - Object with Properties named ABC and ABCField

I have an object with two properties 'ABC' and 'ABCField'. When this object is passed to an .NET application (.NET 3.5 CF) via a WebService a Reference.vb object is created on the client which declares the properties which access variables with the same name + 'Field'.
As a result I get
Error 2 'abcField' is already declared as 'Private aBCField As
ABCOption' in this class.
'''<remarks/>
Public Property ABC() As ABCOption
Get
Return Me.aBCField
End Get
Set
Me.aBCField = value
End Set
End Property
'''<remarks/>
Public Property ABCField() As String
Get
Return Me.aBCFieldField
End Get
Set
Me.aBCFieldField = value
End Set
End Property
For compatibility reasons I would prefer not to rename or remove the properties in this object.
Is there a way arround this? Is there an attribute which can be set to change the name of the property as it is sent over the Web Service?
Its funny how once you submit a question inspiration strikes.
I have used the Xml.Serialization.XmlIgnore() attribute in the object on the service side to hide the property 'ABC' from the client and created a new property called 'ABCProxy' property which gets and sets 'ABC'.

Error when converting 'String' to 'System.IFormatProvider'

I have a page with a nested Gridview where I'm trying to fill the inner grid by pulling a string value from each line of the outer grid. When I try to pass the value from the outer grid to the string variable I get the "Error when converting 'String' to 'System.IFormatProvider'" error. I'm using the following code to store the gridview cell value to the variable:
Dim Svc_Name As String = grdOuterGridView.DataKeyNames(e.Row.RowIndex).ToString("THIRD_PARTY_SERVICE")
Can anyone tell me what I'm doing wrong here? Thanks.
The DataKeysNames is already an array of string, you don't need the ToString(....) part.
Dim Svc_Name As String = grdOuterGridView.DataKeyNames(e.Row.RowIndex)
Actually your error comes from the ToString("THIRD_PARTY_SERVICE"). The ToString() override that takes one parameter requires an object that implements IFormatProvider interface, but of course a string doesn't have this interface, thus the error. However, calling ToString() on a String has no effect as you can read from the MSDN docs
Returns this instance of String; no actual conversion is performed.

Can I cast a string object passed on command line argument to the actual object?

Is it possible to cast a command-line passed string object back to actual object?
I want to do the following, but throwing error can't cast.
Button objPro = (Button) sender;
cProduct cp = (cProduct) objPro.CommandArgument;
If no, then why?
This is what the string holds.
cProduct cpObj = (cProduct)e.Row.DataItem;
Button btnAddProduct = (Button)e.Row.FindControl("btnAddProduct");
if (btnAddProduct != null)
{
btnAddProduct.CommandArgument = cpObj.ToString();
}
You probably can't, because it's a string. It's not a cProduct (whatever that is - consider following .NET naming conventions and naming it Product instead).
Now you could do this if you had a explicit conversion operator in cProduct to create an instance from a string.
You haven't really explained what's in the string, or what's in the type - but if your cProduct type provides a ToString method which contains all the data in a reversible form, then you could easily write a method or a constructor to create the product again:
Product product = new Product(objPro.CommandArgument);
or maybe:
Product product = Product.Parse(objPro.CommandArgument);
You'll have to write that constructor/method, of course.
I would strongly recommend using a constructor or method instead of an operator, just to keep your code clearer - it's very rarely a good idea to write your own conversion operators.
Take a look at CommandArgument on MSDN. The property is a string, when you assign the a value to the property, you aren't casting some complex type to string, you are setting a string value on the property. Can you cast a string back to your object type anyway, regardless of it being a CommandArgument. I doubt it. If the argument is an int you could try int.Parse or similar for other types which have a parse method.

What's the difference between String(value) and value as String?

Just to make this clear - what is the difference between:
String(value)
and
value as String
What are the cases where you would use one over the other? They seem interchangeable...
Casting with Type(variable) can cause a runtime exeception (RTE), while "variable as type" will return null instead of throwing an exception.
See http://raghuonflex.wordpress.com/2007/07/27/casting-vs-the-as-operator/ for more explanations.
String (value) creates a new String object from a string literal. If the constructor argument is not a string literal, I assume it calls the argument object's .toString() method.
value as String will simply pass back value IF value is a String or a subclass of String. It will pass back null if value is not of type String.
The important thing to note is that String(val) creates a new object whereas value as String simply refers to value (and tests for compatibility to String).
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/String.html
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/operators.html#as

Resources