0
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................

Edited by __avd: Added [code] tags. Encase your code in: [code] and [/code] tags.

3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by mith_cool
0

>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.

0
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

Edited by __avd: Added [code] tags. Encase your code in: [code] and [/code] tags.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.