Click to See Complete Forum and Search --> : c# DataReader Problem


HWY909
03-01-2011, 05:22 PM
Hello, I recently upgraded my /net program to 2010 and SQL 2008 and my datareaders stopped working. Everything else works as it always did. Is my syntax out of date? The stored procedure works fine in Management Studio. Here's one of them: SqlCommand cmdBudgetList = new SqlCommand("stpBudget", cs); cmdBudgetList.CommandType = CommandType.StoredProcedure; SqlParameter bScenRunID = new SqlParameter("@ScenarioRunID", SqlDbType.Int); bScenRunID.Value = Session["ScenarioRunID"]; cmdBudgetList.Parameters.Add(bScenRunID); SqlParameter bYear = new SqlParameter("@Year", SqlDbType.Int); bYear.Value = Year; cmdBudgetList.Parameters.Add(bYear); SqlParameter bSYear = new SqlParameter("@StartYear", SqlDbType.Int); bSYear.Value = StartYear; cmdBudgetList.Parameters.Add(bSYear); SqlDataReader rBudget = cmdBudgetList.ExecuteReader();

The stp is USE [datafile] GO /****** Object: StoredProcedure [dbo].[stpBudget] Script Date: 02/27/2011 19:40:21 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER PROCEDURE [dbo].[stpBudget] @ScenarioRunID as int, @Year as int, @StartYear as int AS SET NOCOUNT ON; SELECT * FROM ScenarioBudget WHERE ScenarioBudget.ScenarioRunID= @ScenarioRunID AND ScenarioBudget.[Year]= @Year - @StartYear + 1; and the database is at (pasted from web.config) Connection string is SqlConnection cs = new SqlConnection(ConfigurationManager.ConnectionStrings["SGRConnectionString"].ConnectionString)

Optional Information:
Computer OS: Windows 7
Browser: Firefox

Already Tried:
Everything I could think of except downgrading all my software, which would destroy my other projects. BTW, the DataReaders just give empty sets when run in VS.Net (although not in SQL Studio).

Ribeyed
03-02-2011, 03:52 AM
Hi,

DataReader is obsolete. When you open up your 2008 solution in 2010 it asks you to convert your solution. I'm can't rememeber of the top of my head if it asks you which framework to target however your problem is relating to your solution now targeting the wrong framework.

there is 2 ways to check and change the target framework. Open your solution, in the solution Explorer right click and go to properties. You should see a dropdownlist "Target Framework". You need to set that to the framework you where originally targeting.

You can also check your web.config file in the <system.web> section there will be <compilation> tage with an attribute specifying the target framework.


regards


Ribs

ryanbutler
03-02-2011, 09:02 AM
DataReader isn't obsolete. It's probably just a problem w/ the conversion of your application. Make sure you converted the .csproj file through VS 2010.

Ribeyed
03-02-2011, 09:13 AM
My appology, read the question as a datarepeater when he was asking about datareader please ignore my above post.

regards


Ribs

Ribeyed
03-02-2011, 09:34 AM
Hi,

I think it is syntax due to changes in the target frame work. None of your parameters have a property setting the parameterDirection.



// Add the input parameter and set its properties.
SqlParameter parameter = new SqlParameter();
parameter.ParameterName = "@CategoryName";
parameter.SqlDbType = SqlDbType.NVarChar;
parameter.Direction = ParameterDirection.Input;
parameter.Value = categoryName;




Code is from here:
http://msdn.microsoft.com/en-us/library/yy6y35y8(v=VS.80).aspx

you can use the dropdownlist at the top "Other Versions" to check the syntax with pervious framework versions.

regards


Ribs