473,953 Members | 1,845 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Inserting Record to MS Access

tjc0ol
26 New Member
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 'HelpDeskCatego ries'.
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 1579
IanWright
179 New Member
Sounds like you have a relationship between the table you are trying to modify and HelpDeskCategor ies 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 HelpDeskCategor ies 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 Recognized Expert Expert
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 New Member
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
5087
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 select statement and adding a 1 to the last record's primary key. However, there must be a better way to do this - what if someone adds a record right after I reference the last one. I hope my question makes sense. Thanks! -Nick
3
16529
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 Source=" + Name); connection.Open(); And all is good.
0
1185
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 written, but Its not working for me...
3
3377
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 records to a data base,for which i am using Typed dataSet.(I used DataSet Designer to create one) Then created a table adapter in which i have written query for INSERT , & set Database Direct property to true.(I didnt use a seperate methods for...
20
3038
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 file is to be read into my java program and then inserted (from the program into an Access Database). I am not exactly sure of where the problem lies. I know it has something to do with how I am parsing the file, or how I am passing my arguments to...
0
2655
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 apps to a single PostgreSQL system; I will continue to use Access as the frontend. I have duplicated the table structure in Postgre and have successfully connected Access as a frontend to view data. I am having trouble inserting a record into...
10
1848
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 names in this table. I'm ultimately trying to make it so the user can't add their information twice in this table. Well, you would think just making their ID the PK in the table would work, however I'm running into an issue with this. The user...
2
3128
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 record. My code inserts records one by one using addnew-updatebatch. If there is a duplicate in the db, it will display "already exists" and if it is not in the db it will display "record added". Below is my asp code I found on the net and...
5
2183
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. So here's my method. I created two tables with the same structure as the table I'm inserting from. One table, Split_Temp, is the one I'll be inserting to. The other table, Split_Insert, contains the "Blank" record, which actually just has the word...
0
10004
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
11393
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10715
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9922
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8295
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7457
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6245
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4571
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3570
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.