View Single Post
  #12 (permalink)  
Old Mar 21st, 2008, 11:37
BrianLJMU BrianLJMU is offline
New Member
Join Date: Mar 2008
Location: Liverpool, UK
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Re: IINSERT INTO using ASP and MS Access

Thanks for your help Monie,

i slightly tweaked the code, but the fundamental error was in the foriegn key field. the default was set to 0 - i have removed this for now as i can insert data into a table. I can fix that another time.

My final code for inserting into two tables (but unable to update foriegn key - If anyone can help, it will be much apprieciated)

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)
{
}
// Once user has clicked the submit button the data is sent to the Database
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";
myConnection.Open();

{//New connection to insert data into the first table
OleDbCommand myCommand = new OleDbCommand();
myCommand.Connection = myConnection;

//SQL to 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;
//Execute the query
myCommand.ExecuteNonQuery();
//Close the above connection with the database
myConnection.Close();
}

{//New connection to insert data into second table
OleDbCommand myCommand = new OleDbCommand();
myCommand.Connection = myConnection;
//Open new connection
myConnection.Open();
//SQL to Insert data into the customer details 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;
myCommand.ExecuteNonQuery();
//If successful then a message is displayed on the website
Label1.Text = "Record Inserted";
//Close the above connection
myConnection.Close();
}
}
}
Reply With Quote