Hi gang ;
The code below works fine but a more flexible one would be better.
For example what am looking for an original try-throw-cath block which throws the exception "Item not found" message itself.

      try { 
  SQLiteConnection ^connection = gcnew SQLiteConnection();            
  connection->ConnectionString = "Data Source = atbdn3.db3";
  connection->Open(); 
  System::String ^selecteditem = (textBox1->Text)->ToString();
  SQLiteCommand^ command = connection->CreateCommand();  
  command->CommandText ="select energy from table where product ='"+selecteditem +"'" ;     
    SQLiteDataReader ^dataReader = command->ExecuteReader();

    if (dataReader->HasRows == true)
    {
           while (dataReader->Read())
           {
              label1->Text = dataReader["energy_kcal"]->ToString(); 
           }
     }

    else
    {
    MessageBox::Show("ITEM NOT FOUND.");  //  SQLite error imitation !

    }

    connection->Close();

     }

    catch(Exception ^e )  {

     MessageBox::Show(e->Message) ;  // SQLite error : no such table

 }

Recommended Answers

All 3 Replies

Member Avatar for nileshgr

Please post your environment, which driver you are using, etc.
Also format the code properly. Its very tough to say like this. use the code tags so that smileys and all don't come into it :)

SqLite doesn't throw exceptions, it just returns errors. You will have to change SqLite source code to make it throw exceptions like what you want. You have all the source code so you can change it any way you like.

The issue is when user enters xzzjm which is not exist on the table and also
which is not exist in user options both the query executes RETURNS LAST KNOWN RESULTS and "such a key not exist", ITEM NOT FOUND messages displays.

if (a||b||c||d||e)
{
    if(a){textBox1.text = "a" AND connect_db() TO fill dataset with key a }
    if(b){textBox1.text = "b" AND connect_db() TO fill dataset with key b }
   
connect_db( textBox1.text = "c-d-e doesnt make sense here" connect_db())
}
else  // if useer enters xzzjm
{
MessageBox.Show("such a key not exist!") 
}

// the below is db_connection function declaration only

void connect_db() {
         try {	
  SQLiteConnection connection = gcnew SQLiteConnection();			  
  connection.ConnectionString = "Data Source = atbdn3.db3";
  connection.Open(); 
  string selecteditem = (textBox1.Text).ToString();
  SQLiteCommand command = connection.CreateCommand();  
  command.CommandText ="select energy from table where product ='"+selecteditem +"'" ;     
	SQLiteDataReader dataReader = command.ExecuteReader();
             	
	if (dataReader.HasRows == true)
	{
	       while (dataReader.Read())
	       {
	          label1.Text = dataReader["energy_kcal"].ToString(); 
	       }
	 }

	else
	{
	MessageBox.Show("ITEM NOT FOUND.");  //  SQLite error i-m-i-t-a-t-i-o-n !!! 
				 
	}

	connection.Close();

	 }

	catch(Exception e )  {
				 
	 MessageBox.Show(e.Message) ;  // SQLite error : no such table
	                      }					
 }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.