does anyone konw why is this code giving an error saying:
There is already an open DataReader associated with this Command which must be closed first.
the error is coming form the place where i have cold the line

the code is below:

C# Syntax

public String[] LoadUSEmp()
        {
            String firstName;
            String lastName;
            int NoOfRows;
           

            db.openConnection();
            String query = @"Select FirstName,LastName From Employee Where Citizenship = 'US' Order by FirstName ";
            SqlCommand comm = new SqlCommand(query, DB.getConnection());
            SqlDataAdapter da1 = new SqlDataAdapter(comm);
            DataTable dt1 = new DataTable();

           [B] da1.Fill(dt1);[/B]

            NoOfRows = dt1.Rows.Count;
            String[] fullName = new String[NoOfRows];
            
            for (int i = 0; i < dt1.Rows.Count; i++)
            {
                firstName = dt1.Rows[i]["FirstName"].ToString();
                lastName = dt1.Rows[i]["LastName"].ToString();
                fullName[i] = firstName + " " + lastName;

            }
            comm.Dispose();
            db.closeConnection();
            return fullName;
        }

i need help thanxxxxxxxxxxx

Recommended Answers

All 9 Replies

add the following to your SqlConnection string

MultipleActiveResultSet=True

So it would look something Like this:

SqlConnection sqlCon = new SqlConnection(@"SQlSERVER\DB;Initial Catalog = myDB;MultipleActiveResultSet=True;User Id=finito;Password=pass123");

hey finito,
i have created the database connection in another class if i add this " MultipleActiveResultSet=True " errors comes in the places i have created instances from the DB connection class

what errors?

typeInitialization Exception this was the error

typeInitialization Exception this was the error

Is this error being shown ion the SqlCon line?
All I asked you to do is let Sql get more than one Result on a single connection. I don't see the problem, Try and make a new connection on this page just to test and see if it works.

hello everybody, I have error with dataset. if anybody, plz send to me solution. thanks
my error da.Fill(ds, "dep"); error number :40
class1.cs

using System;
using System.Data.SqlClient;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WindowsFormsApplication1
{
    class Class1
    {
        public string constr="Server=(local)/SQLEXPRESS;DataBase=POS;Integrated Security=SSPI";
        public SqlConnection cnn = new SqlConnection("Server=(local)/SQLEXPRESS;DataBase=POS;Integrated Security=SSPI");
        //Data Source=LINLINPC\SQLEXPRESS;Initial Catalog=POS;Integrated Security=True

        public SqlCommand scm = new SqlCommand();
        public SqlCommand icmd = new SqlCommand();
        public SqlCommand ucmd = new SqlCommand();
        public SqlCommand dcmd = new SqlCommand();
        public SqlDataAdapter da = new SqlDataAdapter();
        public DataSet ds=new DataSet();            
        
        //public SqlConnection Cnn = new SqlConnection(ConfigurationException.["SSConstring"].ConnectionString.ToString());
        //public SqlDataAdapter da = new SqlDataAdapter(); 
        //cnn
        
    
        public Class1()
        {
                cnn.ConnectionString = constr;            
                scm = cnn.CreateCommand();
                da.SelectCommand = scm;
        }

    }


}


form1.cs

using System;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {

        Class1 gp = new Class1();
        SqlConnection con = new SqlConnection();
        DataSet ds = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter();
        SqlCommand scmd = new SqlCommand();
        
       
        
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                con.ConnectionString = gp.constr;

                da.SelectCommand = scmd;
                scmd.Connection = con;             
               
                scmd.CommandText = "select *" + "from departmenttable";
                
                da.Fill(ds, "dep");

                dataGridView1.DataSource = ds.Tables["dep"];                
                dataGridView1.Columns[0].Visible = false;
                toolStripComboBox1.Items.Add("DepartmentID");
                toolStripComboBox1.SelectedIndex = 0;
            }
            catch (Exception ec)
            {

                MessageBox.Show("Connection Error" + ec.Message);
            }


        }
    }
}

do u think this code is right

public string constr = "Server=LINLINPC/SQLEXPRESS;" + "DataBase=POS;" + "Integrated Security=SSPI";
public SqlConnection cnn = new SqlConnection();


or
public string constr = "Data Source=LINLINPC/SQLEXPRESS;Initial Catalog=POS;Integrated Security=True";
public SqlConnection cnn = new SqlConnection();
by using c#;

public string constr = "Data Source=LINLINPC/SQLEXPRESS;Initial Catalog=POS;Integrated Security=True";
public SqlConnection cnn = new SqlConnection();

Where did u define SqlConnection

public SqlConnection cnn = new SqlConnection(constr);

as for your earlier question what is System.Linq ?which version are you using?

if solved please mark as solved

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.