无效的操作异常错误 [英] invalid operation exception error

查看:98
本文介绍了无效的操作异常错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#的初学者,需要执行以下命令,但

 comd.ExecuteNonQuery(); 


正在给出无效的操作异常错误.您能帮我解决此问题吗?
选择查询是完美的.我已经在mysql上检查了它.

 受保护的 无效 Button1_Click(对象发​​件人,EventArgs e)
    {
        SqlCommand comd =  SqlCommand(" )排序;
        comd.ExecuteNonQuery();
       
        SqlDataAdapter da =  SqlDataAdapter(comd);
        DataTable dt =  DataTable();
        da.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    } 

解决方案

我认为您缺少数据库连接.没有连接,您将无法执行查询..
代码样例.

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

SqlCommand cmdHotel =新的SqlCommand(从酒店选择*");
cmdBookHotel.Connection = ConADONET;


ConADONET.Open();
cmdHotel.ExecuteNonQuery();
ConADONET.Close();


您需要一个活动/打开连接才能使查询正常工作,还需要使用MySQLDataAdapterMySQLCommand连接到MySql.

I am a beginner in c# and need to carry out the following command but

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.

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();
    }

解决方案

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();


You need an active/open connection for your queries to work, also you need to use MySQLDataAdapter and MySQLCommand to connect to MySql.


这篇关于无效的操作异常错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