views:

31

answers:

1

This webservice is almost converted form VB to C#, except I get this error shown below on the DataRow arow object when I use it in the foreach statement below to populate the Results Class with a DataSet Object... any ideas???

Error: A local variable named 'arow' cannot be declared in this scope because it would give a different meaning to 'arow', which is already used in a 'parent or current' scope to denote something else

        using System;
        using System.Web;
        using System.Collections;
        using System.Web.Services;
        using System.Web.Services.Protocols;
        using System.Data;
        using System.Data.SqlClient;
        using System.Configuration;
        /// <summary>
        /// Summary description for VTResults
        /// </summary>
        [WebService(Namespace = "http://velocitytrading.net/ResultsVT.aspx")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        public class VTResults : System.Web.Services.WebService {
            public class Results {
                public string Ticker;
                public string BuyDate;
                public string Buy;
                public string SellDate;
                public string Sell;
                public string Profit;
                public string Period;
            }
            [WebMethod]
            public Results[] GetResults() {
                string conn = 
                ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString;
                SqlConnection myconn = new SqlConnection(conn);
                SqlCommand mycomm = new SqlCommand();
                SqlDataAdapter myda = new SqlDataAdapter();
                DataSet myds = new DataSet();

                mycomm.CommandType = CommandType.StoredProcedure;
                mycomm.Connection = myconn;
                mycomm.CommandText = "dbo.Results";

                myconn.Open();
                myda.SelectCommand = mycomm;
                myda.Fill(myds);
                myconn.Close();
                myconn.Dispose();

                int i = 0;

                Results[] dts = new Results[myds.Tables[0].Rows.Count];
                DataRow arow;

                foreach(DataRow arow ** in myds.Tables[0].Rows)
                {
                    dts[i] = new Results();
                    dts[i].Ticker = arow["Ticker"].ToString();
                    dts[i].BuyDate = arow["BuyDate"].ToString();
                    dts[1].Buy = arow["Buy"].ToString();
                    dts[i].SellDate = arow["SellDate"].ToString();
                    dts[i].Sell = arow["Sell"].ToString();
                    dts[i].Profit = arow["Profit"].ToString();
                    dts[i].Period = arow["Period"].ToString();
                    i+=1;
                }
                return dts;
            }    
        }

           ** ERROR ON THIS 'AROW' OBJECT
+2  A: 

aRow is being declared twice, once in the foreach and the other right above it. Remove DataRow aRow right above the foreach and you should be good.

Andrew
System.NullReferenceException error... continuing to try to de-bug this ... not working
CraigJSte
I have removed the reference to Datarow arow and I'm getting a System.NullReferenceException: Object reference not set to an instance of an object. at VTResults.GetResult()
CraigJSte
So you have no compile errors now, just the runtime NullReferenceException?What line is the error occurring at?Is the stored procedure definitely returning some data? Have you run it in query analyzer and verified that it has at least one row returned?Do the field names you're using in your arow["Ticker"] etc. lines definitely match the names of the columns returned by the stored procedure?
Carson63000
it runs fine as DataSet but when I try to use the Result Class as a Data Model to get normalized XML it does not run.
CraigJSte
found problem...dts[1].Buy = arow["Buy"].ToString();should bedts[i] !!!!!!!!!!!!!!!!!!!
CraigJSte