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
System.NullReferenceException錯誤...繼續嘗試解除錯誤...不工作 – CraigJSte 2010-05-24 21:37:48
我已經刪除了對Datarow arow的引用,並且我得到一個 System.NullReferenceException:未將對象引用設置爲實例的一個對象。 at VTResults.GetResult() – CraigJSte 2010-05-24 22:58:51
所以你現在沒有編譯錯誤,只是運行時NullReferenceException? 發生什麼錯誤? 存儲過程是否肯定返回一些數據?你有沒有在查詢分析器中運行它,並驗證它至少有一行返回? 你在arow [「Ticker」]等行中使用的字段名是否與存儲過程返回的列的名稱完全匹配? – Carson63000 2010-05-24 23:22:08