How to check if a row is present in SQL Server table or not?

NewbieProgrammer

I am trying to check if there is a row present in a SQL Server table or not.

If the row exists (on a particular TicketID), it should show a messagebox that you can't continue further as there is already an entry in database. But if there isn't, it should insert some records (on that particular TicketID).

I tried try and catch but wasn't able to do it :

Here is the code of query: (hardcoded ticketID for example)

bool no;

try
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ST"].ConnectionString.ToString());
    con.Open();
    cmd.CommandText = "SELECT EngineerVisited from tblTicketTechnical where TicketID=1";
    cmd.Connection = con;
    rdr = cmd.ExecuteReader();

    while (rdr.Read())
    {
        bool = rdr.GetBoolean(0);
    }

    con.Close();
}
catch
{
    MessageBox.Show("Cannot continue");
}

I would really appreciate if someone could suggest a function that will return true if row is found and return false, if it isn't.

NaveenBhat

You can use HasRows property of SQLDataReader.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to check if a column exists in a SQL Server table?

From Java

Check if table exists in SQL Server

From Dev

Sql Server row size limit and table design

From Dev

PIVOT table only in one row in SQL Server?

From Dev

How To Check Table variable is empty in SQL Server

From Dev

Invalid attempt to read data when no data is present when getting all values from a row in a SQL Server table

From Dev

How to check if a column is present in a table?

From Dev

SUM grouped table row value in SQL Server

From Dev

How to check for combination of column values in sql server table

From Dev

Check row existence in SQL Server with PowerShell

From Dev

How to check if any value is Null in a table single row SQL

From Dev

SQL Server import row string to table columns

From Dev

SQL Server 2014 Select Records not present in other table with Max function

From Dev

Sql Server row size limit and table design

From Dev

check duplicate records for particular row in sql server

From Dev

how to check column data is not there in table sql server

From Dev

How to append a row present inside a nested table

From Dev

How to check if row already exists in log table from inside After Delete trigger in SQL Server 2008?

From Dev

How to get elements from table by row number in sql server

From Dev

Check row existence in SQL Server with PowerShell

From Dev

How to check if any value is Null in a table single row SQL

From Dev

How to sum a specific row from one table to another in SQL Server

From Dev

SQL Server 2014 Select Records not present in other table with Max function

From Dev

How to print a table row wise in SQL Server?

From Dev

How to retrieve data which is not present in any field of one table in SQL Server?

From Dev

SQL - for each entry in a table - check for associated row

From Dev

How to add row to a SQL Server table with uniqueidentifier type column?

From Dev

How can i select row from table according to column values of row in sql server

From Dev

How to check if variable is present in a table? TCL

Related Related

  1. 1

    How to check if a column exists in a SQL Server table?

  2. 2

    Check if table exists in SQL Server

  3. 3

    Sql Server row size limit and table design

  4. 4

    PIVOT table only in one row in SQL Server?

  5. 5

    How To Check Table variable is empty in SQL Server

  6. 6

    Invalid attempt to read data when no data is present when getting all values from a row in a SQL Server table

  7. 7

    How to check if a column is present in a table?

  8. 8

    SUM grouped table row value in SQL Server

  9. 9

    How to check for combination of column values in sql server table

  10. 10

    Check row existence in SQL Server with PowerShell

  11. 11

    How to check if any value is Null in a table single row SQL

  12. 12

    SQL Server import row string to table columns

  13. 13

    SQL Server 2014 Select Records not present in other table with Max function

  14. 14

    Sql Server row size limit and table design

  15. 15

    check duplicate records for particular row in sql server

  16. 16

    how to check column data is not there in table sql server

  17. 17

    How to append a row present inside a nested table

  18. 18

    How to check if row already exists in log table from inside After Delete trigger in SQL Server 2008?

  19. 19

    How to get elements from table by row number in sql server

  20. 20

    Check row existence in SQL Server with PowerShell

  21. 21

    How to check if any value is Null in a table single row SQL

  22. 22

    How to sum a specific row from one table to another in SQL Server

  23. 23

    SQL Server 2014 Select Records not present in other table with Max function

  24. 24

    How to print a table row wise in SQL Server?

  25. 25

    How to retrieve data which is not present in any field of one table in SQL Server?

  26. 26

    SQL - for each entry in a table - check for associated row

  27. 27

    How to add row to a SQL Server table with uniqueidentifier type column?

  28. 28

    How can i select row from table according to column values of row in sql server

  29. 29

    How to check if variable is present in a table? TCL

HotTag

Archive