2016-01-06 63 views
0

我試圖執行在vb.net窗口此查詢窗體應用程序始終從MS Access數據庫vb.net返回空值

select SUM(Amount) 
from FeesRecord 
where [email protected] 
AND Batch_Id=(select Batch_Id from Admissions where [email protected]) 
AND ReExam_Id IS NULL) 

我設置參數正確的價值觀,但我一直都想與空的結果在executioncaler中。

我的代碼

cmd.CommandText = "select SUM(Amount) from FeesRecord where ([email protected] AND Batch_Id=(select Batch_Id from Admissions where [email protected]) AND ReExam_Id IS NULL)" 
         cmd.Parameters.AddWithValue("@p1", txtregno.Text) 
         cmd.Parameters.AddWithValue("@p2", gv1.Rows(i).Cells(0).Value.ToString) 
         MsgBox(cmd.ExecuteScalar.ToString) 
+0

您能否提供您使用的參數值以及表中包含的與這些值相關的數據? – trincot

回答

0

首先,做你打開連接話說con.Open()和引用的,在你的命令對象cmd.Connection = con連接字符串。

其次,你可以修改您的查詢要像下面用簡單的加入

select SUM(fr.Amount) 
from FeesRecord fr join Admissions a 
on fr.Batch_Id = a.Batch_Id 
where fr.Student_Id = @p1 
AND a.Admission_Id = @p2 
AND fr.ReExam_Id IS NULL; 

如果你仍然得到返回null然後運行在DB直接相同的查詢,看看是什麼導致你。最有可能的是,WHERE條件不匹配任何記錄。

+0

我用這個查詢,但我得到錯誤「錯誤在從子句」 – Rajesh