1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

[VB.net] An Offline Login System..Help please:D

Discussion in 'Code Snippets and Tutorials' started by singapore, Jun 15, 2010.

  1. singapore

    singapore Level III

    Joined:
    Jan 4, 2010
    Messages:
    554
    Likes Received:
    10
    i did a simple login system which make use of a offline database using this tutorial..

    http://www.youtube.com/watch?v=p0dcPiLYrck

    can anyone help me by adding a new user function to the program?

    +rep to anyone that can help me :D

    thanks tharoux in advance XD XD

    here is the source code.
     

    Attached Files:

  2. tharoux

    tharoux Level IV

    Joined:
    Dec 30, 2006
    Messages:
    2,733
    Likes Received:
    126
    Location:
    In front of my PC, Montreal
    Hi !
    First, some advices... XD

    I watched the videos, and you can do all this with ALOT less code.
    -By using "Imports System.Data.SqlClient" at the very top of the code, you'll be able to skip all the "sqlClient." in the entire code.
    -I traded the "adaptor" for a SqlDataReader, it's lighter and faster to use cause it's read-only

    Here's how I would do it. This code assume you use the same table as in the video.
    It will add a record (using the "INSERT INTO" sql command) and than show all the records in the database to confirm yours was added.

    BTW, executeNonQuery() will return the numbers of record added. Usefull if you add more than one at once.
    The executeReader() will return a DataReader, usefull to read the content like I said previously.
    Code (Text):
    1. Imports System.Data.SqlClient
    2.  
    3. Public Class Form1
    4.  
    5.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    6.         Dim connection As SqlConnection
    7.         Dim command As SqlCommand
    8.         Dim datareader As SqlDataReader
    9.  
    10.         connection = New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Members.mdf;Integrated Security=True;User Instance=True")
    11.         connection.Open()
    12.         command = New SqlCommand("INSERT INTO [User] VALUES ('testing1','testing2');", connection)
    13.         command.ExecuteNonQuery()
    14.         command = New SqlCommand("SELECT * FROM [User];", connection)
    15.         datareader = command.ExecuteReader()
    16.         While datareader.Read()
    17.             MsgBox(datareader(0).ToString)
    18.             MsgBox(datareader(1).ToString)
    19.             MsgBox(datareader(2).ToString)
    20.         End While
    21.         datareader.Close()
    22.         connection.Close()
    23.     End Sub
    24. End Class
     
  3. singapore

    singapore Level III

    Joined:
    Jan 4, 2010
    Messages:
    554
    Likes Received:
    10
    lol knew u were gonna be the first..

    alright sure..will try it and get back to u once i go on my laptop..(currently using my desktop which is programming-free XD)

    Still need the New User function - to the other 3 who dl the file:)..
     
  4. tharoux

    tharoux Level IV

    Joined:
    Dec 30, 2006
    Messages:
    2,733
    Likes Received:
    126
    Location:
    In front of my PC, Montreal
    the new user function is in there ;)
    You're not expecting someone to give it already made ? it's too easy :maha:
     
  5. singapore

    singapore Level III

    Joined:
    Jan 4, 2010
    Messages:
    554
    Likes Received:
    10
    LOL !..opps didn't even read the code when i was on my desktop and i misread what u typed :p..

    sooo sorry..gonna try that out. will update if i meet with any problem:)..


    edited : tried it out..

    actually i wan the values to be entered from textbox1 and textbox2 :p..

    the values can't be stored into the table? i am able to log in using the values : testing1, testing2 but the table doesn't show these two values in the respective columns..anyway to do that?

    2nd edit : ha

    Code (Text):
    1. command = New SqlClient.SqlCommand("INSERT INTO [User](Username, Password) VALUES ('" & TextBox1.Text & "', '" & TextBox2.Text & "');", connection)
    the program can now add a new username & password temporarily..as specified by me in textbox1 and textbox2..

    the problem is that the table won't be updated and after restarting the program, the new user that i created was gone..
     
  6. tharoux

    tharoux Level IV

    Joined:
    Dec 30, 2006
    Messages:
    2,733
    Likes Received:
    126
    Location:
    In front of my PC, Montreal
    Sorry, I missed your post because you only edited it.

    I don't have VB with me right now but there's an UPDATE function somewhere that should be put before the connection.close() but after the "INSERT INTO".
     
  7. singapore

    singapore Level III

    Joined:
    Jan 4, 2010
    Messages:
    554
    Likes Received:
    10
    its alright :) ..yeah, i saw that function online before but i tried putting it here and there and it doesn't work..

    can u take a look at the program again and maybe tell me where to put it :D ..i tried putting it right before close.connection, doesn't give any error but it still doesn't update the table.

    btw, if i were to use the SET function, will it be easier?

    i found this online
    Code (Text):
    1. UPDATE Persons
    2. SET Address='Nissestien 67', City='Sandnes'
    3. WHERE LastName='Tjessem' AND FirstName='Jakob'
    which will give

    P_Id LastName FirstName Address City
    1 Hansen Ola Timoteivn 10 Sandnes
    2 Svendson Tove Borgvn 23 Sandnes
    3 Pettersen Kari Storgt 20 Stavanger
    4 Nilsen Johan Bakken 2 Stavanger
    5 Tjessem Jakob Nissestien 67 Sandnes

    meaning if i were to program mine, it would be something like this

    Code (Text):
    1. UPDATE [Users]
    2. SET Username='ABC', Password='123'
    3. WHERE ID='?'
    what do i put for the ID in order to user and pass under the correct ID number.

    oh ya, aha coming on to NF during work !!! XD XD ..
     

    Attached Files: