protected void add_question(object sender, EventArgs e) { user faculty = (user)Session["user_logged_in"]; if (question_validator.IsValid && marks_validator.IsValid) { SqlConnection con = new SqlConnection(); con.ConnectionString = "Data Source = (localdb)\\MSSQLlocalDB;Initial Catalog=mini_project;Integrated Security=True"; try { con.Open(); string isMcQ = "No"; if (mcq.Checked) { isMcQ = "Yes"; } String query = "INSERT INTO questions(question_name,marks,mcq,faculty_id,faculty_name,subject,branch,semester,year) VALUES("; query += "@question_name,@marks,@isMcq,@faculty_id,@faculty_name,@subject,@branch,@semester,@year)"; SqlCommand cmd = new SqlCommand(query, con); cmd.Parameters.AddWithValue("@question_name", question.Text); cmd.Parameters.AddWithValue("@marks", marks.Items[marks.SelectedIndex].Value.ToString()); cmd.Parameters.AddWithValue("@isMcq", isMcQ); cmd.Parameters.AddWithValue("@faculty_id", faculty.get_faculty_id()); cmd.Parameters.AddWithValue("@faculty_name", faculty.get_faculty_name()); cmd.Parameters.AddWithValue("@subject", faculty.get_subject()); cmd.Parameters.AddWithValue("@branch", faculty.get_branch()); cmd.Parameters.AddWithValue("@semester", faculty.get_semester()); cmd.Parameters.AddWithValue("@year", faculty.get_year()); int rows_affected = cmd.ExecuteNonQuery(); if (rows_affected == 0) { result.Text = "Fail,Please contact developer"; result.ForeColor = System.Drawing.Color.FromName("Red"); } else { result.Text = "Success"; result.ForeColor = System.Drawing.Color.FromName("Green"); } result.Visible = true; result.Enabled = true; question.Text = ""; } catch (Exception ex) { result.Text = ex.ToString(); result.ForeColor = System.Drawing.Color.FromName("Red"); result.Visible = true; result.Enabled = true; } finally { con.Close(); } //to bind gridview after update //try //{ // con.Open(); // string q = "Select id,question_name,marks,mcq FROM questions WHERE faculty_name = '"+faculty.get_faculty_id()+"'"; // SqlDataAdapter ad = new SqlDataAdapter(q, con); // DataTable dt = new DataTable(); // ad.Fill(dt); // if (dt.Rows.Count > 0) // { // GridView1.DataSource = dt; // GridView1.DataBind(); // } //} //catch (Exception ex) //{ //} //finally //{ // con.Close(); //} GridView1.DataSourceID = SqlDataSource1.ID; } }