|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
How to pass dataReader objectin a client application. I get the following exception "Invalid attempt to read when no data is present." Question: What is the correct way to pass a dataReader? Any Good examples please. thanks public SqlDataReader GetCust(string CustId) { try { if (sqlConn.State == ConnectionState.Closed) sqlConn.Open(); SqlCommand sqlCmd = new SqlCommand("SELECT * FROM dbo.ListCustomers (@CustId)", this.sqlConn); sqlCmd.CommandType = CommandType.Text; SqlParameter p0 = sqlCmd.Parameters.Add("@CustId", SqlDbType.VarChar, 3); p0.Value = CustId; SqlDataReader sqlRead = sqlCmd.ExecuteReader(CommandBehavior.CloseConnection); return sqlRead; } catch(Exception exep) { Console.WriteLine("An error occurred " + exep.Message.ToString()); return null; } } //client static void Main(string[] args) { UserDefinedFunction.UDFSampler udf = new UDFSampler(); SqlDataReader sRead = udf.GetCust("xyz"); while (sRead.Read()) { //do something with record } sRead.Close(); } Hi,
You have an incorrect SQL statement. What are you trying to achieve with this SQL statement? Following syntax is not correct, because @CustId parameter does not belong to any condition Show quoteHide quote "Mori" <M***@discussions.microsoft.com> wrote in message news:0682863C-87EF-4B79-A9E4-F046B16092A7@microsoft.com... >I am trying to pass a datareader object and then iterate through the reader > in a client application. I get the following exception "Invalid attempt > to > read when no data is present." > > Question: What is the correct way to pass a dataReader? Any Good examples > please. thanks > > public SqlDataReader GetCust(string CustId) > { > try > { > if (sqlConn.State == ConnectionState.Closed) > sqlConn.Open(); > SqlCommand sqlCmd = new SqlCommand("SELECT * FROM dbo.ListCustomers > (@CustId)", this.sqlConn); > sqlCmd.CommandType = CommandType.Text; > SqlParameter p0 = sqlCmd.Parameters.Add("@CustId", SqlDbType.VarChar, 3); > p0.Value = CustId; > > SqlDataReader sqlRead = > sqlCmd.ExecuteReader(CommandBehavior.CloseConnection); > return sqlRead; > } > catch(Exception exep) > { > Console.WriteLine("An error occurred " + exep.Message.ToString()); > return null; > } > } > > > //client > static void Main(string[] args) > { > UserDefinedFunction.UDFSampler udf = new UDFSampler(); > SqlDataReader sRead = udf.GetCust("xyz"); > while (sRead.Read()) > { > //do something with record > } > sRead.Close(); > > } > > I am assuming that dbo.ListCustomers is a SQL Server user defined function
.... if so, have you tried running the SQL statement inside of query analyzer to make sure it returns rows when you pass it a parameter? // John Papa // http://codebetter.com/blogs/john.papa Show quoteHide quote "Mori" wrote: > I am trying to pass a datareader object and then iterate through the reader > in a client application. I get the following exception "Invalid attempt to > read when no data is present." > > Question: What is the correct way to pass a dataReader? Any Good examples > please. thanks > > public SqlDataReader GetCust(string CustId) > { > try > { > if (sqlConn.State == ConnectionState.Closed) > sqlConn.Open(); > SqlCommand sqlCmd = new SqlCommand("SELECT * FROM dbo.ListCustomers > (@CustId)", this.sqlConn); > sqlCmd.CommandType = CommandType.Text; > SqlParameter p0 = sqlCmd.Parameters.Add("@CustId", SqlDbType.VarChar, 3); > p0.Value = CustId; > > SqlDataReader sqlRead = > sqlCmd.ExecuteReader(CommandBehavior.CloseConnection); > return sqlRead; > } > catch(Exception exep) > { > Console.WriteLine("An error occurred " + exep.Message.ToString()); > return null; > } > } > > > //client > static void Main(string[] args) > { > UserDefinedFunction.UDFSampler udf = new UDFSampler(); > SqlDataReader sRead = udf.GetCust("xyz"); > while (sRead.Read()) > { > //do something with record > } > sRead.Close(); > > } > > The sql string works in query analyzer. It also works when I use a
sqlAdapter and data table to return the data, but not when I use a data deader. Why? Please help. Show quoteHide quote "John Papa" wrote: > I am assuming that dbo.ListCustomers is a SQL Server user defined function > ... if so, have you tried running the SQL statement inside of query analyzer > to make sure it returns rows when you pass it a parameter? > > // John Papa > // http://codebetter.com/blogs/john.papa > > "Mori" wrote: > > > I am trying to pass a datareader object and then iterate through the reader > > in a client application. I get the following exception "Invalid attempt to > > read when no data is present." > > > > Question: What is the correct way to pass a dataReader? Any Good examples > > please. thanks > > > > public SqlDataReader GetCust(string CustId) > > { > > try > > { > > if (sqlConn.State == ConnectionState.Closed) > > sqlConn.Open(); > > SqlCommand sqlCmd = new SqlCommand("SELECT * FROM dbo.ListCustomers > > (@CustId)", this.sqlConn); > > sqlCmd.CommandType = CommandType.Text; > > SqlParameter p0 = sqlCmd.Parameters.Add("@CustId", SqlDbType.VarChar, 3); > > p0.Value = CustId; > > > > SqlDataReader sqlRead = > > sqlCmd.ExecuteReader(CommandBehavior.CloseConnection); > > return sqlRead; > > } > > catch(Exception exep) > > { > > Console.WriteLine("An error occurred " + exep.Message.ToString()); > > return null; > > } > > } > > > > > > //client > > static void Main(string[] args) > > { > > UserDefinedFunction.UDFSampler udf = new UDFSampler(); > > SqlDataReader sRead = udf.GetCust("xyz"); > > while (sRead.Read()) > > { > > //do something with record > > } > > sRead.Close(); > > > > } > > > > My 2 cents - don't be passin' around open data readers. That encourages bad
design - open connections, left open too long or just never closed. - Sahil Malik [MVP] http://codebetter.com/blogs/sahil.malik/ Show quoteHide quote "Mori" <M***@discussions.microsoft.com> wrote in message news:0682863C-87EF-4B79-A9E4-F046B16092A7@microsoft.com... > I am trying to pass a datareader object and then iterate through the reader > in a client application. I get the following exception "Invalid attempt to > read when no data is present." > > Question: What is the correct way to pass a dataReader? Any Good examples > please. thanks > > public SqlDataReader GetCust(string CustId) > { > try > { > if (sqlConn.State == ConnectionState.Closed) > sqlConn.Open(); > SqlCommand sqlCmd = new SqlCommand("SELECT * FROM dbo.ListCustomers > (@CustId)", this.sqlConn); > sqlCmd.CommandType = CommandType.Text; > SqlParameter p0 = sqlCmd.Parameters.Add("@CustId", SqlDbType.VarChar, 3); > p0.Value = CustId; > > SqlDataReader sqlRead = > sqlCmd.ExecuteReader(CommandBehavior.CloseConnection); > return sqlRead; > } > catch(Exception exep) > { > Console.WriteLine("An error occurred " + exep.Message.ToString()); > return null; > } > } > > > //client > static void Main(string[] args) > { > UserDefinedFunction.UDFSampler udf = new UDFSampler(); > SqlDataReader sRead = udf.GetCust("xyz"); > while (sRead.Read()) > { > //do something with record > } > sRead.Close(); > > } > > 1) I agree with Mr. Malik. Don't pass a DataReader around...
2) Turn on the profiler to see what's getting executed. -- Show quoteHide quote____________________________________ William (Bill) Vaughn Author, Mentor, Consultant Microsoft MVP www.betav.com/blog/billva www.betav.com Please reply only to the newsgroup so that others can benefit. This posting is provided "AS IS" with no warranties, and confers no rights. __________________________________ "Mori" <M***@discussions.microsoft.com> wrote in message news:0682863C-87EF-4B79-A9E4-F046B16092A7@microsoft.com... >I am trying to pass a datareader object and then iterate through the reader > in a client application. I get the following exception "Invalid attempt > to > read when no data is present." > > Question: What is the correct way to pass a dataReader? Any Good examples > please. thanks > > public SqlDataReader GetCust(string CustId) > { > try > { > if (sqlConn.State == ConnectionState.Closed) > sqlConn.Open(); > SqlCommand sqlCmd = new SqlCommand("SELECT * FROM dbo.ListCustomers > (@CustId)", this.sqlConn); > sqlCmd.CommandType = CommandType.Text; > SqlParameter p0 = sqlCmd.Parameters.Add("@CustId", SqlDbType.VarChar, 3); > p0.Value = CustId; > > SqlDataReader sqlRead = > sqlCmd.ExecuteReader(CommandBehavior.CloseConnection); > return sqlRead; > } > catch(Exception exep) > { > Console.WriteLine("An error occurred " + exep.Message.ToString()); > return null; > } > } > > > //client > static void Main(string[] args) > { > UserDefinedFunction.UDFSampler udf = new UDFSampler(); > SqlDataReader sRead = udf.GetCust("xyz"); > while (sRead.Read()) > { > //do something with record > } > sRead.Close(); > > } > >
Other interesting topics
How to look at parameter string
disconnected typed dataset DataAdapter Update Does Nothing ADO error "There is already an open DataReader associated with this Connection" SQLHelper.ExecuteReader - Connection Close Can't Read Excel File (OleDb) w/ ASP.NET Impersonation Oracle Data Providers (Connection Pooling & Transactions) Importing Excel data to Access Filling multiple tables Simple asynchronous method |
|||||||||||||||||||||||