Inserting Record to MS Access

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tjc0ol
    New Member
    • Nov 2007
    • 26

    Inserting Record to MS Access

    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:
    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
  • IanWright
    New Member
    • Jan 2008
    • 179

    #2
    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#.

    Comment

    • debasisdas
      Recognized Expert Expert
      • Dec 2006
      • 8119

      #3
      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.

      Comment

      • tjc0ol
        New Member
        • Nov 2007
        • 26

        #4
        thanks guys, I was a relationship error, I set it mistakenly in MS Access. ;-)

        Comment

        Working...