Esempio n. 1
0
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            bool               success         = true;
            RadioButton        radbtnOptionSet = Repeater1.Controls[Repeater1.Controls.Count - 1].Controls[0].FindControl("radbtnOptionSet") as RadioButton;
            HtmlGenericControl divErr          = Repeater1.Controls[Repeater1.Controls.Count - 1].Controls[0].FindControl("divError1") as HtmlGenericControl;
            Label              lblErr          = Repeater1.Controls[Repeater1.Controls.Count - 1].Controls[0].FindControl("lblError1") as Label;
            int count = 0;

            foreach (RepeaterItem item in Repeater1.Items)
            {
                if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
                {
                    RadioButton radbtnOption = (RadioButton)item.FindControl("radbtnOption");
                    if (radbtnOption.Checked == true)
                    {
                        count++;
                    }
                }
            }
            if (count >= 1 && radbtnOptionSet.Checked == true)
            {
                divErr.Attributes.Add("style", "display:block;");
                lblErr.Text             = "More Than One Answer Cannot be Right Answer*";
                radbtnOptionSet.Checked = false;
                return;
            }
            foreach (RepeaterItem item in Repeater1.Items)
            {
                if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
                {
                    RadioButton radbtnOption = (RadioButton)item.FindControl("radbtnOption");
                    TextBox     txtOption    = (TextBox)item.FindControl("txtOption");
                    Button      btnDelete    = (Button)item.FindControl("btnDelete");
                    string      answer;
                    if (IsOtherLanguage(ddlSubject.SelectedValue))
                    {
                        answer = Regex.Replace(txtOption.Text, "<[^>]*>", string.Empty);
                    }
                    else
                    {
                        answer = txtOption.Text.Trim();
                    }
                    if (answer.Contains("'"))
                    {
                        answer = answer.Replace("'", "`");
                    }
                    string qurAns = "select AnswerId from Answers where QuestionId='" + Session["QuestionId"].ToString() + "' and Answer='" + answer + "'";
                    if (dbLibrary.idHasRows(qurAns))
                    {
                        string ans = dbLibrary.idGetAFieldByQuery(qurAns);
                        if (btnDelete.CommandArgument != ans)
                        {
                            divErr.Attributes.Add("style", "display:block;");
                            lblErr.Text = "This Option Already Exists*";
                            divErr.Focus();
                            success = false;
                            return;
                        }
                        else
                        {
                            if (ddlAnswerType.SelectedValue == "1")
                            {
                                if (IsOtherLanguage(ddlSubject.SelectedValue))
                                {
                                    bool isrightanswer = radbtnOption.Checked == true ? true : false;
                                    //string qur = "update Answers set Answer=N" + "'" + answer + "', IsRightAnswer='" + isrightanswer + "' where AnswerId='" + btnDelete.CommandArgument + "'";
                                    //dbLibrary.idExecute(qur);
                                    SqlConnection sqlConnection = new SqlConnection(dbLibrary.conStr);
                                    string        query         = "Exec [proc_AddOptions] @AnswerId,@Answer,@QuestionId,@isRightAnswer,@EnteredBy,@StatusId,@type";
                                    SqlCommand    cmd           = new SqlCommand(query, sqlConnection);
                                    cmd.Parameters.AddWithValue("@AnswerId", btnDelete.CommandArgument);
                                    cmd.Parameters.AddWithValue("@Answer", answer);
                                    cmd.Parameters.AddWithValue("@QuestionId", Session["QuestionId"].ToString());
                                    cmd.Parameters.AddWithValue("@isRightAnswer", isrightanswer);
                                    cmd.Parameters.AddWithValue("@EnteredBy", Session["DEId"].ToString());
                                    cmd.Parameters.AddWithValue("@StatusId", "1");
                                    cmd.Parameters.AddWithValue("@type", "Update");
                                    try
                                    {
                                        sqlConnection.Open();
                                        cmd.ExecuteNonQuery();
                                    }
                                    catch
                                    {
                                    }
                                    finally
                                    {
                                        sqlConnection.Close();
                                    }
                                }
                                else
                                {
                                    //dbLibrary.idUpdateTable("Answers",
                                    //    "AnswerId=" + btnDelete.CommandArgument,
                                    //    "Answer", txtOption.Text.Replace("'", "`"),
                                    //    "isRightAnswer", radbtnOption.Checked == true ? "1" : "0");
                                    string qry = dbLibrary.idBuildQuery("[proc_AddOptions]", btnDelete.CommandArgument, txtOption.Text.Replace("'", "`"), Session["QuestionId"].ToString(), radbtnOption.Checked == true ? "1" : "0", Session["DEId"].ToString(), "1", "Update");
                                    dbLibrary.idExecute(qry);
                                }

                                //  lblMsg.Text = "Options Updated Successfully";
                                //   Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction1", "runEffect1()", true);
                                divErr.Attributes.Add("style", "display:none;");
                            }
                            else
                            {
                                success = false;
                            }
                        }
                    }
                    else
                    {
                        divErr.Attributes.Add("style", "display:none;");
                    }
                    if (ddlAnswerType.SelectedValue == "1")
                    {
                        if (IsOtherLanguage(ddlSubject.SelectedValue))
                        {
                            bool isrightanswer = radbtnOption.Checked == true ? true : false;
                            //string qur = "update Answers set Answer=N" + "'" + answer + "', IsRightAnswer='" + isrightanswer + "' where AnswerId='" + btnDelete.CommandArgument + "'";
                            //dbLibrary.idExecute(qur);
                            SqlConnection sqlConnection = new SqlConnection(dbLibrary.conStr);
                            string        query         = "Exec [proc_AddOptions] @AnswerId,@Answer,@QuestionId,@isRightAnswer,@EnteredBy,@StatusId,@type";
                            SqlCommand    cmd           = new SqlCommand(query, sqlConnection);
                            cmd.Parameters.AddWithValue("@AnswerId", btnDelete.CommandArgument);
                            cmd.Parameters.AddWithValue("@Answer", answer);
                            cmd.Parameters.AddWithValue("@QuestionId", Session["QuestionId"].ToString());
                            cmd.Parameters.AddWithValue("@isRightAnswer", isrightanswer);
                            cmd.Parameters.AddWithValue("@EnteredBy", Session["DEId"].ToString());
                            cmd.Parameters.AddWithValue("@StatusId", "1");
                            cmd.Parameters.AddWithValue("@type", "Update");
                            try
                            {
                                sqlConnection.Open();
                                cmd.ExecuteNonQuery();
                            }
                            catch
                            {
                            }
                            finally
                            {
                                sqlConnection.Close();
                            }
                        }
                        else
                        {
                            //dbLibrary.idUpdateTable("Answers",
                            //    "AnswerId=" + btnDelete.CommandArgument,
                            //    "Answer", txtOption.Text.Replace("'", "`"),
                            //    "isRightAnswer", radbtnOption.Checked == true ? "1" : "0");
                            string qry = dbLibrary.idBuildQuery("[proc_AddOptions]", btnDelete.CommandArgument, txtOption.Text.Replace("'", "`"), Session["QuestionId"].ToString(), radbtnOption.Checked == true ? "1" : "0", Session["DEId"].ToString(), "1", "Update");
                            dbLibrary.idExecute(qry);
                        }


                        // lblMsg.Text = "Options Updated Successfully";
                        // Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction1", "runEffect1()", true);
                        divErr.Attributes.Add("style", "display:none;");
                    }
                    else
                    {
                        success = false;
                    }
                    UpdatePanel UpdatePanel2 = (UpdatePanel)item.FindControl("UpdatePanel2");
                    UpdatePanel2.Update();
                }
            }
            if (success == true)
            {
                lblMsg.Text = "Options Updated Successfully";
                Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction1", "runEffect1()", true);
            }
            //  gridQuestion_SelectedIndexChanged(gridQuestion, EventArgs.Empty);
        }
