Web Design and Development Forums

IINSERT INTO using ASP and MS Access

This is a discussion on "IINSERT INTO using ASP and MS Access" within the ASP.NET Forum section. This forum, and the thread "IINSERT INTO using ASP and MS Access are both part of the Program Your Website category.


Go Back   Webforumz.com > Program Your Website > ASP.NET Forum

Welcome to Webforumz.com.
Register Now Register now!

Reply
 
LinkBack Thread Tools Rate Thread
Old Mar 19th, 2008, 15:16   #1 (permalink)
New Member
 
Join Date: Mar 2008
Location: Liverpool, UK
Posts: 7
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
BrianLJMU is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old Mar 19th, 2008, 15:26   #2 (permalink)
Most Reputable Member
 
Join Date: Feb 2004
Location: Borneo
Age: 27
Posts: 1,567
Blog Entries: 2
Send a message via Yahoo to Monie
Re: IINSERT INTO using ASP and MS Access

You seems executing two different sql with one execute command!
I don't know C# so I dont really understand what the code is doing

You should execute the 2 SQL that you have with 2 execute command, I guess
Cheers...
__________________

Monie is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old Mar 19th, 2008, 15:36   #3 (permalink)
New Member
 
Join Date: Mar 2008
Location: Liverpool, UK
Posts: 7
Re: IINSERT INTO using ASP and MS Access

Thank you for replying.

i think i know whats wrong know

Thanks
BrianLJMU is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old Mar 19th, 2008, 15:40   #4 (permalink)
Most Reputable Member
 
Join Date: Feb 2004
Location: Borneo
Age: 27
Posts: 1,567
Blog Entries: 2
Send a message via Yahoo to Monie
Re: IINSERT INTO using ASP and MS Access

Just make sure you post in the solution here so that people with the same problem can learn from your mistake
Cheers...
__________________

Monie is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old Mar 19th, 2008, 15:42   #5 (permalink)
New Member
 
Join Date: Mar 2008
Location: Liverpool, UK
Posts: 7
Re: IINSERT INTO using ASP and MS Access

Yes OK,

But thats if i manage to get the solution

regards,
Brian
BrianLJMU is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old Mar 19th, 2008, 16:03   #6 (permalink)
Most Reputable Member
 
Join Date: Feb 2004
Location: Borneo
Age: 27
Posts: 1,567
Blog Entries: 2
Send a message via Yahoo to Monie
Re: IINSERT INTO using ASP and MS Access

Quote:
Originally Posted by BrianLJMU View Post
i think i know whats wrong know
Don't you? So it it related to my previous post up there ^?
__________________

Monie is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old Mar 19th, 2008, 17:21   #7 (permalink)
New Member
 
Join Date: Mar 2008
Location: Liverpool, UK
Posts: 7
Re: IINSERT INTO using ASP and MS Access

I thought i knew how to fix it, but it didn't work.

i think your method is correct. i will look into it further.

Thanks
BrianLJMU is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old Mar 19th, 2008, 22:44   #8 (permalink)
Most Reputable Member
 
Join Date: Feb 2004
Location: Borneo
Age: 27
Posts: 1,567
Blog Entries: 2
Send a message via Yahoo to Monie
Re: IINSERT INTO using ASP and MS Access

Let me know if that success!
Cheers...
__________________

Monie is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old Mar 20th, 2008, 09:26   #9 (permalink)
New Member
 
Join Date: Mar 2008
Location: Liverpool, UK
Posts: 7
Re: IINSERT INTO using ASP and MS Access

It wasn't a success. It's back to the drawing board.

As i am inexperienced at coding, i am unsure how to execute the two pieces of SQL together.

I have a book from library, so will post any results i do get.

Thanks for your help.
BrianLJMU is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old Mar 20th, 2008, 12:48   #10 (permalink)
Most Reputable Member
 
Join Date: Feb 2004
Location: Borneo
Age: 27
Posts: 1,567
Blog Entries: 2
Send a message via Yahoo to Monie
Re: IINSERT INTO using ASP and MS Access

I am not perfectly sure about the sql execution that was causing the problem!
Try google your error message. Here is what I found!
__________________

Monie is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old Mar 20th, 2008, 13:05   #11 (permalink)
New Member
 
Join Date: Mar 2008
Location: Liverpool, UK
Posts: 7
Re: IINSERT INTO using ASP and MS Access

Thank you so much.

I altered the code, then checked to see what was inserted to the database.

The foriegn key field is not updating (this is linked to an autonumber field)

So i will use what you found and edit the relationship.

I also found code that updates the foriegn key in the SQL but i can't find it again now. Which is quite annoying.

Your help is much apprieciated.

Regards
Brian
BrianLJMU is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old Mar 21st, 2008, 11:37   #12 (permalink)
New Member
 
Join Date: Mar 2008
Location: Liverpool, UK
Posts: 7
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();
}
}
}
BrianLJMU is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old Mar 21st, 2008, 17:16   #13 (permalink)
Most Reputable Member
 
Join Date: Feb 2004
Location: Borneo
Age: 27
Posts: 1,567
Blog Entries: 2
Send a message via Yahoo to Monie
Re: IINSERT INTO using ASP and MS Access

Glad to help you but I am not sure I can help you on the Foreign Key thing
I like how you re-design your code to execute the 2 SQL Command
__________________

Monie is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
ASP Multi users Access to Microsoft Access ish ASP Forum 0 Apr 26th, 2007 20:05
Access cheataweb MSSQL & Access 4 Mar 21st, 2007 18:42
ASP to ACCESS ntgcmlfu ASP Forum 16 Apr 18th, 2006 03:33
PHP and Access fogofogo PHP Forum 0 Nov 30th, 2005 11:31
Need help please...ASP and Access DB newbie44 ASP Forum 13 Sep 19th, 2005 22:02



Latest Updates

All Points SEO Security Advisory - CHECK YOUR SITE NOW!

Creative Coding :: February 2008

Webforumz is sponsored by: WESH UK Web Hosting
All times are GMT. The time now is 00:21.

Sleep Study Scoring :: Free Bet :: Website Templates :: Online Betting :: Bookmakers :: Funny Quotes :: Internet Recruitment Software :: Microsoft CRM Experts :: Online Casino :: Decorated Christmas Trees :: Midwife Forums :: Football Betting :: Ecommerce Software :: Web Hosting :: Football Stats :: Dry Cleaning Collection :: xtreme wales - extreme clothing :: Apuestas :: Sharepoint Consultants :: Website Optimisation :: Office Clearance London :: Sharepoint Experts :: Sports Betting :: Casino :: Website Templates :: Web Design Development India :: Online Gambling

Powered by: vBulletin Version 3.7, Copyright ©2000 - 2008, Jelsoft Enterprises Limited.
© 2003-2008 Webforumz.com : All Rights Reserved
Search Engine Friendly URLs by vBSEO 3.2.0 RC6


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59