Variable help

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 1461 Views - Last Post: 18 May 2008 - 09:24 AM Rate Topic: -----

#1 jabarian  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 14-February 08

Variable help

Posted 16 May 2008 - 06:53 AM

Hi everyone,

I've got to design a windows forms project which calculates various calculations. The username, answers and calculations used need to be stored in a database.

Ive created a login and registration form which work fine, ive got the calculation description and the answers saving to another database fine also.

My problem is that i need to store the user name with this data. I know that i need to use sessions but my experence with them is very poor.

I'm using seperate vb code docs to connect to the database, do i place the sessions in there or the main form.


	   Session("username") = txtUsername.Text
	   

Ive tried this but im getting an error "session is not declared"

Any help would be great.
Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Variable help

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Variable help

Posted 16 May 2008 - 09:34 AM

Session variables are not available for a Windows Form, they are used in ASP.NET web applications.

So is this a web application or a windows form?
Was This Post Helpful? 0
  • +
  • -

#3 jabarian  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 14-February 08

Re: Variable help

Posted 16 May 2008 - 10:21 AM

View Postjayman9, on 16 May, 2008 - 09:34 AM, said:

Session variables are not available for a Windows Form, they are used in ASP.NET web applications.

So is this a web application or a windows form?


Its a web form, its going to be WCF, any ideas
Was This Post Helpful? 0
  • +
  • -

#4 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Variable help

Posted 16 May 2008 - 11:41 AM

Try importing the following.

Imports System.Web.SessionState

Was This Post Helpful? 0
  • +
  • -

#5 jabarian  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 14-February 08

Re: Variable help

Posted 16 May 2008 - 11:54 AM

Hi, Ive created a login page, the page then takes u in to the application which is like a calculator, i want to save the calculation made, answer, username and date in a database. Ive got the calc and the answer saved but i'm struggling on the other to.

With the username ive declared the username in the login form, is there anyway i can make the varable availabe for the whole app so i can refer to it on the calc page??

And for the date i dont really know where to start, ive had a good hunt but nothing, any help waould be awsome
Was This Post Helpful? 0
  • +
  • -

#6 jabarian  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 14-February 08

Re: Variable help

Posted 16 May 2008 - 12:14 PM

View Postjayman9, on 16 May, 2008 - 11:41 AM, said:

Try importing the following.

Imports System.Web.SessionState

no luck, cant i just make the variable from the login page accessable to the whole application, so i can refer back to it from the main form, if so any idea how . . ?

Thanks again
Was This Post Helpful? 0
  • +
  • -

#7 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Variable help

Posted 16 May 2008 - 01:28 PM

What main form?

You said this was a web application. There isn't any forms in a web application. Do you mean web page?

It would be more helpful if you actually posted some code. What you are saying doesn't make sense.
Was This Post Helpful? 0
  • +
  • -

#8 bflosabre91  Icon User is offline

  • go sabres

Reputation: 105
  • View blog
  • Posts: 1,439
  • Joined: 22-February 08

Re: Variable help

Posted 16 May 2008 - 01:30 PM

the easiest way to do that is on the login form declare a variable that is Public Shared and use that variable to hold the username. its not the ideal way but its very quick and easy. so the variable would look like

Public Shared sUsername as String

'on login set the variable
sUsername = txtUserName.text



the date, you can just use the Today() function in vb to pass todays date to database. or if ur using stored procedures to write everything to the database, use you would a sql function called GetDate()

This post has been edited by bflosabre91: 16 May 2008 - 01:31 PM

Was This Post Helpful? 0
  • +
  • -

#9 jabarian  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 14-February 08

Re: Variable help

Posted 16 May 2008 - 01:58 PM

View Postbflosabre91, on 16 May, 2008 - 01:30 PM, said:

the easiest way to do that is on the login form declare a variable that is Public Shared and use that variable to hold the username. its not the ideal way but its very quick and easy. so the variable would look like

Public Shared sUsername as String

'on login set the variable
sUsername = txtUserName.text



the date, you can just use the Today() function in vb to pass todays date to database. or if ur using stored procedures to write everything to the database, use you would a sql function called GetDate()

Hi thanks for the response, but i've done that yet on the other form it still says username is undefined??
Was This Post Helpful? 0
  • +
  • -

#10 nofear217  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 14
  • View blog
  • Posts: 324
  • Joined: 08-November 07

Re: Variable help

Posted 16 May 2008 - 01:58 PM

there are two other ways if you plan to do this in the future, you could also declare it as a Protected Friend in the login class and then access it from the calculator class

or

you can declare a variable as Protected Friend in the calculator class and then pass the username value into that variable after the login is validated
Was This Post Helpful? 0
  • +
  • -

#11 jabarian  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 14-February 08

