dcsimg
Results 1 to 8 of 8

Thread: Expression expected error

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2017
    Posts
    19

    Expression expected error

    Hi,

    I have a source code giving "expression expected" error. A small piece of this code below and whole source code is at the attachment. How can I fix this error? Thanks everybody.

    -------------------------
    Private Sub ConnectToSQL()

    Dim con As New SqlConnection
    Dim cmd As New SqlCommand
    Dim Password As String
    Dim Password2 As String
    Dim userName As String

    Try
    If (EXPRESSION EXPECTED)
    'change the data source and initial catalog according to your sql server engine and data base
    con.ConnectionString = "Data Source = Localhost; Initial Catalog = hdur750; Integrated Security = True"

    -----------------------------------------
    Attached Files Attached Files

  2. #2
    Frenzied Member PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Newport, UK
    Posts
    1,057

    Re: Expression expected error

    An If statement needs an expression that evaluates to true or false an If without any condition is meaningless.

    Could you post the relevant section of code here please, show the If statement in full if you have to - most people (myself included) would rather not download a word document from an unknown source just to read some source code.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    May 2017
    Posts
    19

    Re: Expression expected error

    Thank you for your answer. Here is the code. This is a login code in Visual Basic and SQL database


    Code:
    Imports System.Data
    Imports System.Data.SqlClient
    Public Class giris
    
        Private Sub giris_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    
        End Sub
    
        'Login Button
        Private Sub btnlogin_Click(sender As System.Object, e As System.EventArgs) Handles btnLogin.Click
    
            ConnectToSQL()
    
        End Sub
    
        'Connect to SQL method
    
        Private Sub ConnectToSQL()
    
            Dim con As New SqlConnection
            Dim cmd As New SqlCommand
            Dim Password As String
            Dim Password2 As String
            Dim userName As String
    
            Try
                If        (EXPRESSION EXPECTED)
                        'change the data source and initial catalog according to your sql server engine and data base
                    con.ConnectionString = "Data Source = Localhost; Initial Catalog = hdur750; Integrated Security = True"
                    con.Open()
    
                    cmd.Connection = con
                    'change the data fields names and table according to your database
                    cmd.CommandText = " SELECT  userid, Password FROM   login WHERE   (userid = '" & txtKullanici.Text & "' ) AND (Password = '" & txtSifre.Text & "')"
    
                    Dim lrd As SqlDataReader = cmd.ExecuteReader()
                    If lrd.HasRows Then
                        While lrd.Read()
    
                            'Do something here
                            Password = lrd("password").ToString()
                            userName = lrd("userid").ToString()
    
                            Password2 = txtKullanici.Text()
    
                            If Password = Password2 And userName = txtKullanici.Text Then
    
                                MessageBox.Show("Logged in successfully as " & userName, "", MessageBoxButtons.OK, MessageBoxIcon.Information
                                            )
                                anasayfa.Show()
                                Me.Hide()
    
                                'Clear all fields
                                txtKullanici.Text = ""
                                txtSifre.Text = ""
    
                            End If
    
                        End While
    
                    Else
                        MessageBox.Show("Username and Password do not match..", "Authentication Failure", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                        'Clear all fields
                        txtSifre.Text = ""
                        txtKullanici.Text = ""
                    End If
    
                End If
    
            Catch ex As Exception
                MessageBox.Show("Error while connecting to SQL Server." & ex.Message)
    
            Finally
                con.Close() 'Whether there is error or not. Close the connection.
    
            End Try
    
        End Sub
    
        Private Sub txtKullanici_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtKullanici.TextChanged
    
        End Sub
    
        Private Sub txtSifre_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtSifre.TextChanged
    
        End Sub
    
        Private Sub btnClear_Click(sender As System.Object, e As System.EventArgs) Handles btnClear.Click
            'User clicking on cancel button only clears field
            ' and refocus to first field
    
            txtKullanici.Text = ""
            txtSifre.Text = ""
            txtKullanici.Focus()
        End Sub
    
    End Class

  4. #4
    Frenzied Member PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Newport, UK
    Posts
    1,057

    Re: Expression expected error

    An If statement is used to make a decision about what code to execute based on checking a condition, if the condition is true then you execute the code, if the condition is false then you would execute the code in the "else" part (if one exists).

    You need to decide what you are trying to do here, under what conditions should you be running the code that follows the If statement? Once you have decided on this bit then writing the code to check if it is met should be easy.

    If there is no decision to make then you don't need an If statement.

    You have a working example of an If further down in the code you posted "If lrd.HasRows Then" and again with "If Password = Password2 And userName = txtKullanici.Text Then"

  5. #5

    Thread Starter
    Junior Member
    Join Date
    May 2017
    Posts
    19

    Re: Expression expected error

    I copied this code from Internet and changed the name of TextBox, Label, Button, Database ..etc. according to my Project.
    In original source code there is not any expression after "If" where the mistake happens. So I couldn't find any solution here.


    Thanks for your interest a lot.

  6. #6
    Frenzied Member PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Newport, UK
    Posts
    1,057

    Re: Expression expected error

    If there is nothing needed then just delete the If and the matching End If and you should be fine.

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,020

    Re: Expression expected error

    As you don't know what it would be used for, it would be best to remove that "If" line, and the matching "End If" (just before the Catch).


    edit: I was too slow!

  8. #8
    Hyperactive Member Mike Storm's Avatar
    Join Date
    Jun 2017
    Location
    Belgium
    Posts
    425

    Re: Expression expected error

    Hi...
    Well the first use of a IF in there i can think of is to check if the Textbox's controls are empty or not, so here is a sample code:

    Code:
    Private Sub ConnectToSQL()
    
            Dim con As New SqlConnection
            Dim cmd As New SqlCommand
            Dim Password As String
            Dim Password2 As String
            Dim userName As String
    
            Try
                If txtKullanici.Text <> "" And txtSifre.text <> "" Then
    
                    'change the data source and initial catalog according to your sql server engine and data base
                    con.ConnectionString = "Data Source = Localhost; Initial Catalog = hdur750; Integrated Security = True"
                    con.Open()
    
                    cmd.Connection = con
                    'change the data fields names and table according to your database
                    cmd.CommandText = " SELECT  userid, Password FROM   login WHERE   (userid = '" & txtKullanici.Text & "' ) AND (Password = '" & txtSifre.Text & "')"
    
                    Dim lrd As SqlDataReader = cmd.ExecuteReader()
                    If lrd.HasRows Then
                        While lrd.Read()
    
                            'Do something here
                            Password = lrd("password").ToString()
                            userName = lrd("userid").ToString()
    
                            Password2 = txtKullanici.Text()
    
                            If Password = Password2 And userName = txtKullanici.Text Then
    
                                MessageBox.Show("Logged in successfully as " & userName, "", MessageBoxButtons.OK, MessageBoxIcon.Information
                                            )
                                anasayfa.Show()
                                Me.Hide()
    
                                'Clear all fields
                                txtKullanici.Text = ""
                                txtSifre.Text = ""
    
                            End If
    
                        End While
    
                    Else
                        MessageBox.Show("Username and Password do not match..", "Authentication Failure", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                        'Clear all fields
                        txtSifre.Text = ""
                        txtKullanici.Text = ""
                    End If
    
                End If
    
            Catch ex As Exception
                MessageBox.Show("Error while connecting to SQL Server." & ex.Message)
    
            Finally
                con.Close() 'Whether there is error or not. Close the connection.
    
            End Try
    
        End Sub
    But as refered in the upper coments, if you using a "IF" clause its to validate a condition TRUE, FALSE, EQUAL, Superior, Inferior, Diferent, etc.... so when you looking for help especify what do you want to do.

    https://docs.microsoft.com/en-us/dot...else-statement


    PS: You can also be wanting to check if the database exists, if SQL server is there, explain what you want to do maybe people will be able to help.
    Last edited by Mike Storm; Jun 16th, 2017 at 07:09 AM.

Tags for this Thread

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width