• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 276
  • Last Modified:

Hating DW generated code...(Cont..3)

I haven't been getting many responses for my questions lately but i hope that i will get some help with this...

In the effort to eliminate everythinng Dreamweaver related i need help with one finaly piece of code. I have been doing my loging page useing dreamweaver and i need to convert this to VB code. I attempted doing this myself but i got a few errors...

Please help.....

OLD DW CODE:
---------------------------------------------------------------------------------------------------------------------------------------------
<MM:DataSet
id="DS_Login"
runat="Server"
IsStoredProcedure="false"
ConnectionString='<%# System.Configuration.ConfigurationSettings.AppSettings("MM_CONNECTION_STRING_Conn_Intranet") %>'
DatabaseType='<%# System.Configuration.ConfigurationSettings.AppSettings("MM_CONNECTION_DATABASETYPE_Conn_Intranet") %>'
CommandText='<%# "SELECT Fld_Password, Fld_Username  FROM Tbl_Users  WHERE Tbl_Users.Fld_Password  = @txtPassword  AND Tbl_Users.Fld_Username = @txtUserID" %>'
Debug="true"
><Parameters>
  <Parameter  Name="@txtUserID"  Value='<%# IIf((Request.Form("txtUserID") <> Nothing), Request.Form("txtUserID"), "xyz")       %>'  Type="VarChar"   />  
  <Parameter  Name="@txtPassword"  Value='<%# IIf((Request.Form("txtPassword") <> Nothing), Request.Form("txtPassword"), "xyz")       %>'  Type="VarChar"   />  
</Parameters></MM:DataSet>
<MM:PageBind runat="server" PostBackBind="true" />

<!-- --------------------------------------User Authentication--------------------------------------------------- -->
<script language="vb" runat="server">
      Sub Login_Click(Src As Object, E As EventArgs)
            IF DS_Login.Recordcount > 0 then
                  FormsAuthentication.RedirectFromLoginPage(txtUserID.Text, chkPersistLogin.Checked)
                     Session("Sess_Username") = Request("txtUserID")
            Else
                  ErrorMsg.InnerHtml  = "Login Failed! Please try again."
            End If
      End Sub
</script>
<!-- ------------------------------------------------------------------------------------------------------------ -->
----------------------------------------------------------------------------------------------------------------------------------------------------

MY ATTEMPT:
-----------------------------------------------------------------------------------------------------------------------------------------------------
<script language="vb" runat="server">
      Sub Login_Click(Src As Object, E As EventArgs)

      Dim oleConn As New OleDb.OleDbConnection(System.Configuration.ConfigurationSettings.AppSettings("MM_CONNECTION_STRING_Conn_Intranet"))
      Dim oleComm AS NEW OleDb.oleDbCommand()
            oleComm.Connection=oleConn
            
      dim da as New OleDb.OleDbDataAdapter()
      Dim ds as New DataSet()
            da.selectCommand=Olecomm
      
            'get dataset 1, table 1
            oleComm.CommandText="SELECT Fld_Password, Fld_Username  FROM Tbl_Users  WHERE Tbl_Users.Fld_Password  =" & Request.Form(txtPassword) & " AND Tbl_Users.Fld_Username =" & Request.Form(txtUserID) & ""
            da.Fill(ds,"DS_Login")

            oleConn.Close()
            
            
            
            IF DS_Login.Recordcount > 0 then
                  FormsAuthentication.RedirectFromLoginPage(txtUserID.Text, chkPersistLogin.Checked)
                     Session("Sess_Username") = Request("txtUserID")
            Else
                  ErrorMsg.InnerHtml  = "Login Failed! Please try again."
            End If
      End Sub
</script>
----------------------------------------------------------------------------------------------------------------------------------------------

I know there is allot of things wrong here. First off the 'RecordCount' is picking up the error... But i am sure that there is allot more wrong with this... can someone please help set me straight...

Nugs
0
Nugs
Asked:
Nugs
  • 3
  • 2
1 Solution
 
NugsAuthor Commented:
Ok how about this:

