C# - DataReader Already Open When Using LINQ?

Apr 22, 2010

I've got a static class containing a static field which makes reference to a wrapper object of a DataContext.

The DataContext is basically generated by Visual Studio when we created a dbml file & contains methods for each of the stored procedures we have in the DB.

Our class basically has a bunch of static methods that fire off each of these stored proc methods & then returns an array based on a LINQ query.

Example:

public static TwoFieldBarData[] GetAgesReportData(string pct)
{
return DataContext
.BreakdownOfUsersByAge(Constants.USER_MEDICAL_PROFILE_KEY, pct)
.Select(x => new TwoFieldBarData(x.DisplayName, x.LeftValue, x.RightValue, x.TotalCount))
.ToArray();
}

Every now and then, we get the following error:

There is already an open DataReader associated with this Command which must be closed firs

This is happening intermittently and I'm curious as to what is going on. My guess is that when there's some lag between one method executing and the next one firing, it's locking up the DataContext and throwing the error.

Could this be a case for wrapping each of the DataContext LINQ calls in a lock(){} to obtain exclusivity to that type and ensure other requests are queued?

View 3 Replies


Similar Messages:

C# - Already An Open DataReader Associated With This Command - When I'm Not Using Datareader

Jul 28, 2010

I am getting an error that an open DataReader associated with this Command, when I'm not using datareader(though probably executereader() is the same thing) how would I close this if I don't have a datareader present?

using (SqlConnection conn = new SqlConnection(ConnectionString))
{
SqlCommand cmd = new SqlCommand("spSelectAllTypes",conn);
cmd.CommandType = CommandType.StoredProcedure;
[code]...

I just want to be able to databind a bunch of dropdownlist in one open connection. (before I had multiple open and closes for each control)

View 2 Replies

ADO.NET :: There Is Already An Open DataReader Associated With This Command Which Must Be Closed First

Aug 27, 2010

when i insert record ,There is already an open DataReader associated with this Command which must be closed first.

SqlConnection conn = new SqlConnection(@"Data Source=LocalhostSQLEXPRESS;Initial Catalog=StudentDetails;Integrated Security=True"); [code]...

View 6 Replies

DataReader Already Open Error When Trying To Run Two Queries

Nov 10, 2010

I have a couple of queries that I need to run one to a linked server and one not like this

Dim InvestorLookup As String = "DECLARE @investor varchar(10), @linkedserver varchar(25), @sql varchar(1000) "
InvestorLookup += "SELECT @investor = '" & investor & "', @linkedserver = '" & db & "', "
InvestorLookup += "@sql = 'SELECT * FROM OPENQUERY(' +@linkedserver + ', ''SELECT * FROM db WHERE investor = ' + @investor + ' '')' EXEC(@sql)"
Dim queryInvestorLookup As SqlCommand = New SqlCommand(InvestorLookup , conn)
Dim BondNoDR As SqlDataReader = queryInvestorLookup.ExecuteReader()
Dim PasswordCheck As String = "DECLARE @investor varchar(10), @password varchar(20), @linkedserver varchar(25), @sql varchar(1000) "
PasswordCheck += "SELECT @investor = '" + investor + "', @password = '" + password + "', @server = '" + db2 + "', "
PasswordCheck += "@sql = 'SELECT * FROM @server WHERE investor = @investor AND password = ' + @password + ' '' EXEC(@sql)"
Dim queryPasswordCheck As SqlCommand = New SqlCommand(PasswordCheck, conn)
Dim PasswordDR As SqlDataReader = queryPasswordCheck.ExecuteReader()

As far as I can tell from debugging the queries both run as they should but I get the error There is already an open DataReader associated with this Command which must be closed first. Is it possible to run two queries in two different DataReaders. I need to later reference each DataReader and select values from each.

View 3 Replies

ADO.NET :: Convert Procedure With Datareader To LINQ?

Aug 3, 2010

i have this procedure:

[Code]....

the difficulties are the follows:

1)i cannot know how to execute stored procedure with LINQ

2)how can i set value to my properties after a selection?

3)how can i set optional parameters in a stored procedure by LINQ?

View 1 Replies

WCF / ASMX :: There Is Already An Open DataReader Associated With This Command Needs To Be Closed

Nov 26, 2010

My scenario:I am using a web service which access the sql server database to provide an XML output. this part is working fine. I am using a SqlDataAdapter and DataTable with Fill method to get the data.

Problem:When more than one person access this web service at the same time i get the error message which is given in the subject line.

I understand the problem is because of the sql connection which is still open by the first user. If the first user completes his operation before the second users request then both the user receives the correct response.

My Solution:What i did was i initiated 10 connection strings and will check which connection string is closed and i have used this connection for the second persons request if they both access at the same time.

View 6 Replies

DataSource Controls :: There Is Already An Open DataReader Associated With This Command Which Must Be Closed First.

May 27, 2010

protected void Page_Load(object sender, EventArgs e)

View 7 Replies

Access :: How To Avoid "There Is Already Open Datareader Associated With This Command Which Muct Be Closed

Jul 21, 2010

i am getting this error "There is already open Datareader associated with this command which muct be closed first"

what are the chances of getting this error becoz it giving me so much problem

and how can i solve this error by writing a well code

View 3 Replies

DataSource Controls :: MARS - "There Is Already An Open DataReader Associated With This Command Which Must Be Closed "

Feb 23, 2010

I am using MS SQL Server 2005 and i added "" to my connection string and i still have the problem

"There is already an open DataReader associated with this Command which must be closed" ;

SqlDataReader dr2;
StrSql = " SELECT * from Modules";
cmd.Connection = conn;
cmd.CommandText = StrSql; [code]....

View 1 Replies

Datareader Vs Dataset - Get The Number Of Rows From The Datareader?

Nov 15, 2010

I'm having a method that exports content from the database to excel files. The method taks as paramaters a DataReader param and a int param - the number of rows. For the number of rows i'm using a dataset, wich i fill using the same query as for the datareader. So I'm executing it twice... Is there a way I can avoid that? get the number of rows from the datareader?

View 1 Replies

Open Source LINQ Search Engine For Website?

Apr 22, 2010

I want to add a search engine to my website. I want it to handler boolean searches and give me a list of results in order or best match. I need it to be able to work with LINQ, because I want to add additional where clauses to the final query that gets run. I am looking for the best open source .NET search engine that works with LINQ. I like lucene.net but the problem is the LINQ interface (LINQ to Lucene) hasn't been updated since 2008. Are there any good options out there?

View 3 Replies

.net - Going Through A DataReader Twice?

Mar 30, 2011

I have the following code which does what it's supposed to do:

objSQLCommand = New SqlCommand("select * from table1", objSQLConnection)
objSQLCommand.Connection.Open()
objSQLDataReader = objSQLCommand.ExecuteReader()[code]....

But I need to loop through the objSQLDataReader 1 more time. How would I do that?

View 2 Replies

How Can Serialize A Datareader In Webservices

Mar 23, 2010

how we can serialize a datareader in webservices.

View 1 Replies

What Is The Difference Between DataSet And DataReader

Dec 1, 2010

What is the difference between DataSet and DataReader in Asp.net with Example..

View 11 Replies

How To Serialize The DataReader In Webservices

Mar 20, 2010

How to serialize the DataReader in webservices in asp.net?

View 4 Replies

ADO.NET :: Fetching A Single Row From Sql Datareader?

Aug 17, 2010

Lang-ASP.net using C# and Sql server 2005

i m trying to use the sql datareader to fetch a single row at a time i wanna know can we use it to fetch a single row at a time or not if yes i m unable to figure out what i m doing wrong here bcus its not returning any data

[Code]....

of course data is present in my table bcuz when i m using dataset i m able to the return the single row throuugh dataset

View 3 Replies

ADO.NET :: Converting Datareader To A List?

Feb 17, 2011

In C#.I have a list<object>.

I want to convert any datareader result (with data) to a list of objects.

How can I do that?

View 6 Replies

ADO.NET :: Can't Pass DataReader Values

Nov 1, 2010

Why can I not call the global variables CatCode and CompanyName which are equated in the first procedure GetCompanyCatID And use their new values in the second procedure UpdateCompanyCounts as parameter values. When I hard code them code their values at the end of the GetCompany CatID procedure the following procedure updates properly.

string CatCode; - delcared global
string CompanyName; - delcared global
protected void GetCompanyCatID()
{
LinkID2 = Request.QueryString["linkid"].ToString();
string L_ID = LinkID2;
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString001"].ConnectionString);
SqlCommand cmd = new SqlCommand("GetCID", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@L_ID", L_ID);
try
{
con.Open();
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
if (reader["CG_ID"] != System.DBNull.Value)
CatCode = (string)reader["CG_ID"];
CompanyName = (string)reader["CG_Name"];
}
}
}
catch (SqlException exc)
{ }
finally
{ con.Close(); }
// ******* HARD CODED to test below update procedure, it works. When commented out the following update procedure does not update.
CatCode = "55555"
CompanyName = "Test";
UpdateCompanyCounts();
}
protected void UpdateCompanyCounts()
{
LinkID2 = Request.QueryString["linkid"].ToString();
string L_ID = LinkID2;
string CatCode2 = CatCode;
string CompanyName2 = CompanyName;
// string CatCode2 = "23456";
// string CompanyName2 = "TEST";
SqlConnection con2 = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString001"].ConnectionString);
SqlCommand cmd2 = new SqlCommand("UpdateCatCounter", con2);
cmd2.CommandType = CommandType.StoredProcedure;
cmd2.Parameters.AddWithValue("@L_ID", LinkID2);
cmd2.Parameters.AddWithValue("@CG_ID", CatCode2 );
cmd2.Parameters.AddWithValue("@CG_Name", CompanyName2 );
try
{
con2.Open();
cmd2.ExecuteNonQuery();
}
catch (SqlException exc)
{
// ErrorBox.Text += string.Format("Error: {0}<br/>", exc.Message);
}
finally
{
con2.Close();
}
}

View 5 Replies

.net DataReader Execution Technique

Jan 3, 2010

I heard that DataReader works on forward only readonly fashion and at a time it willread a single record.Suppose when i execute the below code

SqlDataReader reader=cmd.ExecuteReader();
gv1.DataSource=reader;
gv.DataBind();

How the does the gridview populate all records?.As the reader is capable of reading one row per read,I thought only the last row is available for GridView to display.

View 3 Replies

C# - Check A Datareader For Null?

Feb 10, 2011

I have an aspx page which allows a user to submit modified entries into the database, but when the user clicks Submit to fire the stored procedure I want to first run a check to see if a modified row with the same relationship exists.I am passing the results of the following query:

SELECT SwitchRoom.ModifiedID FROM SwitchRoom WHERE
SwitchRoomID = @ChkSwitchRmID", constring;

into a DataReader to determine if that relationship exists.I need it to determine whether the reader returns NULL to allow the procedure to execute, if it doesn't, then don't allow the user to save the information.I've tried the following:

if (dbreader = NULL)
{
Fire Procedure
}
else
{
"Error Message"
}

and I've even tried passing the reader into a datatable and running it against that without any luck.

View 5 Replies

DataSource Controls :: Use A DataReader With SQL?

Mar 29, 2011

I have put a SQLDataSource on my page and everything is adjusted and it works properly. I read an article
here which was indicating that you can use a DataReader in order to get data from sqldatasource, but it doesn't say how, the only thing that is said is to use DataSourceMode property in order to adjust it for a DataReader.

View 1 Replies

Filling Datatable Using Datareader?

Nov 3, 2010

see i want to fill datatable using datareader.

i have created object like this

SqlDataReader dr=cmd.ExecuteReader();
if(dr.HasRows)
{
}

View 2 Replies

C# - Filling An Arraylist From A Datareader?

Mar 15, 2011

I have a problem with how to fill an array list from a data reader

string queryDTL = " SELECT * FROM tbl1 ";
connection.Connect();
cmd = new OracleCommand(queryDTL, connection.getConnection());
dr_DTL = qcmd2.ExecuteReader();
ArrayList RecordsInfo = new ArrayList();
while (dr_DTL.Read())
{
RecordsInfo = dr_DTL["number"].ToString();
}

The problem is the datareader contain alot of info other than the number but I don't know how to put them in their correct position.

View 3 Replies

String Format Date Using Datareader?

Jan 28, 2010

I am using this line to pull out the date from a datareader linked to an sql statement.

PHP Code:
lbl_wall.Text = "<span style='font-size:12px;color:#555; font-style:italic'>Member since - " + String.Format("{0:ddd, MMM d, yyyy}", dbReaderPr["date_added"].ToString()) +"</span>";

The date format should be like:-

Quote:
"Sun, Mar 9, 2008"

But it appears like this:-

Quote:
"1/28/2010 8:00:16 AM"

How can i fix this?

View 3 Replies

WCF / ASMX :: Web Services Do Not Support Datareader?

Dec 25, 2010

why does web services do not support datareader? plz explain it ,it's urgent .

View 3 Replies







Copyrights 2005-15 www.BigResource.com, All rights reserved