Click here to Skip to main content
14,932,737 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
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
Posted
Updated 25-Jul-18 0:12am
Comments
Jochen Arndt 19-Jul-18 3:34am
   
Would be good to know which function call throws the execption.

It might be con.Open(). Then check your connection string.

It might be also later when using the SqlDataReader object returned by cmd.ExecuteReader(). But you did not show the code using that.
F-ES Sitecore 19-Jul-18 5:15am
   
Whenever you get an error always say what line the error occurs on.

Just wanted to add something to what they've suggested. Here are a few points that you want to consider:

(1) When binding a Data 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 put objects that eat resources such as SqlConnection, SqlCommand and SqlDataAdapter within a using statement to ensure that objects will be properly disposed and closed after they are used.


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


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

C#
SqlCommand cmd = new SqlCommand("select * from Expense_Type", con);
   
Comments
Vincent Maverick Durano 19-Jul-18 9:33am
   
5ed. Great catch!
   
Thnx, it worked.. corrected code is /* 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);
   
SqlCommand cmd = new SqlCommand("select * from Expense_Type", con);


you have missed connection instance, Please check above line. it should work
   
Comments
Richard Deeming 25-Jul-18 9:57am
   
As mentioned last week in solution 1.

And again earlier today in solution 3.

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