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"
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.
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
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"
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.
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.