Click here to Skip to main content
15,889,595 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am a beginner in c# and need to carry out the following command but
C#
comd.ExecuteNonQuery();

is giving Invalid Operation Exception error.Can you help me resolve this problem.
The select query is perfect.Ihave checked it on mysql.

C#
protected void Button1_Click(object sender, EventArgs e)
    {
        SqlCommand comd = new SqlCommand("select MT.Reg_id,PR.fname+PR.mname+PR.lname AS Name, CONVERT(varchar(10),PR.DOB,104)as[DD.MM.YYYY],PR.F_H_name,PR.adr_local+PR.adr_po+PR.adr_city+PR.adr_state AS Address,PR.Pincode,ED.exam_10,ED.school_10,ED.Board_10,ED.Yr_pass_10,ED.sub_10,ED.Percent_10,ED.Exam_12,ED.school_12,ED.Board_12,ED.Yr_Pass_12,ED.sub_12,ED.Percent_12,ED.Exam_grad,ED.school_grad,ED.Board_grad,ED.Yr_pass_grad,ED.sub_grad,ED.Percent_grad,ED.Exam_post_grad,ED.school_post_grad,ED.Board_post_grad,ED.Yr_pass_post_grad,ED.sub_post_grad,ED.Percent_post_grad,ED.Exam_phd,ED.school_phd,ED.Board_phd,ED.Yr_pass_phd,ED.sub_post_phd,ED.Percent_phd,ED.Exam_other,ED.school_other,ED.Board_other,ED.Yr_pass_other,ED.sub_post_other,ED.Percent_other,MT.submit_date,CT.Csir_emp,CT.Category,CT.ph,CT.Govt_emp,CT.J_K,CT.exservice,CT.women_cat,JN.bond,JN.Min_time,JN.Max_time,EX.Emp_Name_Addr1,EX.Post1,EX.Date_from1,EX.Date_to1,EX.Perm_temp1,EX.Sal_grade1,EX.Emp_Name_Addr2,EX.Post2,EX.Date_from2,EX.Date_to2,EX.Perm_temp2,EX.Sal_grade2,CN.country_visit,CN.Period_from,CN.Period_to,CN.Purpose,EE.prof_train,EE.Res_exp,EE.List_pub,RF.Ref_name1,RF.Desig1,RF.Lab_inst1,RF.Relation1,RF.Ref_name2,RF.Desig2,RF.Lab_inst2,RF.Relation2 FROM Main_Table1 MT INNER JOIN Category1 CT ON MT.Id=CT.Id INNER JOIN Country1 CN ON CT.Id=CN.Id INNER JOIN Experience1 EX ON CT.Id=EX.iD INNER JOIN Extra_EXP1 EE ON CT.Id=EE.Id INNER JOIN Joining1 JN ON CT.Id=JN.Id INNER JOIN List_enclose1 LE ON CT.Id=LE.Id INNER JOIN Personal1 PR ON CT.Id=PR.Id INNER JOIN Education1 ED ON ED.Id=CT.Id INNER JOIN Refrees1 RF ON RF.Id=MT.ID order by reg_id");
        comd.ExecuteNonQuery();
       
        SqlDataAdapter da = new SqlDataAdapter(comd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
Posted

I think your are missing database connection . Without connection You cannot execute your query..
Sample piece of code.

SqlConnection ConADONET=new SqlConnection(ConfigurationManager.
ConnectionStrings["ConnectionStringNameinWebConfig"].ToString());

SqlCommand cmdHotel = new SqlCommand("select * from hotel");
cmdBookHotel.Connection = ConADONET;


ConADONET.Open();
cmdHotel.ExecuteNonQuery();
ConADONET.Close();
 
Share this answer
 
v2
You need an active/open connection for your queries to work, also you need to use MySQLDataAdapter and MySQLCommand to connect to MySql.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900