C#中的Invalidoperationexception错误 [英] Invalidoperationexception error in C#
本文介绍了C#中的Invalidoperationexception错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
InvalidOperationException error in C#
我的代码:
My code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace Expense_System
{
public partial class Transactions : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
String strConnString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
con.Open();
SqlCommand cmd = new SqlCommand("select * from Expense_Type");
DropDownListexpensetype.DataSource = cmd.ExecuteReader();
DropDownListexpensetype.DataTextField = "Expense_Type";
DropDownListexpensetype.DataValueField = "Expense_Type";
DropDownListexpensetype.DataBind();
}
protected void BtncreateExpense_Click(object sender, EventArgs e)
{
}
}
}
我的尝试:
谷歌搜索它但没有得到正确的解决方案
What I have tried:
Googled it but not getting proper solution
推荐答案
只想添加一些东西给他们有人建议。以下是您要考虑的几点:
(1)手动绑定数据控件
时Page_Load事件,请确保在中包装代码以进行绑定!IsPostback
阻止以避免意外结果。
(2)养成使用SqlConnection
,SqlCommand等资源的
和对象
的习惯SqlDataAdapter
在使用语句
中,以确保在以后正确处理和关闭对象他们被使用。
Just wanted to add something to what they've suggested. Here are a few points that you want to consider:
(1) When binding aData Control
manually at Page_Load event, make sure that you wrap your code for binding within!IsPostback
block to avoid unexpected results.
(2) Make it a habit to putobjects
that eat resources such asSqlConnection
,SqlCommand
andSqlDataAdapter
within ausing statement
to ensure that objects will be properly disposed and closed after they are used.
protected void Page_Load(object sender, EventArgs e){
if(!IsPostback){
BindList();
}
}
private void BindList(){
String strConnString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
using(SqlConnection con = new SqlConnection(strConnString)){
using(SqlCommand cmd = new SqlCommand("select * from Expense_Type", con)){
con.Open();
DropDownListexpensetype.DataSource = cmd.ExecuteReader();
DropDownListexpensetype.DataTextField = "Expense_Type";
DropDownListexpensetype.DataValueField = "Expense_Type";
DropDownListexpensetype.DataBind();
}
}
}
注意:我注意到你已经分配了字段 Expense_Type
适用于文本
和值
。只需确保 DataValueField
包含唯一值,以避免在从列表中选择/预选值时出现意外行为。
Note: I've noticed that you've assigned the field Expense_Type
for both Text
and Value
. Just make sure that the DataValueField
contain unique values to avoid unexpected behavior when selecting/pre-selecting values from the list.
您可以从谷歌获得正确的解决方案,因为你不知道哪一行代码导致了这个错误。该错误非常通用。并且,还有更多的错误信息,所以你也需要分享它。
但是,我可以告诉你一个问题。您没有将SqlCommand与连接相关联。你可以这样做:
You can't get the proper solution from google because you don't know which line of code is causing that error. The error is pretty generic. AND, there is more to the error message so you need to share that too.
However, I can tell you one problem. You did not associate the SqlCommand with a connection. You can do this:
SqlCommand cmd = new SqlCommand("select * from Expense_Type", con);
问题已得到纠正。
更正后的代码部分是:
Issue has been rectified.
Corrected code part is:
SqlCommand cmd = new SqlCommand("select * from Expense_Type", con);
这篇关于C#中的Invalidoperationexception错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文