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
Similar Messages:
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
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
May 27, 2010
protected void Page_Load(object sender, EventArgs e)
View 7 Replies
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
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
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
Feb 21, 2011
what are the best way to handle this error
There is already an open DataReader associated with this Command which must be closed first.
i have created my own conneciton class for the same
is there any best way ,is .net manage this things if i use any inbuild class
View 8 Replies
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
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
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
Mar 23, 2010
how we can serialize a datareader in webservices.
View 1 Replies
Dec 1, 2010
What is the difference between DataSet and DataReader in Asp.net with Example..
View 11 Replies
Mar 20, 2010
How to serialize the DataReader in webservices in asp.net?
View 4 Replies
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
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
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
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
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
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
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
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
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
Dec 25, 2010
why does web services do not support datareader? plz explain it ,it's urgent .
View 3 Replies
Feb 16, 2011
I have the following code
public class ProductDetails
{
private string _productid;
public string ProductID
{
get { return _productid; }
}
private string _description;
public string Description
{
get { return _description; }
}
private string _image;
public string Image
{
get { return _image; }
}
public ProductDetails(string productid, string description, string image)
{
_productid = productid;
_description = description;
_image = image;
}
}
public class DataAccess
{
private static string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
public ProductDetails GetProductDetails()
{
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand("ProductDetails", con);
cmd.CommandType = CommandType.StoredProcedure;
string productid = HttpContext.Current.Request.QueryString["productid"];
cmd.Parameters.Add(new SqlParameter("productid", SqlDbType.NVarChar)).Value = productid;
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
reader.Read();
ProductDetails pro = new ProductDetails(
(string)reader["ProductID"], (string)reader["Description"], (string)reader["Image"]);
reader.Close();
return pro;
}
}
protected void PopulateControls()
private DataAccess da = new DataAccess();
{
ProductDetails product = da.GetProductDetails();
if (product.Description == String.Empty)
{
lblDescription.Visible = false;
}
else
{
lblDescription.Text = product.Description;
}
}
In the last piece i want to check if the Description field is null or empty in order to hide the label control but i get an error if i write: if (product.Description == DBNull.Value) and can only use ring.Empty. But i want to check for NULL values as well..
View 3 Replies