Esempio n. 2
0
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            HtmlGenericControl divError1 = Repeater1.Controls[Repeater1.Controls.Count - 1].Controls[0].FindControl("divError1") as HtmlGenericControl;
            Label       lblError1        = Repeater1.Controls[Repeater1.Controls.Count - 1].Controls[0].FindControl("lblError1") as Label;
            TextBox     txtOption        = Repeater1.Controls[Repeater1.Controls.Count - 1].Controls[0].FindControl("txtOption") as TextBox;
            RadioButton radbtnOptionSet  = Repeater1.Controls[Repeater1.Controls.Count - 1].Controls[0].FindControl("radbtnOptionSet") as RadioButton;
            string      fileName         = "";
            int         count            = 0;

            foreach (RepeaterItem item in Repeater1.Items)
            {
                if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
                {
                    RadioButton radbtnOption = (RadioButton)item.FindControl("radbtnOption");
                    if (radbtnOption.Checked == true)
                    {
                        count++;
                    }
                }
            }
            if (count >= 1 && radbtnOptionSet.Checked == true)
            {
                divError1.Attributes.Add("style", "display:block;");
                lblError1.Text          = "More Than One Answer Cannot be Right Answer*";
                radbtnOptionSet.Checked = false;
                return;
            }

            if (ddlAnswerType.SelectedValue == "3")
            {
                FileUpload FileUpload1 = Repeater1.Controls[Repeater1.Controls.Count - 1].Controls[0].FindControl("FileUpload1") as FileUpload;
                if (!string.IsNullOrEmpty(FileUpload1.PostedFile.FileName))
                {
                    if (CheckFileType(FileUpload1.FileName))
                    {
                        int     latestAnswerid  = 0;
                        string  qur             = "SELECT IDENT_CURRENT ('Answers') as id Select COUNT(*) as count";
                        DataSet ds              = dbLibrary.idGetCustomResult(qur);
                        int     currentIdentity = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                        int     totalRows       = int.Parse(ds.Tables[1].Rows[0]["count"].ToString());
                        if (totalRows == 0 && currentIdentity == 1)
                        {
                            latestAnswerid = currentIdentity;
                        }
                        else
                        {
                            latestAnswerid = currentIdentity + 1;
                        }
                        divError1.Attributes.Add("style", "display:none;");
                        string extension = Path.GetFileName(FileUpload1.PostedFile.FileName);
                        extension = extension.Split('.')[1];
                        fileName  = Session["QuestionId"].ToString() + "_" + latestAnswerid + "." + extension;
                        FileUpload1.PostedFile.SaveAs(Server.MapPath("~/testImages/") + fileName);
                    }
                    else
                    {
                        divError1.Attributes.Add("style", "display:block;");
                        lblError1.Text = "Invalid FileType!!Image File Can only be jpg, png or gif";
                        return;
                    }
                }
                else
                {
                    divError1.Attributes.Add("style", "display:block;");
                    lblError1.Text = "Image File Cannot be Empty*";
                    return;
                }
            }
            if (ddlAnswerType.SelectedValue == "1")
            {
                if (txtOption.Text == "")
                {
                    divError1.Attributes.Add("style", "display:block;");
                    lblError1.Text = "Option Text Cannot be Empty*";
                    return;
                }
                else
                {
                    divError1.Attributes.Add("style", "display:none;");
                }
            }
            string answer = ddlAnswerType.SelectedValue == "3" ? "~/testImages/" + fileName : txtOption.Text;

            if (ddlAnswerType.SelectedValue == "1")
            {
                if (IsOtherLanguage(ddlSubject.SelectedValue))
                {
                    answer = Regex.Replace(answer, "<[^>]*>", string.Empty);
                }
                else if (answer.Contains("'"))
                {
                    answer = answer.Replace("'", "`");
                }
            }
            string qurAns = "select AnswerId from Answers where QuestionId='" + Session["QuestionId"].ToString() + "' and Answer='" + answer + "'";

            if (dbLibrary.idHasRows(qurAns))
            {
                divError1.Attributes.Add("style", "display:block;");
                lblError1.Text = "This Option Already Exists*";
                divError1.Focus();
                return;
            }
            else
            {
                divError1.Attributes.Add("style", "display:none;");
            }

            if (IsOtherLanguage(ddlSubject.SelectedValue))
            {
                bool isrightanswer = radbtnOptionSet.Checked == true ? true : false;
                //string qurans = "Insert Into Answers values('" + Session["QuestionId"].ToString() + "',N" + "'" + answer + "','" + isrightanswer + "')";
                //dbLibrary.idExecute(qurans);
                SqlConnection sqlConnection = new SqlConnection(dbLibrary.conStr);
                string        query         = "Exec [proc_AddOptions] @AnswerId,@Answer,@QuestionId,@isRightAnswer,@EnteredBy,@StatusId,@type";
                SqlCommand    cmd           = new SqlCommand(query, sqlConnection);
                cmd.Parameters.AddWithValue("@AnswerId", "");
                cmd.Parameters.AddWithValue("@Answer", answer);
                cmd.Parameters.AddWithValue("@QuestionId", Session["QuestionId"].ToString());
                cmd.Parameters.AddWithValue("@isRightAnswer", isrightanswer);
                cmd.Parameters.AddWithValue("@EnteredBy", Session["DEId"].ToString());
                cmd.Parameters.AddWithValue("@StatusId", "1");
                cmd.Parameters.AddWithValue("@type", "Insert");
                try
                {
                    sqlConnection.Open();
                    cmd.ExecuteNonQuery();
                }
                catch
                {
                }
                finally
                {
                    sqlConnection.Close();
                }
            }
            else
            {
                //dbLibrary.idInsertInto("Answers",
                //    "QuestionId", Session["QuestionId"].ToString(),
                //    "Answer", ddlAnswerType.SelectedValue == "3" ? "~/testImages/" + fileName : answer.Replace("''", "'"),
                //    "IsRightAnswer", radbtnOptionSet.Checked == true ? "1" : "0");
                string qry = dbLibrary.idBuildQuery("[proc_AddOptions]", "", ddlAnswerType.SelectedValue == "3" ? "~/testImages/" + fileName : answer.Replace("''", "'"), Session["QuestionId"].ToString(), radbtnOptionSet.Checked == true ? "1" : "0", Session["DEId"].ToString(), "1", "Insert");
                dbLibrary.idExecute(qry);
            }
            lblMsg.Text = "Option Added Successfully";
            Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction1", "runEffect1()", true);
            LoadRepeater();
            //   gridQuestion.SelectedIndex = -1;
            //   gridQuestion_SelectedIndexChanged(sender, EventArgs.Empty);
        }