473,406 Members | 2,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Inserting Record to MS Access

tjc0ol
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
3 1550
IanWright
179 100+
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
8,127 Expert 4TB
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
26
thanks guys, I was a relationship error, I set it mistakenly in MS Access. ;-)
Jul 15 '08 #4

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

Similar topics

3
by: Nick | last post by:
Is there a way to reference the 'primary key' field of a record you are actually inserting at the moment. Im using an 'int auto increment' field as the primary key. I could reference it with a...
3
by: James Alba | last post by:
Hey all, I am accessing an ms access database using .NET and C#. Like so, /* Create the database connection. */ connection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data...
0
by: hzgt9b | last post by:
Using VS2003, VB.NET, Here's some pseudo code that I'm trying to get to work... if <a specific record existsthen do nothing else <insert the specific record> Endif I've got the code...
3
by: Surya | last post by:
Dear All, I have problem on inserting a record to database..Although it looked easy.. i have caught up with following issue .. please go ahead and help me to find solution I Need to insert...
20
by: dav3 | last post by:
Alright folks I am in need of a lil guidance/assistance here. I have a program which reads in a txt file. This txt file contains lines of the form January 3, 2007, 85.8 Now each line of the txt...
0
by: freeskier | last post by:
Hello, Sorry for the newb question; I've spent a good amount of time trying to figure this out. I am fairly new to the Postgre world. I am currently in the process of upsizing several MS Access...
10
by: Clamato | last post by:
Good Morning, I'm working with a form that basically add's a users windows logon ID, first name, and last name to a table. A list box on this form is then requeried once added displaying the...
2
by: hakkatil | last post by:
Hi to all, I have a page that inserts excel sheet to access database. I am using asp. What I want to do is to check the inserting record if it is in the database. Basicly checking the dublicate...
5
by: rando1000 | last post by:
Okay, here's my situation. I need to loop through a file, inserting records based on a number field (in order) and if the character in a certain field = "##", I need to insert a blank record. ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.