By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,837 Members | 1,766 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,837 IT Pros & Developers. It's quick & easy.

Inserting Record to MS Access

tjc0ol
P: 26
Hi guys,
Im a newbie in .NET, I follow the book in SitePoint which is Building your own ASP.NET Website using C# but I'm having trouble in inserting new data to MS ACCESS. When I run it, I've got an error i.e. You cannot add or change a record because a related record is required in table 'HelpDeskCategories'.
How to correct this error and How to insert new record to MS Access without experiencing error?

Here is my code:
Expand|Select|Wrap|Line Numbers
  1. <script type="text/c#" language="C#" runat="server">
  2. OleDbConnection objConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=D:\\ASPX\\" + "data-x.mdb");
  3. OleDbCommand objCmd;
  4. OleDbDataReader objRdr;
  5.  
  6. void Page_Load() {
  7. if (!IsPostBack) {
  8. objConn.Open();
  9.  
  10. objCmd = new OleDbCommand("SELECT * FROM HelpDeskCategories", objConn);
  11. objRdr = objCmd.ExecuteReader();
  12. ddlCategory.DataSource = objRdr;
  13. ddlCategory.DataValueField = "CategoryID";
  14. ddlCategory.DataTextField = "Category";
  15. ddlCategory.DataBind();
  16. objRdr.Close();
  17. objCmd = new OleDbCommand("SELECT * FROM HelpDeskSubjects", objConn);
  18. objRdr = objCmd.ExecuteReader();
  19. ddlSubject.DataSource = objRdr;
  20. ddlSubject.DataValueField = "SubjectID";
  21. ddlSubject.DataTextField = "Subject";
  22. ddlSubject.DataBind();
  23. objRdr.Close();
  24.  
  25. objConn.Close();
  26. }
  27. }
  28. void SubmitHelpDesk(Object s, EventArgs e) {
  29.  
  30. if (Page.IsValid) {
  31.  
  32. objCmd = new OleDbCommand(
  33. "INSERT INTO HelpDesk (EmployeeID, StationNumber, " +
  34. "CategoryID, SubjectID, Description, StatusID) " +
  35. "VALUES (@EmployeeID, @StationNumber, @CategoryID, " +
  36. "@SubjectID, @Description, @StatusID)", objConn);
  37. objCmd.Parameters.Add("@EmployeeID", 5);
  38. objCmd.Parameters.Add("@StationNumber", txtStationNum.Text);
  39. objCmd.Parameters.Add("@CategoryID",
  40. ddlCategory.SelectedItem.Value);
  41. objCmd.Parameters.Add("@SubjectID",
  42. ddlSubject.SelectedItem.Value);
  43. objCmd.Parameters.Add("@Description", txtDescription.Text);
  44. objCmd.Parameters.Add("@StatusID", 1);
  45. objConn.Open();
  46. objCmd.ExecuteNonQuery();
  47. objConn.Close();
  48. Response.Redirect("helpdesk.aspx");
  49. }
  50. }
  51. </script>
Hope to hear from the expert, thanks. -tj
Jul 14 '08 #1
Share this Question
Share on Google+
3 Replies


100+
P: 179
Sounds like you have a relationship between the table you are trying to modify and HelpDeskCategories which isn't being satisfied. Find the field that is linked to the other table, and ensure that you provide a value that is contained wtihin the HelpDeskCategories table, or if its not contained, create it beforehand...

You could probably practise just using the MSAccess SQL rather than having to do it through code as well, as this error doesn't lie within C#.
Jul 14 '08 #2

debasisdas
Expert 5K+
P: 8,127
To insert a record into the child table ,you need to insert the parent record to the parent table to follow the rule of referential integrity.
Jul 14 '08 #3

tjc0ol
P: 26
thanks guys, I was a relationship error, I set it mistakenly in MS Access. ;-)
Jul 15 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.