Click here to Skip to main content
15,885,842 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi
I am creating Insert,Update,Delete and Select in Xamarin forms.program are working fine but when i run two or three times so it is throwing exception error.
1 i have created Helper Class for writing insert update and delete methods
2 inside the APP.cs class i have create DB property
3 on the button click event i have created validation and apply insert and update methods. i am sending these methods blew

4 after that this Exception is coming every time.
System.Reflection.TargetInvocationException: 'Exception has been thrown by the target of an invocation.'




What I have tried:

Helper Class
public class MTNRCHelper
   {
       public readonly SQLiteAsyncConnection db;
       public MTNRCHelper(string localDb)
       {
           db = new SQLiteAsyncConnection(localDb);
           db.CreateTableAsync<Materials>();

         // db.DropTableAsync<Materials>();

           db.CreateTableAsync<ToolsDetailNewList>();
          // db.DropTableAsync<ToolsDetailNewList>();


       }

       public async Task<int> SaveMaterial(Materials obj)
        {
        return await db.InsertAsync(obj);
        }
       public async Task<List<Materials>> GetMaterialList()
       {
           return await db.Table<Materials>().ToListAsync();
        }
       public async Task<int> UpdateMaterial(Materials obj)
       {
           return await db.UpdateAsync(obj);
       }
       public async Task<int> DeleteMaterial(Materials obj)
       {
           return await db.DeleteAsync(obj);
       }

       //Method For Tools
       public async Task<int> SaveTools(ToolsDetailNewList obj)
       {
           return await db.InsertAsync(obj);


       }
       public async Task<List<ToolsDetailNewList>> GetToolsList()
       {

           return await db.Table<ToolsDetailNewList>().ToListAsync();

       }
       public async Task<int> UpdateTools(ToolsDetailNewList obj)
       {
           return await db.UpdateAsync(obj);
       }
       public async Task<int> DeleteTools(ToolsDetailNewList obj)
       {
           return await db.DeleteAsync(obj);
       }



   }





App.Cs Class

private static  MTNRCHelper db;
		public static  MTNRCHelper MyDababase
		{
			get
			{
				if (db == null)
				{
					db = new MTNRCHelper(Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "LocalDb.db3"));
				}
				return db;
			
			}
		}



On the Page Click event
private void btnsave_Clicked(object sender, EventArgs e)
{


        if (string.IsNullOrEmpty(txtNPD_ALTERNATE_OFFPART_NO.Text) ||
            string.IsNullOrWhiteSpace(txtNPD_ALTERNATE_OFFPART_NO.Text)||
            string.IsNullOrEmpty(txtOffSerialNumber.Text) ||
            string.IsNullOrWhiteSpace(txtOffSerialNumber.Text) ||
            string.IsNullOrEmpty(txtonPart.Text) ||
            string.IsNullOrWhiteSpace(txtonPart.Text) ||
            string.IsNullOrEmpty(txtOnPArtNumbers.Text) ||
            string.IsNullOrWhiteSpace(txtOnPArtNumbers.Text) ||
            string.IsNullOrEmpty(txtPartType.Text) ||
            string.IsNullOrWhiteSpace(txtPartType.Text) ||
            string.IsNullOrEmpty(txtNPD_ALTERNATE_PART_NO.Text) ||
             string.IsNullOrWhiteSpace(txtNPD_ALTERNATE_PART_NO.Text) ||
            string.IsNullOrEmpty(txtNPD_OFF_S_NO.Text) ||
             string.IsNullOrWhiteSpace(txtNPD_OFF_S_NO.Text) ||
             string.IsNullOrEmpty(txtdesc.Text) ||
             string.IsNullOrWhiteSpace(txtdesc.Text) ||
            string.IsNullOrEmpty(txtNPD_IPC_REFERENCE.Text) ||
             string.IsNullOrWhiteSpace(txtNPD_IPC_REFERENCE.Text) ||
            string.IsNullOrEmpty(NPDStatusPicker.SelectedItem.ToString()) ||
            string.IsNullOrWhiteSpace(NPDStatusPicker.SelectedItem.ToString()) ||
            string.IsNullOrEmpty(txtEnterPOS.Text) ||
             string.IsNullOrWhiteSpace(txtEnterPOS.Text) ||
            string.IsNullOrEmpty(txtNPD_REQ_QTY.Text) ||
            string.IsNullOrWhiteSpace(txtNPD_REQ_QTY.Text) ||
            string.IsNullOrEmpty(txtNPD_UOM.Text) ||
            string.IsNullOrWhiteSpace(txtNPD_UOM.Text) ||
            string.IsNullOrEmpty(txtNDP_Updated_By.Text) ||
            string.IsNullOrWhiteSpace(txtNDP_Updated_By.Text) ||
            string.IsNullOrEmpty(txtNPD_Inward.Text)||
            string.IsNullOrWhiteSpace(txtNPD_Inward.Text)
            )




        {
            DisplayAlert("message", "Please Fill the Fields", "OK");
        }

        else if (objmaterial != null)
        {
            EditMaterial();
        }
        else
        {
            AddMaterialData();
        }


}

private async void EditMaterial()
{
   // objmaterial.NPD_ALTERNATE_PART_NO = txtNPD_ALTERNATE_PART_NO.Text;

   await App.MyDababase.UpdateMaterial(objmaterial);
  // await Navigation.PushAsync(new NRCMeterialList(objmaterial));
}

private  async void AddMaterialData()
{


   await App.MyDababase.SaveMaterial(new Materials
    {
        PART_NO = txtNPD_ALTERNATE_OFFPART_NO.Text,
        NPD_ON_S_NO = txtOffSerialNumber.Text,
        NPD_ALTERNATE_PART_NO = txtonPart.Text,
        NPD_OFF_PART_NO = txtOnPArtNumbers.Text,
        NPD_REQ_ON_PART_NO = txtOnPArtNumbers.Text,
        NPD_Part_Type = txtPartType.Text,
        NPD_OFF_S_NO = txtNPD_OFF_S_NO.Text,

        NPD_DESC = txtdesc.Text,
        NPD_STATUS = NPDStatusPicker.SelectedItem.ToString(),
        NPD_IPC_REFERENCE = txtNPD_IPC_REFERENCE.Text,
        NPD_POS = txtEnterPOS.Text,
        NPD_REQ_QTY = txtNPD_REQ_QTY.Text,
        NPD_UOM = txtNPD_UOM.Text,
        NDP_Updated_By = txtNDP_Updated_By.Text,
        NPD_Inward = txtNPD_Inward.Text
    });

       await Navigation.PushAsync(new NRCMeterialList());
}
Posted
Updated 19-Feb-23 22:21pm

1 solution

We can't help - we have no access to the whole of your code and any data it is using while it runs, and no idea what exactly you do to duplicate the problem.

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900