----------------------------------------------------------------------------------------------------------------------------------
<script language="vb" runat="server">
      Sub Login_Click(Src As Object, E As EventArgs)

      Dim oleConn As New OleDb.OleDbConnection(System.Configuration.ConfigurationSettings.AppSettings("MM_CONNECTION_STRING_Conn_Intranet"))
      Dim oleComm AS NEW OleDb.oleDbCommand()
            oleComm.Connection=oleConn
            
      dim da as New OleDb.OleDbDataAdapter()
      Dim ds as New DataSet()
            da.selectCommand=Olecomm
      Dim dtSource As DataTable
      Dim maxRec As Integer                  
      
            'get dataset 1, table 1
            oleComm.CommandText="SELECT Fld_Password, Fld_Username  FROM Tbl_Users  WHERE Tbl_Users.Fld_Password  =" & txtPassword.text & " AND Tbl_Users.Fld_Username =" & txtUserID.text & ""
            da.Fill(ds,"DS_Login")
            
            ' Set the source table.
            dtSource = ds.Tables("DS_Login")
            maxRec = dtSource.Rows.Count            

            oleConn.Close()

            IF maxRec > 0 then
                  FormsAuthentication.RedirectFromLoginPage(txtUserID.Text, chkPersistLogin.Checked)
                     Session("Sess_Username") = Request("txtUserID")
            Else
                  ErrorMsg.InnerHtml  = "Login Failed! Please try again."
            End If
      End Sub
</script>
------------------------------------------------------------------------------------------------------------------------------------

ERROR:
------------------------------------------------------------------------------------------------------------------------------------
Exception Details: System.Data.OleDb.OleDbException: Data type mismatch in criteria expression.

Source Error:

Line 22:             'get dataset 1, table 1
Line 23:             oleComm.CommandText="SELECT Fld_Password, Fld_Username  FROM Tbl_Users  WHERE Tbl_Users.Fld_Password  =" & txtPassword.text & " AND Tbl_Users.Fld_Username =" & txtUserID.text & ""
-->Line 24:             da.Fill(ds,"DS_Login")
Line 25:             
Line 26:             ' Set the source table.
------------------------------------------------------------------------------------------------------------------------------------

Closer ?????

Nugs
0
 
HavaganCommented:
Line 23:           oleComm.CommandText="SELECT Fld_Password, Fld_Username  FROM Tbl_Users  WHERE Tbl_Users.Fld_Password  =" & txtPassword.text & " AND Tbl_Users.Fld_Username =" & txtUserID.text & ""

*cough* You're not using the right SQL again. Put single quotes around your TextBox.Text values...

"SELECT Fld_Password, Fld_Username  FROM Tbl_Users " & _
"WHERE Tbl_Users.Fld_Password  = '" & txtPassword.text & "' AND Tbl_Users.Fld_Username = '" & txtUserID.text & "'"

Paul
0
 
HavaganCommented:
Nugs,

Take a look at using the String.Format method for making your SQL somewhat easier to read...

Dim sql As String = " SELECT Fld_Password, Fld_Username  FROM Tbl_Users  WHERE Tbl_Users.Fld_Password  = '{0}' AND Tbl_Users.Fld_Username = '{1}' "
String.Format(sql, txtPassword.Text, txtUserID.Text)

{0} is populated with txtPassword.Text
{1} is populated with txtUserID.Text

You can do this for any number of parameters in your string.

While I'm being nitpicky, it's also somewhat cleaner (and safer) to use Try ... Catch ... Finally blocks in your code, especially when dealing with objects which must be closed (like database connections). The code in the Finally blocks runs even if there are errors elsewhere -- otherwise you run the risk of leaving connections open, etc.

Try
     ...
     oleConn.Open
     ...
Catch ex As Exception
     'handle errors of certain types,
     'log for later research or just throw the error
     Throw
Finally
     If Not oleConn Is Nothing Then oleConn.Close
End Try

Paul
0
 
NugsAuthor Commented:
Thanks again Paul, The SQL was the problem......
0
 
HavaganCommented:
Any time. =)

Paul
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now