Re: Variable help

Posted 16 May 2008 - 02:07 PM

View Postnofear217, on 16 May, 2008 - 01:58 PM, said:

there are two other ways if you plan to do this in the future, you could also declare it as a Protected Friend in the login class and then access it from the calculator class

or

you can declare a variable as Protected Friend in the calculator class and then pass the username value into that variable after the login is validated

ok great, can you point me to the right direction on how to do that?
Was This Post Helpful? 0
  • +
  • -

#12 jabarian  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 14-February 08

Re: Variable help

Posted 16 May 2008 - 02:52 PM

View Postjayman9, on 16 May, 2008 - 01:28 PM, said:

What main form?

You said this was a web application. There isn't any forms in a web application. Do you mean web page?

It would be more helpful if you actually posted some code. What you are saying doesn't make sense.

Sorry, i'll be more clear.

Its a windows application,

Ive got a front login page, I need the username entered into a database on the next page when the calculation id done.

on the login page ive got


Public Class LoginForm2

	Public Shared Username As String

	Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click


		Dim insert As New login()
		Dim Password As String
		Dim DataContent As String

		Username = UsernameTextBox.Text
		Password = PasswordTextBox.Text
		DataContent = insert.Login(Username, Password)





on the other page ive got

Public Class home
	'Inherits System.Windows.Forms.Form
	Private myCalculation As New Calculations.Calculations
	Private Calc As String
	Public Shared Username
	Private Sub Hypotenuse_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Hypotenuse.Click
		Dim adj1 As Double
		Dim opp1 As Double
		Dim answer As Double
		Dim add As New Service()
		Dim Result As String
		Dim DataContent As String
		Dim home As New home
		If Convert.ToString(adj.Text) <> "" Then
			If Convert.ToString(opp.Text) <> "" Then
				'proceed
			Else
				MsgBox("Please enter opposite value")

				Exit Sub
			End If
		Else
			MsgBox("Please Enter adjacent value")

			Exit Sub
		End If
		Calc = "Hyp with Adj and Opp"
		adj1 = CType(adj.Text, Double)
		opp1 = CType(opp.Text, Double)

		answer = myCalculation.hypcalc(adj1, opp1)

		Answerlbl.Text = answer.ToString
		Result = Answerlbl.Text


		DataContent = add.InsertData(Username, Calc, Result, Today())

	End Sub



not working tho, ive debugged it and the username variable is not even making it through to the next page.

Sorry to be a pain.
Was This Post Helpful? 0
  • +
  • -

#13 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Variable help

Posted 16 May 2008 - 03:14 PM

As I already mentioned Session variables are not available in a windows form application.

And since you already have another topic going on this subject I am going to merge the two together.

Lets keep it all in one place.

Are you instantiating your 'home' form from the 'LoginForm2' after the validating the user?

If not, then how are you instantiating the 'home' form?
Was This Post Helpful? 0
  • +
  • -

#14 jabarian  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 14-February 08

Re: Variable help

Posted 16 May 2008 - 03:34 PM

View Postjayman9, on 16 May, 2008 - 03:14 PM, said:

As I already mentioned Session variables are not available in a windows form application.

And since you already have another topic going on this subject I am going to merge the two together.

Lets keep it all in one place.

Are you instantiating your 'home' form from the 'LoginForm2' after the validating the user?

If not, then how are you instantiating the 'home' form?

I'm trying to instance the login form from the home form, the login form is where i create the public shared username as string

I refer to that from the home page with public shared username at the top of the page.

Then i just write username where i want to enter it into the database. for some reason the variable is not coming over from the login page to the home page. I'm sure its simple but i'm missing it.

Thanks so much for your help
Was This Post Helpful? 0
  • +
  • -

#15 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Variable help

Posted 16 May 2008 - 03:53 PM

Well if you are instantiating the 'login' form from your 'home' form, then create a Property on the 'login'. From within the 'login' form, assign the username to this property on successful login.

Back on your 'home' form, after you instantiate the 'login' form, then get the username from the Property that you created.

Now you can use it on your 'home' form.

Example:
Public Class Login

Private _userName As String

Public Property UserName() As String

Get
   Return _userName
End Get

Set (ByVal value As String)
   _userName = value
End Set

End Property

  'the rest of your code that does validation and such

  'after user has been validated then assign the username to the property
   UserName = txtUserName.Text

End Class


Public Class Home
   Private _userName As String

   ' the following code should be inside your Load event
   Dim login As New Login()
   login.ShowDialog()
   
   'now get the username and store it in a global variable
   'so that it is accessable whenever you need it.
   _userName = login.UserName

  ''the rest of your code
End Class


This is just to give you an idea of how to do it. Give it a try and let us know how it goes.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2