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: - <script type="text/c#" language="C#" runat="server">
-
OleDbConnection objConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=D:\\ASPX\\" + "data-x.mdb");
-
OleDbCommand objCmd;
-
OleDbDataReader objRdr;
-
-
void Page_Load() {
-
if (!IsPostBack) {
-
objConn.Open();
-
-
objCmd = new OleDbCommand("SELECT * FROM HelpDeskCategories", objConn);
-
objRdr = objCmd.ExecuteReader();
-
ddlCategory.DataSource = objRdr;
-
ddlCategory.DataValueField = "CategoryID";
-
ddlCategory.DataTextField = "Category";
-
ddlCategory.DataBind();
-
objRdr.Close();
-
objCmd = new OleDbCommand("SELECT * FROM HelpDeskSubjects", objConn);
-
objRdr = objCmd.ExecuteReader();
-
ddlSubject.DataSource = objRdr;
-
ddlSubject.DataValueField = "SubjectID";
-
ddlSubject.DataTextField = "Subject";
-
ddlSubject.DataBind();
-
objRdr.Close();
-
-
objConn.Close();
-
}
-
}
-
void SubmitHelpDesk(Object s, EventArgs e) {
-
-
if (Page.IsValid) {
-
-
objCmd = new OleDbCommand(
-
"INSERT INTO HelpDesk (EmployeeID, StationNumber, " +
-
"CategoryID, SubjectID, Description, StatusID) " +
-
"VALUES (@EmployeeID, @StationNumber, @CategoryID, " +
-
"@SubjectID, @Description, @StatusID)", objConn);
-
objCmd.Parameters.Add("@EmployeeID", 5);
-
objCmd.Parameters.Add("@StationNumber", txtStationNum.Text);
-
objCmd.Parameters.Add("@CategoryID",
-
ddlCategory.SelectedItem.Value);
-
objCmd.Parameters.Add("@SubjectID",
-
ddlSubject.SelectedItem.Value);
-
objCmd.Parameters.Add("@Description", txtDescription.Text);
-
objCmd.Parameters.Add("@StatusID", 1);
-
objConn.Open();
-
objCmd.ExecuteNonQuery();
-
objConn.Close();
-
Response.Redirect("helpdesk.aspx");
-
}
-
}
-
</script>
Hope to hear from the expert, thanks. -tj
3 1579
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#.
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.
thanks guys, I was a relationship error, I set it mistakenly in MS Access. ;-)
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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.
|
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...
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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,...
|
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,...
| |
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...
|
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...
|
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...
|
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();...
|
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...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |