473,382 Members | 1,271 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,382 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 1548
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. ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.