-3
我正在連接到數據庫的我的會員網站上工作。所有的表格都已經在數據庫中創建了。但是,我在處理外鍵時遇到問題。其實,我不知道該怎麼做...在asp.net中處理外鍵
我使用VS 2012的Web
這是當他們想加入俱樂部的會員填寫的註冊頁面。在成員表中,我有幾個需要處理的外鍵:addressID,membershipID,emergencyContactEmail和clubID。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(IsPostBack)
{
SqlConnection regConn = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString);
regConn.Open();
string check = "select count(*) from member where memberEmail ='" + emailTB.Text + "'";
SqlCommand cmd = new SqlCommand(check, regConn);
int i = Convert.ToInt32(cmd.ExecuteScalar().ToString());
if (i == 1)
{
Response.Write("User already exists");
}
regConn.Close();
}
}
protected void SubmitButton_Click(object sender, EventArgs e)
{
try
{
Guid addressGuid = Guid.NewGuid();
Guid medicalGuid = Guid.NewGuid();
Guid membershipGuid = Guid.NewGuid();
SqlConnection regConn = new SqlConnection(ConfigurationManager.ConnectionStrings["Data Source=student5;Initial Catalog=FeelGoodDatabase;Persist Security Info=True;User ID=sa;Password=*******"].ConnectionString);
regConn.Open();
string insertMember = "insert into member (memberEmail, firstName, surname, dateOfBirth, password, contactNumber) values(@email, @firstName, @surname, @dob, @password, @phone)";
string insertAddress = "insert into address (addressID, lineOne, lineTwo, town, county) values (@addressID, @lineOne, @lineTwo, @town, @county)";
string insertEmergency = "insert into emergencyContact (emergencyContactEmail, emergencyContactName, emergencyContactSurname, emergencyContactNumber) values (@emEmail, @emName, @emSurname, @emPhone)";
string insertMedical = "insert into medicalInfo(medicalInfoID, physicalHealthInfo) values (@medicalInfoID, @healthInfo)";
string insertMembership = "insert into membership(membershipID, membershipType, paymentOption, membershipPrice) values (@memID, @memType, @payOption, @amount)";
string selectedMembership = this.RadioButtonList1.SelectedItem.Text;
string selectedPayOption = this.RadioButtonList2.SelectedItem.Text;
SqlCommand cmd = new SqlCommand(insertMember, regConn);
SqlCommand cmdAddress = new SqlCommand(insertAddress, regConn);
SqlCommand cmdEmergency = new SqlCommand(insertEmergency, regConn);
SqlCommand cmdMedical = new SqlCommand(insertMedical, regConn);
SqlCommand cmdMembership = new SqlCommand(insertMembership, regConn);
cmdAddress.Parameters.AddWithValue("@addressID", addressGuid.ToString());
cmdMedical.Parameters.AddWithValue("@medicalInfoID", medicalGuid.ToString());
cmdMembership.Parameters.AddWithValue("@memID", membershipGuid.ToString());
cmd.Parameters.AddWithValue("@email", emailTB.Text);
cmd.Parameters.AddWithValue("@firstName", firstNameTB.Text);
cmd.Parameters.AddWithValue("@surname", surnameTB.Text);
cmd.Parameters.AddWithValue("@dob", dobTB.Text);
cmd.Parameters.AddWithValue("@password", passwordTB.Text);
cmd.Parameters.AddWithValue("@phone", phoneTB.Text);
cmdAddress.Parameters.AddWithValue("@lineOne", lineOneTB.Text);
cmdAddress.Parameters.AddWithValue("@lineTwo", lineTwoTB.Text);
cmdAddress.Parameters.AddWithValue("@town", townTB.Text);
cmdAddress.Parameters.AddWithValue("@county", emailTB.Text);
cmdEmergency.Parameters.AddWithValue("@emEmail", emEmailTB.Text);
cmdEmergency.Parameters.AddWithValue("@emName", emnameTB.Text);
cmdEmergency.Parameters.AddWithValue("@emSurname", emSurnameTB.Text);
cmdEmergency.Parameters.AddWithValue("@emPhone", emPhoneTB.Text);
cmdMedical.Parameters.AddWithValue("@healthInfo", medicalTB.Text);
cmdMembership.Parameters.AddWithValue("@memType", selectedMembership);
cmdMembership.Parameters.AddWithValue("@paymentOption", selectedPayOption);
cmdMembership.Parameters.AddWithValue("@amount", amountTB.Text);
cmd.ExecuteNonQuery();
cmdAddress.ExecuteNonQuery();
cmdEmergency.ExecuteNonQuery();
cmdMedical.ExecuteNonQuery();
cmdMembership.ExecuteNonQuery();
Response.Redirect("MemberPage.aspx");
regConn.Close();
}
catch (Exception)
{
Response.Write("Sign up not successful");
}
}
}
此外,當我運行此代碼,我得到一個錯誤信息,指出:對象引用未設置到發生在這條線的對象的實例:
SqlConnection regConn = new SqlConnection(ConfigurationManager.ConnectionStrings["Data Source=student5;Initial Catalog=FeelGoodDatabase;Persist Security Info=True;User ID=sa;Password=********"].ConnectionString);
你有什麼想法如何解決這個問題? 謝謝。
它本質上是一個不確定的變量。忘記所有這些數據庫內容並專注於此。 –
ConfigurationManager.ConnectionStrings是一個數組,它可能有一個簡單的鍵,它與Web.config的ConnectionString部分的元素相關。將整個連接字符串作爲數組的索引可能不是一個好主意:) – thsorens
請參見[ConfigurationManager.ConnectionStrings屬性]中的示例(https://msdn.microsoft.com/zh-cn/library/system.configuration .configurationmanager.connectionstrings%28v = vs.110%29.aspx)如何使用它。 –