2010-05-24 71 views
0

这个webservice几乎从VB转换为C#,除了我在下面的foreach语句中使用它时填充DataRow arow对象时出现此错误结果类与DataSet对象...任何想法?仍然有问题转换为VB Webserice到C#....需要帮助

错误:名为“AROW”的局部变量不能在此范围内声明,因为它会给予不同的意义“AROW”,这已经是一个“父母或电流”范围用来表示别的

 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

aRow正在申报两次,一次在foreach和另一个正确的上面。删除DataRow aRow在foreach上方,你应该很好。

+0

System.NullReferenceException错误...继续尝试解除错误...不工作 – CraigJSte 2010-05-24 21:37:48

+0

我已经删除了对Datarow arow的引用,并且我得到一个 System.NullReferenceException:未将对象引用设置为实例的一个对象。 at VTResults.GetResult() – CraigJSte 2010-05-24 22:58:51

+0

所以你现在没有编译错误,只是运行时NullReferenceException? 发生什么错误? 存储过程是否肯定返回一些数据?你有没有在查询分析器中运行它,并验证它至少有一行返回? 你在arow [“Ticker”]等行中使用的字段名是否与存储过程返回的列的名称完全匹配? – Carson63000 2010-05-24 23:22:08