View Single Post
  #1 (permalink)  
Old Mar 19th, 2008, 15:16
BrianLJMU BrianLJMU is offline
New Member
Join Date: Mar 2008
Location: Liverpool, UK
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
IINSERT INTO using ASP and MS Access

I was wondering if somebody could help me with a small problem i have, i am connecting an ASP website to an Access Database. (The coding is in C#)

I am very inexperienced in ASP and i can't find a solution in the various threads.
I have a registration page that requires data to be stored in two seperate tables (CustomerDetails and CustomerEmail)

The error message i get on my page is: You cannot add or change a record because a related record is required in table 'CustomerDetails'.

I need to update the customer details table, then the customer email table.

Appreciate any help

Regards Brian

Here is my code:

Code: Select all
Using  System;
using System.Web;
using System.Data;
using System.Data.OleDb;
public partial class _Register : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
// Click the button to register with website
protected void Button1_Click(object sender, EventArgs e)
{
//Establish the connection with the database
OleDbConnection myConnection = new OleDbConnection();
myConnection.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath("~") + @"\App_Data\websitedesign.mdb";
//New connection
OleDbCommand myCommand = new OleDbCommand();
myCommand.Connection = myConnection;
//Insert data into the customer details table
myCommand.CommandText = "INSERT INTO CustomerDetails([FirstName], [Surname], [HouseNumber], [RoadName], [Town], [City], [Postcode]) VALUES (@FirstName, @Surname, @HouseNumber, @RoadName, @Town, @City, @Postcode)";
myCommand.Parameters.Add("@FirstName", OleDbType.Char).Value = txtFirstName.Text;
myCommand.Parameters.Add("@Surname", OleDbType.Char).Value = txtSurname.Text;
myCommand.Parameters.Add("@HouseNumber", OleDbType.Char).Value = txtHouseNumber.Text;
myCommand.Parameters.Add("@RoadName", OleDbType.Char).Value = txtRoadName.Text;
myCommand.Parameters.Add("@Town", OleDbType.Char).Value = txtTown.Text;
myCommand.Parameters.Add("@City", OleDbType.Char).Value = txtCity.Text;
myCommand.Parameters.Add("@Postcode", OleDbType.Char).Value = txtPostcode.Text;
//Insert data into the customer email table
myCommand.CommandText = "INSERT INTO CustomerEmail([EmailAddress], [Password], [SecretQuestion], [SecretAnswer]) VALUES (@EmailAddress, @Password, @SecretQuestion, @SecretAnswer)";
myCommand.Parameters.Add("@EmailAddress", OleDbType.Char).Value = txtEmail.Text;
myCommand.Parameters.Add("@Password", OleDbType.Char).Value = txtPassword.Text;
myCommand.Parameters.Add("@SecretQuestion", OleDbType.Char).Value = txtQuestion.Text;
myCommand.Parameters.Add("@SecretAnswer", OleDbType.Char).Value = txtAnswer.Text;
myConnection.Open();

try
{
//Execuste the query, display message if successful
myCommand.ExecuteNonQuery();
Label1.Text = "Record Inserted";
}
catch (Exception ex)
{
//Displays an error message if unsuccessful
Label1.Text = ex.Message;
}
finally
{
myConnection.Close();
}
}
}


Last edited by saltedm8; Mar 21st, 2008 at 19:20. Reason: added [code] tags
Reply With Quote