Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Please explain code

Please explain code

Scheduled Pinned Locked Moved ASP.NET
algorithmshelpquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    ASPnoob
    wrote on last edited by
    #1

    Hi, I was searching the web for ways to check if a user name is already in existence and I found the following code.

    Function DBAuthenticate(ByVal strUsername As String, ByVal strPassword As String) As Integer
    Dim bResult As Boolean = False
    Dim objConn As New OleDbConnection
    Dim strSQL As String
    Dim strGoodPassword As String
    Dim objCommand As New OleDbCommand

        objCommand.Connection = objConn
        strSQL = String.Format("SELECT p\_w FROM myDB WHERE (email='{0}');", strUsername)
        objCommand.CommandText = strSQL
        objCommand.CommandType = CommandType.Text
    
        objConn.Open()
        strGoodPassword = CType(objCommand.ExecuteScalar, String)
        objConn.Close()
    
        If Not strGoodPassword Is Nothing Then
            If strGoodPassword = strPassword Then
                bResult = True
            Else
                lblMessage.Text = "Invalid Login!"
                lblMessage.Text &= "  If you are not a member please click the above link to register."
            End If
        Else
            lblMessage.Text = "Invalid Login!"
            lblMessage.Text &= "  If you are not a member please click the above link to register."
        End If
    
        Return bResult
    End Function
    

    I don't understand three lines of the code above. what does the following mean? Dim objConn As New OleDbConnection(ConfigurationSettings.AppSettings("myDB")) Also what do the lines below do? strSQL = String.Format("SELECT p_w FROM myDB WHERE (email='{0}');", strUsername) strGoodPassword = CType(objCommand.ExecuteScalar, String) Thank you in advance for your help.

    J T 2 Replies Last reply
    0
    • A ASPnoob

      Hi, I was searching the web for ways to check if a user name is already in existence and I found the following code.

      Function DBAuthenticate(ByVal strUsername As String, ByVal strPassword As String) As Integer
      Dim bResult As Boolean = False
      Dim objConn As New OleDbConnection
      Dim strSQL As String
      Dim strGoodPassword As String
      Dim objCommand As New OleDbCommand

          objCommand.Connection = objConn
          strSQL = String.Format("SELECT p\_w FROM myDB WHERE (email='{0}');", strUsername)
          objCommand.CommandText = strSQL
          objCommand.CommandType = CommandType.Text
      
          objConn.Open()
          strGoodPassword = CType(objCommand.ExecuteScalar, String)
          objConn.Close()
      
          If Not strGoodPassword Is Nothing Then
              If strGoodPassword = strPassword Then
                  bResult = True
              Else
                  lblMessage.Text = "Invalid Login!"
                  lblMessage.Text &= "  If you are not a member please click the above link to register."
              End If
          Else
              lblMessage.Text = "Invalid Login!"
              lblMessage.Text &= "  If you are not a member please click the above link to register."
          End If
      
          Return bResult
      End Function
      

      I don't understand three lines of the code above. what does the following mean? Dim objConn As New OleDbConnection(ConfigurationSettings.AppSettings("myDB")) Also what do the lines below do? strSQL = String.Format("SELECT p_w FROM myDB WHERE (email='{0}');", strUsername) strGoodPassword = CType(objCommand.ExecuteScalar, String) Thank you in advance for your help.

      J Offline
      J Offline
      J 0
      wrote on last edited by
      #2

      ASPnoob wrote:

      Dim objConn As New OleDbConnection(ConfigurationSettings.AppSettings("myDB"))

      This line of code instantiates an OleDbConnection. This is a connection to a database such as MS Access. It can be used to query for data, etc.

      ASPnoob wrote:

      strSQL = String.Format("SELECT p_w FROM myDB WHERE (email='{0}');", strUsername)

      This ling of code basically creates a string, and assigns the value to strSQL. For example, if strUserName contains the value "you@urdomain.com", the string that will be contained in strSQL will be "SELECT p_w FROM myDB WHERE (email='you@urdomain.com');"

      ASPnoob wrote:

      strGoodPassword = CType(objCommand.ExecuteScalar, String)

      OK, this one is a little more complicated. objCommand is an OleDbCommand, which can be used to query, update, delete, insert data to or from a database (using the OleDbConnection assigned to it). In this case, they are selecting a single field (p_w) from the table myDB for a particular email address. When you use the .ExecuteScalar() method, you basically are indicating that you are only expecting a single value back from whatever query you are executing. So basically the CType will return a String representation of the p_w you are looking up in the table myDB. If none is found, strGoodPassword will be equal to nothing, otherwise, it will be equal to the p_w value stored in the table myDB for the corresponding email address. I know I probably could've worded that a little better, but hopefully you get the general idea of what is going on now. HTH.

      1 Reply Last reply
      0
      • A ASPnoob

        Hi, I was searching the web for ways to check if a user name is already in existence and I found the following code.

        Function DBAuthenticate(ByVal strUsername As String, ByVal strPassword As String) As Integer
        Dim bResult As Boolean = False
        Dim objConn As New OleDbConnection
        Dim strSQL As String
        Dim strGoodPassword As String
        Dim objCommand As New OleDbCommand

            objCommand.Connection = objConn
            strSQL = String.Format("SELECT p\_w FROM myDB WHERE (email='{0}');", strUsername)
            objCommand.CommandText = strSQL
            objCommand.CommandType = CommandType.Text
        
            objConn.Open()
            strGoodPassword = CType(objCommand.ExecuteScalar, String)
            objConn.Close()
        
            If Not strGoodPassword Is Nothing Then
                If strGoodPassword = strPassword Then
                    bResult = True
                Else
                    lblMessage.Text = "Invalid Login!"
                    lblMessage.Text &= "  If you are not a member please click the above link to register."
                End If
            Else
                lblMessage.Text = "Invalid Login!"
                lblMessage.Text &= "  If you are not a member please click the above link to register."
            End If
        
            Return bResult
        End Function
        

        I don't understand three lines of the code above. what does the following mean? Dim objConn As New OleDbConnection(ConfigurationSettings.AppSettings("myDB")) Also what do the lines below do? strSQL = String.Format("SELECT p_w FROM myDB WHERE (email='{0}');", strUsername) strGoodPassword = CType(objCommand.ExecuteScalar, String) Thank you in advance for your help.

        T Offline
        T Offline
        Tirthadip
        wrote on last edited by
        #3

        ASPnoob wrote:

        I don't understand three lines of the code above. what does the following mean? Dim objConn As New OleDbConnection(ConfigurationSettings.AppSettings("myDB"))

        There is a Configuration file(web.config in case of web application and App.config in case of Windows application) where we can set the connection string as well as other configuration settings of our .NET solution.This is an XML file.Under the appSettings node the connection string is written with the string variable name myDB.whereever in the project you need to reference the connection string you will get it by ConfigurationSettings.AppSettings("myDB")

        Tirtha Do not go where the path may lead, go instead where there is no path and leave a trail. Author: Ralph Waldo Emerson (1803-82), American writer, philosopher, poet, essayist

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups