Showing posts with label variable. Show all posts
Showing posts with label variable. Show all posts

Saturday, March 31, 2012

Set a session variable to pre defined value

I just want to set a sesison variable to a defined value when a page loads, I've tried the following but it doesn't seem to work. Any ideas where I'm going wrong?

<%# Session("var1") = "abc" %
Thanks

Julian ParkinsonYou've used adata binding expression, so it will only be called when DataBind() is called on the Page.

I think you want to use aninline code expression instead:

<% Session("var1") = "abc" %>

Hi,

You need to put the Session variable in the Page_Load like this:


if(!Page.IsPostBack)
{

Session["Name"] = "NewValue";
Response.Redirect("Mynewpage.aspx");

}

And then in the Mynewpage.aspx you can recieve the session using this code in the Page_Load with not postback.

if(Session["Name"] == null )
Response.Redirect("LoginPage.aspx");

else
Label1.Text = Session["Name"].ToString();


Dan,

Cheers for that. I just needed the remove the hash!! Duhh!

I'm moving from asp vb to asp.net vb and i'm finding it strange, some stuff is the same, some isn't sometimes I can't see the wood for the trees.

Thanks again.

Set application variable from a class

Hi
I am trying to set an application variable from a class file in my website.
Currently, the class file inherits from System.Web.UI.Page
I also referenced all the namespaces a normal webform would.
However, when i try save my application variable like such:
Application["test"] = "hello";
I get the following error:
Object reference not set to an instance of an object
How can i set an application variable from a class
It works fine from a webform
TIA
GrantGot It
HttpContext.Current.Application[ ...
"Grant Merwitz" <grant@.workshare.com> wrote in message
news:%23hZBPQ9NGHA.3984@.TK2MSFTNGP14.phx.gbl...
> Hi
> I am trying to set an application variable from a class file in my
> website.
> Currently, the class file inherits from System.Web.UI.Page
> I also referenced all the namespaces a normal webform would.
> However, when i try save my application variable like such:
> Application["test"] = "hello";
> I get the following error:
> Object reference not set to an instance of an object
> How can i set an application variable from a class
> It works fine from a webform
> TIA
> Grant
>

Set application variable from a class

Hi

I am trying to set an application variable from a class file in my website.

Currently, the class file inherits from System.Web.UI.Page
I also referenced all the namespaces a normal webform would.

However, when i try save my application variable like such:
Application["test"] = "hello";
I get the following error:
Object reference not set to an instance of an object

How can i set an application variable from a class
It works fine from a webform

TIA

GrantGot It

HttpContext.Current.Application[ ...

"Grant Merwitz" <grant@.workshare.com> wrote in message
news:%23hZBPQ9NGHA.3984@.TK2MSFTNGP14.phx.gbl...
> Hi
> I am trying to set an application variable from a class file in my
> website.
> Currently, the class file inherits from System.Web.UI.Page
> I also referenced all the namespaces a normal webform would.
> However, when i try save my application variable like such:
> Application["test"] = "hello";
> I get the following error:
> Object reference not set to an instance of an object
> How can i set an application variable from a class
> It works fine from a webform
> TIA
> Grant

Thursday, March 22, 2012

set local variable with values of a dataset...

How can I set my local variables with a result in a dataset? with a sql-statement I use the sqldatareader but what's the way when i use a xml file?

Thanx!Hi siebengewald

Try this:


Dim ds as DataSet = new DataSet()
ds.readxml(yourpath & yourxmlfile.xml)
dim varLocal as string = ds.tables("yourtablename").row(numberofyourrow).item("itemname").value

It's not the best way to do it, but the shortest :-)
Are you trying to get a single node from your xml file? I'm a little confused as to what you want. Do you want to set a variable to a value from your xml file? If so what you need to do is load your xml file then perform an xpath statement to obtain your value and load it into the variable. It would be something like this..

string myVariable; //Your variable
string xDoc = "mydocument.xml"; //string to the xml document in your system
XmlDocument myDoc = new XmlDocument(); //a xml document declaration
myDoc.Load(xDoc); //loading your xml document

string xpath = "/masterNode/child::subNode"; //xpath query for the node you want. replace MasterNode and subNode with your root and sub root node.

XmlNode myNode = myDoc.SelectSingleNode(xpath); //querying the node
myVariable = myNode.InnerXml; //setting your variable to the node value

This is the simplest form of code to obtain a single node value from an xml file. Hope this helps
Jeffs way is another good solution to do it directly with the XML Classes. Just to note, my example is done with VB.net and Jeffs with C#
i load a dataset:


Dim ProdFile As New FileStream(Server.MapPath("type.xml"), FileMode.Open, FileAccess.Read)
Dim dsProductsData As New DataSet
Dim vProductsView As New DataView
dsProductsData.ReadXml(ProdFile)

If Not IsPostBack Then
DLProdId.DataSource = dsProductsData.Tables(0).DefaultView
DLProdId.DataBind()
Else
vProductsView = New DataView(dsProductsData.Tables(0))
'vProductsView.RowFilter = "id='" & DLProdId.SelectedItem.Text & "'"
GProdDetails.DataSource = vProductsView
GProdDetails.DataBind()
End If

now the result i got is the complete table, so i can only fill a datagrid or dropdownlist. Now is my question, can i set a local string variable to a specific row of a specific record?
Yup, you sure can! Since you have a dataset, you can just reference a record in it by:

txtBox.text = dataset.Tables("TableName").Rows(row#)("FieldName")

try that!
IT WORKS PERFECTLY THANK YOU VERY MUCH!!!