Results 1 to 8 of 8

Thread: Session? (Login from msql) Read for more info..

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2010
    Posts
    7

    Session? (Login from msql) Read for more info..

    Hello there,

    I have a website that has a login (Like most websites xD) This then obviosly fetches information from a database and loads it on the page. (EG. Welcome "Display Name")

    I have designed and coded a application for my site you can use the features from my web on your desktop, I have added a login (required to use the application) and a register. Both login and register work (Fetching information from the database and writing to the database).

    Now I have those out of the way I'm now onto the main part of my program which is to display infomation onto the application about the user account. This could include editing the user account, uploading content to the website or viewing content from the website.. (Sorry but I'd like to explain how I have certain things to get the point across clearly )

    Anyway how would I create a sort of session? Like PHP, once you login you can grab information from the database based on the information submitted from the login which was fetched from the database.. When the user presses login on my application it brings them to the main part of the application but I'm now unsure how to load variables and/or session data, could anyone help me accomplish this?

    (Side note, I have also sha encryption on my website in the register/login, at the moment VB reads the information from the textbox as normal text is there anyway I can get it to read the sha encryption? and also insert data into the database with this encryption?) - This question is optional ..

    Thanks in advance for your help, I am a noob at this but I can learn quickly.

  2. #2
    Hyperactive Member dnanetwork's Avatar
    Join Date
    Oct 2007
    Location
    Mumbai
    Posts
    349

    Re: Session? (Login from msql) Read for more info..

    abt how to use session..

    Session.Add("SessionName","YourValue");
    // That's how u add value to session...

    string username = Session["YouValue"].toString();
    // Grab the value from session..
    Next SHA Encryption.. try this SHA512 code and go to heaven

    Code:
    protected string MySHA512( )
    
        {
    
            SHA512 sha512 = new System.Security.Cryptography.SHA512Managed();
    
            byte[] sha512Bytes = System.Text.Encoding.Default.GetBytes("vb.netdev");
    
            byte[] cryString = sha512.ComputeHash(sha512Bytes);
    
            string sha512Str = string.Empty;
    
            for (int i = 0; i < cryString.Length; i++)
    
            {
    
                sha512Str += cryString[i].ToString("X");
    
            }
    
            return sha512Str;
    
        }
    hope that helps.

  3. #3
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Session? (Login from msql) Read for more info..

    Hey,



    When you say:

    Quote Originally Posted by LeeMC89 View Post
    I have added a login (required to use the application) and a register. Both login and register work (Fetching information from the database and writing to the database).
    Do you mean that you have created your own code to do this? If so, have you thought about using the ASP.Net Membership, Profile and Roles Providers? They provide this functionality, out of the box, in a simple to implement manner. Including the functionality that you are now trying to add to the application.

    Take a look at this very detailed walkthrough on what can be done using these providers:

    http://www.4guysfromrolla.com/articles/120705-1.aspx

    It is a big article, but well worth reading, and is broken down into logical chunks.

    Gary

  4. #4
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Session? (Login from msql) Read for more info..

    Quote Originally Posted by dnanetwork View Post
    Session.Add("SessionName","YourValue");
    // That's how u add value to session...

    string username = Session["YouValue"].toString();
    // Grab the value from session..
    This actually wouldn't work.

    As per the documentation here:

    http://msdn.microsoft.com/en-us/libr...state.add.aspx

    The first parameter is the name, and the second parameter is the value. You in order to retrieve the value from the object collection, you would need to use this:

    Code:
    Session.Add("VariableName","VariableValue");
    // That's how u add value to session...
    
    string username = Session["VariableName"].ToString();
    Alternatively, you can do this:

    Code:
    Session["VariableName"] = "VariableValue";
    string username = Session["VariableName"].ToString();
    Gary

  5. #5

    Thread Starter
    New Member
    Join Date
    Mar 2010
    Posts
    7

    Re: Session? (Login from msql) Read for more info..

    Hi,

    Thanks for the replies, I'm a little confused now to how I'm supposed to create my program..?

    So you are supposed to use asp.net for the login? or sessions? Because I read that asp.net is for creating websites, how does asp.net help me make a application?

    Code:
    Session.Add("VariableName","VariableValue");
    // That's how u add value to session...
    
    string username = Session["VariableName"].ToString();
    So.. I'd add session.add to my login code and add a name to variablename (username) and variablevalue as (email)?

    This is my code from the OK button (I used a basic tutorial and added the rest myself)

    Code:
      Dim conn As MySqlConnection
            'connection to DB
            conn = New MySqlConnection()
            conn.ConnectionString = "server=******; user id=******; password=*****; database=*******;"
            'see if connection filed.
            Try
                conn.Open()
            Catch myerror As MySqlException
                MessageBox.Show("Cannot connect to database: " & myerror.Message)
            End Try
            Dim myadapter As New MySqlDataAdapter
    
            Dim sqlquary = "SELECT * FROM accounts WHERE email = '" & txtuser.Text & "' AND progpass = '" & txtpass.Text & "';"
            Dim mycommand As New MySqlCommand
            mycommand.Connection = conn
            mycommand.CommandText = sqlquary
            myadapter.SelectCommand = mycommand
            Dim mydata As MySqlDataReader
            mydata = mycommand.ExecuteReader()
            If mydata.HasRows = 0 Then
                MsgBox("This account does not exist!" & vbCrLf & vbCrLf & "To access the progam you need to register for a account.", _
    vbExclamation, _
    "ERROR - Invalid Username / Password.")
            Else
                MsgBox("Welcome to MyMediaUpload Desktop! " & txtuser.Text & vbCrLf & vbCrLf, _
    vbExclamation, _
    "Thanks for logging in.")
                MMUDesktop.Show()
                Me.Hide()
            End If
    Ok, so when I add the session in there how am I able to access the database to get data into the program? (as said in my previous post when a user logs in I want the program to then display information that is related to their account)

    I'm just curious now to how I would now develop my application or how ASP.net is used with Visual Basic :/?..

    Thanks.

  6. #6
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Session? (Login from msql) Read for more info..

    Quote Originally Posted by LeeMC89 View Post
    So you are supposed to use asp.net for the login? or sessions? Because I read that asp.net is for creating websites, how does asp.net help me make a application?
    ASP.Net is a development platform which allows to you create Web Applications, Web Sites, and Web Services. Your web application/site will contain all the logic that is required to perform the operations that you want to take, such as, writing information into a Session Variable, or authenticating the user against information stored within a Database.

    Quote Originally Posted by LeeMC89 View Post
    Code:
    Session.Add("VariableName","VariableValue");
    // That's how u add value to session...
    
    string username = Session["VariableName"].ToString();
    So.. I'd add session.add to my login code and add a name to variablename (username) and variablevalue as (email)?
    This is one way to do it, but I would suggest that this is quite a long way to do it. For each property that you want to make available, you would have to write code to do it. I would suggest that using the ASP.Net Profile Provider, that you will have much more flexibility.

    Quote Originally Posted by LeeMC89 View Post
    This is my code from the OK button (I used a basic tutorial and added the rest myself)

    Code:
      Dim conn As MySqlConnection
            'connection to DB
            conn = New MySqlConnection()
            conn.ConnectionString = "server=******; user id=******; password=*****; database=*******;"
            'see if connection filed.
            Try
                conn.Open()
            Catch myerror As MySqlException
                MessageBox.Show("Cannot connect to database: " & myerror.Message)
            End Try
            Dim myadapter As New MySqlDataAdapter
    
            Dim sqlquary = "SELECT * FROM accounts WHERE email = '" & txtuser.Text & "' AND progpass = '" & txtpass.Text & "';"
            Dim mycommand As New MySqlCommand
            mycommand.Connection = conn
            mycommand.CommandText = sqlquary
            myadapter.SelectCommand = mycommand
            Dim mydata As MySqlDataReader
            mydata = mycommand.ExecuteReader()
            If mydata.HasRows = 0 Then
                MsgBox("This account does not exist!" & vbCrLf & vbCrLf & "To access the progam you need to register for a account.", _
    vbExclamation, _
    "ERROR - Invalid Username / Password.")
            Else
                MsgBox("Welcome to MyMediaUpload Desktop! " & txtuser.Text & vbCrLf & vbCrLf, _
    vbExclamation, _
    "Thanks for logging in.")
                MMUDesktop.Show()
                Me.Hide()
            End If
    Ok, so when I add the session in there how am I able to access the database to get data into the program? (as said in my previous post when a user logs in I want the program to then display information that is related to their account)

    I'm just curious now to how I would now develop my application or how ASP.net is used with Visual Basic :/?..

    Thanks.
    First up, don't concatenate your SQL String like that. Doing so leaves you open to SQL injection attacks. If you take a look at the link in my signature regarding this, you will find a list of reasons why this is a bad idea.

    Secondly, MsgBox, or rather MessageBox is a class within System.Windows.Forms and since you are creating a Web Application, you should not use this, so I would suggest that you remove it, in preference to adding logging to your application, or writing directly to a Label on your page.

    ASP.Net is the platform, and the server side code that you write, in either VB.Net or C#, is what gets executed when the page loads.

    As I have already mentioned, all of the above work is done for you if you use the ASP.Net Membership Provider, which I would guess that you already have installed, since you have MySqlConnection, which you would only get if you installed the MySql Connector for .Net.

    Gary

  7. #7

    Thread Starter
    New Member
    Join Date
    Mar 2010
    Posts
    7

    Re: Session? (Login from msql) Read for more info..

    Ok Thanks I'll take a look into that ..

    Sorry if I'm starting to sound really noobish.. Can I still make my application in VB?? (I want to make a windows application not a website, I think there has been some mix up somewhere as I probably still didn't explain properly.. Sorry DX.., I did post in the VB forum but it got moved here so I'm really confused..)

    Sorry if I sound dumb I just still don't understand how it works, although I have looked through some guides...

    Thanks for you help!
    Last edited by LeeMC89; Mar 17th, 2010 at 05:51 PM.

  8. #8
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Session? (Login from msql) Read for more info..

    Hey,

    Don't worry about asking questions, everyone has to start somewhere, and no-one here will (or at least they shouldn't) slag you off for asking them.

    Ok, back on topic....

    Visual Studio as a whole lets to do lots of thing, for instance, create a Windows Application, a Windows Console Application, a Window Service, a Mobile Device Application, a Web Site, a Web Application, a Web Service etc.

    Across all of these application types, you can create all of these in VB.Net or C# (notice I am limiting this to talking about the main managed languages, but there are other options if C++ is more your thing).

    When it comes to ASP.Net Applications, i.e. web site, web service, web application, this is exactly the same. You can create your application in either VB.Net or C#. This is your server side code that I am talking about here. In addition to this, you will create your ASPX markup. This is what the ASP.Net engine will parse and convert to HTML for you, to display on the client machine.

    Does that make sense?

    So basically, your ASP.Net application will be a combination of ASPX markup, and either VB.Net or C# server side code.

    Gary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width