public string insert_questions(questions q) { string msg = " "; SqlConnection conn = new SqlConnection(connstring); try { SqlCommand cmd = new SqlCommand("[insert_questions]", conn); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add("@q_title", SqlDbType.NVarChar).Value = q.q_title; cmd.Parameters.Add("@q_opa", SqlDbType.NVarChar, 200).Value = q.q_opa; cmd.Parameters.Add("@q_opb", SqlDbType.NVarChar, 200).Value = q.q_opb; cmd.Parameters.Add("@q_opc", SqlDbType.NVarChar, 200).Value = q.q_opc; cmd.Parameters.Add("@q_opd", SqlDbType.NVarChar, 200).Value = q.q_opd; cmd.Parameters.Add("@q_opcorrect", SqlDbType.NVarChar, 200).Value = q.q_opcorrect; cmd.Parameters.Add("@q_addeddate", SqlDbType.NVarChar, 20).Value = q.q_addeddate; cmd.Parameters.Add("@q_fk_ad", SqlDbType.Int).Value = q.q_fk_ad; cmd.Parameters.Add("@q_fk_sub", SqlDbType.Int).Value = q.q_fk_sub; conn.Open(); cmd.ExecuteNonQuery(); msg = "data successfully inserted!"; } catch (Exception) { msg = "data is not successfully inserted!"; } finally { conn.Close(); } return(msg); }
private void button1_Click(object sender, EventArgs e) { questions q = new questions(); q.q_title = textBox1.Text; q.q_opa = textBox2.Text; q.q_opb = textBox3.Text; q.q_opc = textBox4.Text; q.q_opd = textBox5.Text; q.q_opcorrect = textBox6.Text; q.q_addeddate = System.DateTime.Now.ToShortDateString(); q.q_fk_ad = AdminLogin.fk_ad; q.q_fk_sub = comboBox1.SelectedValue.ToString(); insertclass ic = new insertclass(); string msg = ic.insert_questions(q); MessageBox.Show(msg); textbox(); }