protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            string  strSql = "select courseName=CourseMainType+'_'+Title,idx from v_CourseMain order by CourseMainType,SortNo";
            DataSet ds     = SqlHelper.ExecuteDataset(CommandType.Text, strSql);
            this.ddlCourse.DataSource     = ds.Tables[0];
            this.ddlCourse.DataTextField  = "courseName";
            this.ddlCourse.DataValueField = "idx";
            this.ddlCourse.DataBind();

            if (!string.IsNullOrEmpty(Request["Idx"]))
            {
                DBEntity.Tab_CourseMain_TestQuestion ent = new DBEntity.Tab_CourseMain_TestQuestion();
                ent = ent.Get(Request["Idx"]);
                this.QuestionText.Text = ent.QuestionText;
                this.Aquestion.Text    = ent.Aquestion;
                this.Bquestion.Text    = ent.Bquestion;
                this.Cquestion.Text    = ent.Cquestion;
                this.Dquestion.Text    = ent.Dquestion;
                this.Answer.Text       = ent.Answer;
                for (int i = 0; i < ddlCourse.Items.Count; i++)
                {
                    if (ddlCourse.Items[i].Value == ent.CourseMainIdx_Fx.ToString())
                    {
                        ddlCourse.Items[i].Selected = true;
                    }
                }
            }
        }
    }
        private static Tab_CourseMain_TestQuestion ToModel(DataRow row)
        {
            Tab_CourseMain_TestQuestion model = new Tab_CourseMain_TestQuestion();

            model.Idx = row.IsNull("Idx")?null:(System.Int32?)row["Idx"];
            model.CourseMainIdx_Fx = row.IsNull("CourseMainIdx_Fx")?null:(System.Int32?)row["CourseMainIdx_Fx"];
            model.QuestionText     = row.IsNull("QuestionText")?null:(System.String)row["QuestionText"];
            model.Aquestion        = row.IsNull("Aquestion")?null:(System.String)row["Aquestion"];
            model.Bquestion        = row.IsNull("Bquestion")?null:(System.String)row["Bquestion"];
            model.Cquestion        = row.IsNull("Cquestion")?null:(System.String)row["Cquestion"];
            model.Dquestion        = row.IsNull("Dquestion")?null:(System.String)row["Dquestion"];
            model.Answer           = row.IsNull("Answer")?null:(System.String)row["Answer"];
            model.CreatedDate      = row.IsNull("CreatedDate")?null:(System.DateTime?)row["CreatedDate"];
            return(model);
        }
        public int AddNew(Tab_CourseMain_TestQuestion model)
        {
            string sql = "insert into Tab_CourseMain_TestQuestion(CourseMainIdx_Fx,QuestionText,Aquestion,Bquestion,Cquestion,Dquestion,Answer,CreatedDate)  values(@CourseMainIdx_Fx,@QuestionText,@Aquestion,@Bquestion,@Cquestion,@Dquestion,@Answer,@CreatedDate); select @@identity ;";
            int    Idx = Convert.ToInt32(SqlHelper.ExecuteScalar(CommandType.Text, sql
                                                                 , new SqlParameter("@CourseMainIdx_Fx", model.CourseMainIdx_Fx)
                                                                 , new SqlParameter("@QuestionText", model.QuestionText)
                                                                 , new SqlParameter("@Aquestion", model.Aquestion)
                                                                 , new SqlParameter("@Bquestion", model.Bquestion)
                                                                 , new SqlParameter("@Cquestion", model.Cquestion)
                                                                 , new SqlParameter("@Dquestion", model.Dquestion)
                                                                 , new SqlParameter("@Answer", model.Answer)
                                                                 , new SqlParameter("@CreatedDate", model.CreatedDate)
                                                                 ));

            return(Idx);
        }
        public bool Update(Tab_CourseMain_TestQuestion model)
        {
            string sql  = "update Tab_CourseMain_TestQuestion set CourseMainIdx_Fx=@CourseMainIdx_Fx,QuestionText=@QuestionText,Aquestion=@Aquestion,Bquestion=@Bquestion,Cquestion=@Cquestion,Dquestion=@Dquestion,Answer=@Answer,CreatedDate=@CreatedDate where Idx=@Idx";
            int    rows = SqlHelper.ExecuteNonQuery(CommandType.Text, sql
                                                    , new SqlParameter("@CourseMainIdx_Fx", model.CourseMainIdx_Fx)
                                                    , new SqlParameter("@QuestionText", model.QuestionText)
                                                    , new SqlParameter("@Aquestion", model.Aquestion)
                                                    , new SqlParameter("@Bquestion", model.Bquestion)
                                                    , new SqlParameter("@Cquestion", model.Cquestion)
                                                    , new SqlParameter("@Dquestion", model.Dquestion)
                                                    , new SqlParameter("@Answer", model.Answer)
                                                    , new SqlParameter("@CreatedDate", model.CreatedDate)
                                                    , new SqlParameter("Idx", model.Idx)
                                                    );

            return(rows > 0);
        }
        public Tab_CourseMain_TestQuestion Get(string Idx)
        {
            DataTable dt = SqlHelper.ExecuteDataset(CommandType.Text, "select * from Tab_CourseMain_TestQuestion  where Idx=@Idx",
                                                    new SqlParameter("Idx", Idx)).Tables[0];

            if (dt.Rows.Count > 1)
            {
                throw new Exception("more than 1 row was found");
            }

            if (dt.Rows.Count <= 0)
            {
                return(null);
            }

            DataRow row = dt.Rows[0];
            Tab_CourseMain_TestQuestion model = ToModel(row);

            return(model);
        }