Hi guys, im trying to insert data in different tables one after another, the first table is "Account" with P_key "Username" then have another table called "Student" with P_key "Alumnus_no". Username is a foreign key on "Student" table meaning in order to enter records on Student table Username must exist in both tables.
I have entered that correctly i dont know whether is my code but here it is.
View_Layer
//Here i insert details to account, and can insert
protected void Button1_Click(object sender, EventArgs e)
{
try
{
newAcc = new NewAccount(txtId.Text, txtConfirmPassword.Text, txtemail.Text, txtCell.Text);
newAcc.CreateAccount();
Response.Redirect("Student.aspx?Username="+ Conv.EncryptString(txtId.Text), false);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + newAcc.error);
}
}
Then i send parameters to Student class in Business layer. This cannot insert into database
protected void Button1_Click(object sender, EventArgs e)
{
try
{
Alumnus_no = Convert.ToInt32(txtAlumnus.Text);
date_of_birth = ddlDay.SelectedItem.Text + "/" + ddlMonth.SelectedItem.Text + "/" + txtYear.Text;
ST = new BusinessLayer.Student(Convert.ToInt32(txtAlumnus.Text), txtId.Text, ddlTitle.SelectedItem.Text, txtFirstnames.Text, txtSurname.Text, date_of_birth, txtCitizen.Text, ddlMStatus.SelectedItem.Text, txtLanguage.Text);
ST.addDetails();
MessageBox.Show(txtId.Text);
Response.Redirect("ContactDetails.aspx?Alumnus_no=" + Alumnus_no, false);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
catch (FormatException)
{
MessageBox.Show("Incorrect Alumnus number!", "Error");
}
}
Here's my Business layer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Data_Logic_Layer;
using System.Windows.Forms;
namespace BusinessLayer
{
public class Student
{
public string error;
private int alumnus_no;
private string title, fname, lname, initials, DOB, citizen, m_status, language, username;
Data_Logic_Layer.StudentDA Stud = new Data_Logic_Layer.StudentDA();
public Student()
{
Alumnus_no = 0;
Title = " ";
Fname = " ";
Lname = " ";
Initials = " ";
dob = " ";
Citizen = " ";
Status = " ";
Language = " ";
Username = " ";
}
public Student(int aNo, string tit, string fn, string ln, string d, string cit, string status, string lang, string userN)
{
Alumnus_no = aNo;
Title = tit;
Fname = fn;
Lname = ln;
Initials = getInitials();
dob = d;
Citizen = cit;
Status = status;
Language = lang;
Username = userN;
}
public int Alumnus_no
{
get { return alumnus_no; }
set { alumnus_no = value; }
}
public string Title
{
get { return title; }
set { title = value; }
}
public string Fname
{
get { return fname; }
set { fname = value; }
}
public string Lname
{
get { return lname; }
set { lname = value; }
}
public string Initials
{
get { return initials; }
set { initials = value; }
}
public string dob
{
get { return DOB; }
set { DOB = value; }
}
public string Citizen
{
get { return citizen; }
set { citizen = value; }
}
public string Status
{
get { return m_status; }
set { m_status = value; }
}
public string Language
{
get { return language; }
set { language = value; }
}
public string Username
{
get { return username; }
set { username = value; }
}
public void addDetails()
{
Stud.addStudent(Alumnus_no, Title, Fname, Lname, getInitials(), dob, Citizen, Status, Language, Username);
}
}
And the Data layer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace Data_Logic_Layer
{
public class StudentDA : Connection
{
public void addStudent(int AlumnusNo, string Title, string Names, string surname, string initials, string DOB, string citizen, string marital_status, string language, string username)
{
try
{
OpenConnection();
com = new SqlCommand("add_Student", con);
com.CommandType = System.Data.CommandType.StoredProcedure;
com.Parameters.AddWithValue("@Alumnus_no", AlumnusNo);
com.Parameters.AddWithValue("@Title", Title);
com.Parameters.AddWithValue("@Firstnames", Names);
com.Parameters.AddWithValue("@Surname", surname);
com.Parameters.AddWithValue("@Initials", initials);
com.Parameters.AddWithValue("@Date_of_birth", DOB);
com.Parameters.AddWithValue("@Citizenship", citizen);
com.Parameters.AddWithValue("@Marital_status", marital_status);
com.Parameters.AddWithValue("@Preferred_Language", language);
com.Parameters.AddWithValue("@Username", username);
com.ExecuteNonQuery();
}
catch (Exception ex)
{
error = ex.Message;
}
}
}