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;
            }
        }
}

Recommended Answers

All 3 Replies

Do you receive an error .... or ? You provided a lot of code but not much information on the behavior of the problem.

The code suppose to insert to the databes using stored procedure which i called on Data layer, its not inserting and not giving any error and cant find any bug

Are you sure its not giving any errors? You handle the exceptions all over the place. Look at your "output" window in the IDE for exceptions lines being written but your debugger may not break.

Also you need to break this code in to a sample project and isolate the problem if you can't debug it with this complex scenario. If the SQL Server is not throwing an exception then this is really a C# question. Simplify your code and see if you can run it down.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.