My problem occurs when importing a new recipe from a text file. I create the reipe first and call the adapter update for the recipes table and then refill it. Then I create an entry in the recipe_ingredients table which I also update and try to refill. However when I then call the recipe_ingredientsAdapter.Fill I get a "Object reference not set to an instance of an object" error which suggests a null value somewhere.
I have checked that all the data is valid. Also if I use Access to open the modified database file after the updates it is perfectly happy - so is my own programme if I re-run it using the modified database.
Any ideas where the error message may be coming from? Attached are code snippets from the relevant routines.
Thanks
Expand|Select|Wrap|Line Numbers
- private int ImportRecipe(string rcp)
- {
- {
- ....... Some text parsing of string rcp to extract the data values
- and create an ArrayList (AllIngs) of Ingredient strings
- }
- // Create a new recipe row
- recipesDataSet.RecipesDataTable table = (recipesDataSet.RecipesDataTable)recipesDataSet.Tables["Recipes"];
- recipesDataSet.RecipesRow row = table.AddRecipesRow(title
- ,foodrow
- ,veggy
- ,Preptime
- ,nserve
- ,ncal
- ,nutrition
- ,instructions
- ,notes
- ,bookrow
- ,wheat
- ,dairy
- ,utensils
- ,page);
- recipesTableAdapter.Update(table);
- recipesTableAdapter.Fill(recipesDataSet.Recipes);
- // Now we have a new recipe id we can update other related table
- int newid = (int)table.Rows[table.Count - 1]["RecipeID"];
- UpdateRecipeIngredientsTable(ref AllIngs, newid);
- return newid;
- }
- private void UpdateRecipeIngredientsTable(ref ArrayList list, int recid)
- {
- recipesDataSet.Recipe_IngredientsDataTable table = (recipesDataSet.Recipe_IngredientsDataTable)recipesDataSet.Tables["Recipe Ingredients"];
- int id = 0;
- foreach (Ingredient ing in list) {
- recipesDataSet.Recipe_IngredientsRow drow = (recipesDataSet.Recipe_IngredientsRow)table.NewRow();
- drow["RecipeID"] = recid;
- drow["IngredientID"] = ing.ingID;
- drow["Quantity"] = ing.quantity.ToString();
- drow["UnitID"] = ing.unitID;
- drow["Order"] = ++id;
- table.AddRecipe_IngredientsRow(drow);
- }
- recipe_IngredientsTableAdapter.Update(table);
- try {
- recipe_IngredientsTableAdapter.Fill(recipesDataSet.Recipe_Ingredients);
- }
- catch (Exception ex) {
- MessageBox.Show(ex.Message);
- }
- }