public bool InsertStudent(Student stu)
        {
            if (con.State != ConnectionState.Open)
            {
                con.Open();
            }
            using (SqlTransaction tran = con.BeginTransaction())
            {
                try
                {
                    //con.Open();
                    SqlCommand _command = new SqlCommand();
                    string     query    = "INSERT INTO Student(StudentName,DepartmentId,FatherName,MotherName,DateOfBirth,AdmissionYear,StudentCode,Hall,Address,ContactNo,Email,BloodGroup,Sex,BioPhoto) VALUES ('"
                                          + stu.StudentName + "','" + stu.DepartmentId + "','" + stu.FatherName + "','" + stu.MotherName + "','" + stu.DateOfBirth + "','" + stu.AdmissionYear + "','" + stu.StudentCode +
                                          "','" + stu.Hall + "','" + stu.Address + "','" + stu.ContactNo + "','" + stu.Email + "','" + stu.BloodGroup +
                                          "','" + stu.Sex + "',@BioPhoto);" + " SELECT SCOPE_IDENTITY();";


                    _command.Parameters.Add("@BioPhoto", SqlDbType.VarBinary).Value = stu.BioPhoto;

                    _command.CommandText = query;
                    _command.Connection  = con;
                    _command.Transaction = tran;


                    UserInfo usrobj = new UserInfo();



                    usrobj.UserName     = stu.UserName;
                    usrobj.Password     = stu.Password;
                    usrobj.UserTypeCode = 1;                              ////student

                    Int64 ID = Convert.ToInt64(_command.ExecuteScalar()); ////Get id Of StudentTable

                    usrobj.EmpOrStdId = ID;

                    UserInfo_DBAccess db_usrinfo = new UserInfo_DBAccess(con, _command);
                    db_usrinfo.InsertUserInfo(usrobj);
                    tran.Commit();
                    ///_command.ExecuteNonQuery();
                }
                catch
                {
                    tran.Rollback();
                    return(false);
                }
                finally
                {
                    if (con.State != ConnectionState.Closed)
                    {
                        con.Close();
                    }
                }
            }
            return(true);
        }
Ejemplo n.º 2
0
        public bool InsertEmployee(Employee emp)
        {
            if (con.State != ConnectionState.Open)
            {
                con.Open();
            }
            //SqlConnection con = new SqlConnection();
            //con.ConnectionString = @"Data Source=THE-OPPORTUNIST\SQLEXPRESS; Initial Catalog=OR_DB; Integrated Security=True";
            using (SqlTransaction tran = con.BeginTransaction())
            {
                try
                {
                    //con.Open();
                    SqlCommand _command = new SqlCommand();
                    string     query    = "INSERT INTO Employee(EmployeeName,EmployeeCode,Address,ContactNo,Email,BloodGroup,Sex,JoiningDate,DepartmentId,BioPhoto) VALUES ('" + emp.EmployeeName + "','" + emp.EmployeeCode + "','"
                                          + emp.Address + "','" + emp.ContactNo + "','" + emp.Email + "','"
                                          + emp.BloodGroup + "','" + emp.Sex + "','" + emp.JoiningDate + "','"
                                          + emp.DepartmentId + "',@BioPhoto);" + " SELECT SCOPE_IDENTITY();";

                    _command.Parameters.Add("@BioPhoto", SqlDbType.VarBinary).Value = emp.BioPhoto;
                    _command.CommandText = query;
                    _command.Connection  = con;
                    _command.Transaction = tran;

                    UserInfo usrobj = new UserInfo();


                    usrobj.UserName     = emp.UserName;
                    usrobj.Password     = emp.Password;
                    usrobj.UserTypeCode = 2;///////////type=empoyee

                    Int64 ID = Convert.ToInt64(_command.ExecuteScalar());

                    usrobj.EmpOrStdId = ID;

                    UserInfo_DBAccess db_usrinfo = new UserInfo_DBAccess(con, _command);
                    db_usrinfo.InsertUserInfo(usrobj);
                    tran.Commit();
                }
                catch
                {
                    tran.Rollback();
                    return(false);
                }
                finally
                {
                    if (con.State != ConnectionState.Closed)
                    {
                        con.Close();
                    }
                }
            }
            return(true);
        }