2016-11-13 90 views
-2

這是我的語法,但我得到的錯誤試圖填補數據表

SelectCommand屬性錯誤調用「補」之前尚未初始化。

爲了能夠填充數據表,我需要做些什麼?

using (SqlConnection conn = new SqlConnection("Server=Test;Database=Test;Integrated Security=SSPI;")) 
{ 
SqlCommand command = new SqlCommand(); 
command.CommandText = "SELECT * FROM [dbo].[selfservice] WHERE saleID = @userid;"; 
command.Parameters.Add("@userid", SqlDbType.VarChar); 
command.Parameters["@userid"].Value = row.Field<string>("saleID"); 
command.Connection = conn;      
using (SqlDataAdapter dataadapter1 = new SqlDataAdapter() 
{ 
    dataadapter1.Fill(dtData); 
} 
} 
+0

標記爲答案,如果它有幫助 – Sajeetharan

回答

1

通知。您需要將選擇命令添加到您的適配器:

using (SqlDataAdapter dataadapter1 = new SqlDataAdapter() 
{ 
    dataadapter1.SelectCommand = command 
    dataadapter1.Fill(dtData); 
} 
+0

哦,那麼容易,謝謝你的修復! –

+0

爲什麼投票反對? – user3185569

0

你必須填寫之前指定的SqlDataAdapter的select命令,也是你在最後缺少一個右括號,你不使用命令對象

using (SqlDataAdapter dataadapter1 = new SqlDataAdapter()) 
{ 
    dataadapter1.SelectCommand=command; 
    dataadapter1.Fill(dtData); 
}