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. String.Format

String.Format

Scheduled Pinned Locked Moved ASP.NET
questioncsshelptutorial
5 Posts 4 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 have posted this question before but the response was less than satisfactory. What does the following line of code actually do?

        strSQL = String.Format("SELECT UserName FROM MembersInfotbl WHERE (email='{0}');", txtUserName.Text)
    

    I had done some search on String.Format and some of the sites I visited said that the 1st value within the parentheses serves as the place holder for the 2nd value. So for example if k=String.Format( Page{0}, 1) the number 0 will be replaced by the 2nd number which is 1. That doesn't make sense when applied to the line above, because then email='{0}' would become email= txtUserName.text. Please correct me if I'm wrong. Thank you in advance for your help.

    A 1 Reply Last reply
    0
    • A ASPnoob

      Hi, I have posted this question before but the response was less than satisfactory. What does the following line of code actually do?

          strSQL = String.Format("SELECT UserName FROM MembersInfotbl WHERE (email='{0}');", txtUserName.Text)
      

      I had done some search on String.Format and some of the sites I visited said that the 1st value within the parentheses serves as the place holder for the 2nd value. So for example if k=String.Format( Page{0}, 1) the number 0 will be replaced by the 2nd number which is 1. That doesn't make sense when applied to the line above, because then email='{0}' would become email= txtUserName.text. Please correct me if I'm wrong. Thank you in advance for your help.

      A Offline
      A Offline
      Arun Immanuel
      wrote on last edited by
      #2

      ASPnoob wrote:

      I had done some search on String.Format and some of the sites I visited said that the 1st value within the parentheses serves as the place holder for the 2nd value.

      What you have got from the search is right. But you have a small misunderstanding with that.

      ASPnoob wrote:

      strSQL = String.Format("SELECT UserName FROM MembersInfotbl WHERE (email='{0}');", txtUserName.Text)

      if the textbox has some value say XXX@YYY.COM, then strSQL will be SELECT UserName FROM MembersInfotbl WHERE (email='XXX@YYY.COM');

      Regards, Arun Kumar.A

      A 1 Reply Last reply
      0
      • A Arun Immanuel

        ASPnoob wrote:

        I had done some search on String.Format and some of the sites I visited said that the 1st value within the parentheses serves as the place holder for the 2nd value.

        What you have got from the search is right. But you have a small misunderstanding with that.

        ASPnoob wrote:

        strSQL = String.Format("SELECT UserName FROM MembersInfotbl WHERE (email='{0}');", txtUserName.Text)

        if the textbox has some value say XXX@YYY.COM, then strSQL will be SELECT UserName FROM MembersInfotbl WHERE (email='XXX@YYY.COM');

        Regards, Arun Kumar.A

        A Offline
        A Offline
        ASPnoob
        wrote on last edited by
        #3

        The reason I was confused was because txtUserName.text is used for inputing a user's user name. The following was the code that I found for validating a user's Login.

        Function DBAuthenticate(ByVal strUsername As String, ByVal strPassword As String) As Integer
        Dim bResult As Boolean = False
        Dim objConn As New OleDbConnection(ConfigurationSettings.AppSettings("myDB"))
        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}');", txtUserName.text)
            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
        

        Could you please explain the role of strSQL in this code? I am confused as to why it was used like that. Thank you in advance for your help.

        P D 2 Replies Last reply
        0
        • A ASPnoob

          The reason I was confused was because txtUserName.text is used for inputing a user's user name. The following was the code that I found for validating a user's Login.

          Function DBAuthenticate(ByVal strUsername As String, ByVal strPassword As String) As Integer
          Dim bResult As Boolean = False
          Dim objConn As New OleDbConnection(ConfigurationSettings.AppSettings("myDB"))
          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}');", txtUserName.text)
              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
          

          Could you please explain the role of strSQL in this code? I am confused as to why it was used like that. Thank you in advance for your help.

          P Offline
          P Offline
          PandemoniumPasha
          wrote on last edited by
          #4

          hi, the purpose of strSQL is to store the sql command string, which in this case will retrieve the field p_w from the table myDB and whose email is equal to the email address provided by the user. the string.format function simply replaces {0} with the string from the textbox - txtUserName, which the user provided. the otput of this will be same as : strSQL = "SELECT p_w FROM myDB WHERE (email='" & txtUserName.text & "');" you need this string because it tells the command object what to fetch from the database. hope this helps.

          regards :)

          1 Reply Last reply
          0
          • A ASPnoob

            The reason I was confused was because txtUserName.text is used for inputing a user's user name. The following was the code that I found for validating a user's Login.

            Function DBAuthenticate(ByVal strUsername As String, ByVal strPassword As String) As Integer
            Dim bResult As Boolean = False
            Dim objConn As New OleDbConnection(ConfigurationSettings.AppSettings("myDB"))
            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}');", txtUserName.text)
                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
            

            Could you please explain the role of strSQL in this code? I am confused as to why it was used like that. Thank you in advance for your help.

            D Offline
            D Offline
            Dave Sexton
            wrote on last edited by
            #5

            Firstly, that appears to be a nasty bit of code. No wonder you're confused. If I were you I'd do some research, some learning & then rewrite it properly. strSQL is being used here as an SQL Select statement[^] that retrieves data, in this case an email address, from a database (I'm guessing Access) using a Command Object[^]. Why is this code is bad? Here's a few reasons off the cuff - I'm giving you these (and a few links for your benefit) to point you in the right direction when you rewrite it 1. The function is being passed an argument (strUsername) that is never used. 2. There is no input validation being done on the email address in the SQL statement which opens the database up to SQL Injection[^]. 3. There are no Try...Catch...Finally[^] blocks - standard practice when working with a database. I shudder to think what the rest of this code looks like... Good luck.

            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