2

I am using Sqlite as a back End and Windows Form Application (C#) as a front end.

I am going through this code:

cmdgetTransaction_ID = new SQLiteCommand("SELECT MAX(transaction_id) as expr1  FROM  transaction_master WHERE transaction_id LIKE '"+temp+"%' ", con);
SQLiteDataReader reader = cmdgetTransaction_ID.ExecuteReader();
if (reader["expr1"]!=DBNull.Value)
{
     name= reader.GetString(0);
     string[] substrings = System.Text.RegularExpressions.Regex.Split(name, "([a-z]+)|([0-9]+)");
     MessageBox.Show(substrings[0]);
}
else
{
     name=name+temp+"1";
     lblTranID.Text = name;
}

I have also tried with this: if (reader.IsDBNull(0))

While debugging (step into) it reports following Exception :

A first chance exception of type 'System.InvalidOperationException' occurred in System.Data.SQLite.dll

I can not figure it out what mistake i am doing, so that it generates an exception.

Steve
  • 9,335
  • 10
  • 49
  • 81
user2130649
  • 89
  • 1
  • 5

1 Answers1

1

This should work for you:

if (reader != null && reader.HasRows) 
{
   while (reader.Read()) 
   {
      ...
   }
}
Nikhil Vartak
  • 5,002
  • 3
  • 26
  • 32
  • still the problem persist : Even though ,data reader does not contain any record the if condition becomes true ,it goes into while loop, as there is no data it raises an exception - I have attached Screen Shots – user2130649 Nov 04 '15 at 04:22
  • Does it raise exception when at `while` line or after that when you try to read `expr1` column? Also do not edit answers for posting own screen shots. Use comments instead. – Nikhil Vartak Nov 04 '15 at 05:23