0

I have datatable tblEmployeeInfo and column EmployeeName with contains data.

EmployeeName
Mike              
Jay
Paul

Also I have Textbox1 to type a name and button to "Verify". How do I check if the Employee Name exists in the column EmployeeName?

For example, "Jay".

So far this is my code on button "Verify":

    conn.Open()
    cmd.Connection = conn
    cmd.CommandText = "SELECT [Employee Name] FROM tblHolidayOvertimeEntry WHERE [Employee Name] = '" & tbEmployeeName.Text & "' "
    Dim result1 as String = cmd.ExecuteNonQuery
    conn.Close()


    If result1 <> tbEmployeeName.Text Then

        MsgBox("No Employee Name found")
    Else

        MsgBox("Employee name still exist")

    End If
David
  • 2,298
  • 6
  • 22
  • 56
Jaron2016
  • 29
  • 1
  • 6
  • 1
    Gah! The sql injection vulnerability. It burns us! – Joel Coehoorn Mar 30 '17 at 01:49
  • 3
    Think about what you're doing. Why would you call `ExecuteNonQuery` to execute a query? If you've done any reading on ADO.NET, which you should have done before posting here, then you know that there are basically three ways to execute a query: call `Fill` on a data adapter, call `ExecuteReader` on a command and call `ExecuteScalar` on a command. Decide which is appropriate to your case and do it. – jmcilhinney Mar 30 '17 at 02:02
  • That has not been the right way to create SQL for a long time now. – Ňɏssa Pøngjǣrdenlarp Mar 30 '17 at 02:02
  • Sorry guyz it is my first time. Please help me. – Jaron2016 Mar 30 '17 at 02:10
  • 2
    I sure hope that Jay's full name isn't Jay O' Drop Table – David Mar 30 '17 at 10:16

1 Answers1

0

first of all clear concept about ExecuteScalar, ExecuteReader and ExecuteNonQuery from this link: When to use ExecuteScalar,ExecuteReader,ExecuteNonQuery?

and you have to set like

cmd.CommandText = "SELECT [Employee Name] FROM tblHolidayOvertimeEntry WHERE [Employee Name] = '" & tbEmployeeName.Text & "' "
Dim result1 as String = cmd.ExecuteScalar
Community
  • 1
  • 1
android
  • 2,942
  • 1
  • 15
  • 20