mercredi 24 février 2016

survey page in asp c#

I have requirement to create a survey web form which should be loaded from database. Each questions will have 10 rating items from 1-10 and the result should be saved to the database with question number. I tried to achieve it with a web form with label control at the top and RadioButtonList for options, but only one question and answer is possible to load display at a time. I'm new to web programming. If there is any working code sample or any idea how to achieve it would be helpful. I've done the coding to put each question on page and on button click loading next question. But need all the questions on a single page.

                public partial class _Default : System.Web.UI.Page 
                {
                public static SqlConnection sqlconn;
                protected string PostBackStr;

                protected void Page_Load(object sender, EventArgs e)
                {
                sqlconn = new SqlConnection(ConfigurationManager.AppSettings["sqlconnstr"].ToString());
                PostBackStr = Page.ClientScript.GetPostBackEventReference(this, "time");
                if (IsPostBack)
                {
                string eventArg = Request["__EVENTARGUMENT"];
                if (eventArg == "time")
                {
                string str = "select * from tbl_Question";
                SqlDataAdapter da2 = new SqlDataAdapter(str, sqlconn);
                DataSet ds2 = new DataSet();
                da2.Fill(ds2, "Question");
                int count = ds2.Tables[0].Rows.Count;
                getNextQuestion(count);
                }
                }
                }
                protected void Button1_Click(object sender, EventArgs e)
                {
                Label1.Visible = false;
                txtName.Visible =false;
                Button1.Visible = false;
                Panel1.Visible = true;
                lblName.Text = "Name : " + txtName.Text;
                int score = Convert.ToInt32(txtScore.Text);
                lblScore.Text = "Score : " + Convert.ToString(score);
                Session["counter"]="1";
                Random rnd = new Random();
                int i = rnd.Next(1, 10);//Here specify your starting slno of question table and ending no.
                //lblQuestion.Text = i.ToString();
                getQuestion(i);

                }
                protected void Button2_Click(object sender, EventArgs e)
                {
                string str = "select * from tbl_Question ";
                SqlDataAdapter da2 = new SqlDataAdapter(str, sqlconn);
                DataSet ds2 = new DataSet();
                da2.Fill(ds2, "Question");
                int count = ds2.Tables[0].Rows.Count;
                getNextQuestion(count);

                }
                public void getQuestion(int no)
                {
                string str = "select * from tbl_Question where slNo=" + no + "";
                SqlDataAdapter da2 = new SqlDataAdapter(str, sqlconn);
                DataSet ds2 = new DataSet();
                da2.Fill(ds2, "Question");
                // int count = ds2.Tables[0].Rows.Count;
                if (ds2.Tables[0].Rows.Count > 0)
                {

                DataRow dtr;
                int i = 0;
                while (i < ds2.Tables[0].Rows.Count)
                {
                dtr = ds2.Tables[0].Rows[i];
                Session["Answer"] = Convert.ToString(Convert.ToInt32 (dtr["Correct"].ToString())-1);
                lblQuestion.Text = "Q." + Session["counter"].ToString() + "  " + dtr["Question"].ToString();
                lblQuestion2.Text = "Q." + Session["counter"].ToString() + "  " + dtr["arQuestion"].ToString();
                LblQNNo.Text = Session["counter"].ToString();
                RblOption.ClearSelection();
                RblOption.Items.Clear();
                RblOption.Items.Add(dtr["Option1"].ToString());
                RblOption.Items.Add(dtr["Option2"].ToString());
                RblOption.Items.Add(dtr["Option3"].ToString());
                RblOption.Items.Add(dtr["Option4"].ToString());
                RblOption.Items.Add(dtr["Option5"].ToString());
                RblOption.Items.Add(dtr["Option6"].ToString());
                RblOption.Items.Add(dtr["Option7"].ToString());
                RblOption.Items.Add(dtr["Option8"].ToString());
                RblOption.Items.Add(dtr["Option9"].ToString());
                RblOption.Items.Add(dtr["Option10"].ToString());

                i++;
                }
                }
                }
                public void getNextQuestion(int cnt)
                {

                if (Convert.ToInt32(Session["counter"].ToString()) < cnt)
                {
                Random rnd = new Random();
                int i = rnd.Next(1, 10);
                lblQuestion.Text = i.ToString();
                getQuestion(i);
                //qst_no = i;
                Session["counter"] = Convert.ToString(Convert.ToInt32(Session["counter"].ToString()) + 1);

                }
                else
                {
                Panel2.Visible = false;
                //code for displaying after completting the exam, if you want to show the result then you can code here.
                }
                }




                protected void Button3_Click(object sender, EventArgs e)
                {
                insertAns();
                }

                private void insertAns()
                {
                SqlCommand cmd;
                sqlconn = new SqlConnection(ConfigurationManager.AppSettings["sqlconnstr"].ToString());
                cmd = new SqlCommand("insert into Tbl_Ans(UserID, Question_ID, Answer) values(@ans, @ans1, @ans2)", sqlconn);

                cmd.Parameters.AddWithValue("@ans", txtName.Text);
                cmd.Parameters.AddWithValue("@ans1", Session["counter"].ToString());
                cmd.Parameters.AddWithValue("@ans2", RblOption.SelectedItem.Text);
                sqlconn.Open();
                int i = cmd.ExecuteNonQuery();
                sqlconn.Close();

                if (i != 0)
                {
                lbmsg.Text = "Your Answer Submitted Succesfully";
                lbmsg.ForeColor = System.Drawing.Color.ForestGreen;
                }
                else
                {
                lbmsg.Text = "Some Problem Occured";
                lbmsg.ForeColor = System.Drawing.Color.Red;
                }
                }
                #region Connection Open
                public void ConnectionOpen()
                {
                try
                {
                if (sqlconn.State == ConnectionState.Closed) { sqlconn.Open(); }
                }
                catch (SqlException ex)
                {
                lbmsg.Text = ex.Message;
                }
                catch (SystemException sex)
                {
                lbmsg.Text = sex.Message;
                }
                }
                #endregion
                #region Connection Close
                public void ConnectionClose()
                {
                try
                {
                if (sqlconn.State != ConnectionState.Closed) { sqlconn.Close(); }
                }
                catch (SqlException ex)
                {
                lbmsg.Text = ex.Message;
                }
                catch (SystemException exs)
                {
                lbmsg.Text = exs.Message;
                }
                }
                #endregion

                }




Aucun commentaire:

Enregistrer un commentaire