public partial class QuestionPage : System.Web.UI.Page
{
    string scon = "Data Source=RBWORKSTATION2\\SQLEXPRESS;Initial Catalog=onlineexamdb;Integrated Security=True;MultipleActiveResultSets=true";
    SqlDataAdapter da;
    SqlCommand cmd;
    DataSet ds;
    SqlConnection con;
    ArrayList myArrayList = new ArrayList();
    public String ques, option1,option2,option3,option4;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SqlConnection con = new SqlConnection(scon);
            con.Open();
            da = new SqlDataAdapter("select Question,Option1,Option2,Option3,Option4 from QuestionData", con);
            ds = new DataSet();
            da.Fill(ds);
        }
        
   }
    protected void SubmitButton_Click1(object sender, EventArgs e)
    {
         con = new SqlConnection(scon);
        con.Open();
              try
        {
            cmd = new SqlCommand("insert into Table_1 values(@Question,@Option1,@Option2,@Option3,@Option4) ", con);
            cmd.Parameters.AddWithValue("@Question", SqlDbType.VarChar).Value = ques;
            cmd.Parameters.AddWithValue("@Option1", SqlDbType.Variant).Value = option1;
            cmd.Parameters.AddWithValue("@Option2", SqlDbType.Variant).Value = option2;
            cmd.Parameters.AddWithValue("@Option3", SqlDbType.Variant).Value = option3;
            cmd.Parameters.AddWithValue("@Option4", SqlDbType.Variant).Value = option4;
          cmd.ExecuteNonQuery();
          // cmd.ExecuteScalar();
            
            //Response.Write("<script>alert('Your record is insert successfuly')</script>");
        }
        finally
        {

            if (con != null)
            {
                con.Close();

            }
        }
       
        
    }

 protected void fv_ItemInserted(object sender, FormViewInsertedEventArgs e)
    {
        try
        {
            ques = (fv.Row.FindControl("QuestionLabel") as Label).Text;
            option1 = (fv.FindControl("CheckBox13") as CheckBox).Text;
            option2 = (fv.FindControl("CheckBox14") as CheckBox).Text;
            option3 = (fv.FindControl("CheckBox15") as CheckBox).Text;
            option4 = (fv.FindControl("CheckBox16") as CheckBox).Text;
        }
        finally
        {

            if (con != null)
            {
                con.Close();

            }
        }
    }

I am getting error on line cmd.ExecuteNonQuery();
I can not insert my data in database
please please help me................

Recommended Answers

All 2 Replies

>I am getting error on line cmd.ExecuteNonQuery()

Do not handle event for child controls. Child control automatically bubble-up the event to their parent. Please read this tutorial.

cmd.ExecuteNonQuery should be replaced by 
cmd.ExecuteReader();

i just have an example hope you understand this

cn.Open();
        SqlDataAdapter da = new SqlDataAdapter("select * from register", cn);
        SqlCommandBuilder cmd = new SqlCommandBuilder(da);
        DataSet ds = new DataSet("register");
        da.Fill(ds,"register");
        DataRow dr = ds.Tables["register"].NewRow();
        dr["firstname"] = txtfname.Text;
        dr["lastname"] = txtlname.Text;
        dr["loginname"] = txtdesiredlname.Text;
        dr["password"] = txtpass.Text;
        dr["securityq"] = Ddlsecq.Text;
        dr["securitya"] = txtsecans.Text;
        dr["email"] = txtemail.Text;
        dr["location"] = Ddllocation.Text;
        dr["wordverification"] = txtverify.Text;
        ds.Tables["register"].Rows.Add(dr);
        da.Update(ds, "register");
        cn.Close();

txt is the textboxes

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.