2011-01-21 228 views
1

即時獲取數據庫中的值。它出現了一個錯誤,提示「從DBNULL到Double的轉換無效」。任何人,請幫助我?如何將DBNULL轉換爲雙精度?

公共職能共有()爲雙 昏暗總爲雙

Dim strConn As String 
    strConn = ConfigurationManager.ConnectionStrings("***").ToString 
    Dim conn As New SqlConnection(strConn) 

    Dim strSql As String 
    strSql = "SELECT SUM (p.Price * c.Quantity) as 'Total' " & _ 
    "FROM CartItem sci INNER JOIN Product p ON c.ProductID=p.ProductID " & _ 
    "WHERE [email protected] " 

    Dim cmd As New SqlCommand(strSql, conn) 

    cmd.Parameters.AddWithValue("@CartID", CartID) 

    Dim da As New SqlDataAdapter(cmd) 

    Dim ds As New DataSet 

    conn.Open() 

    da.Fill(ds, "CartItem") 

    conn.Close() 

    If (ds.Tables("CartItem").Rows.Count) <> 0 Then 
     **Total = ds.Tables("ShopCartItem").Rows(0)("Total")** 
    Else 
     Total = 0.0 
    End If 
    Return Total 
End Function 
+0

相似問題:http://stackoverflow.com/questions/222834/handling-dbnull-data-in-vb-net,你只需要測試DbNull – 2011-01-21 23:12:04

回答

1

試着改變你的SQL語句:

SELECT SUM (ISNULL(p.Price,0) * ISNULL(c.Quantity,0)) as 'Total' " & _ 
    "FROM CartItem sci INNER JOIN Product p ON c.ProductID=p.ProductID " & _ 
    "WHERE [email protected] 

您還可以設置你的數據庫表不允許爲空

+0

Nopes,它不起作用。不管怎麼說,還是要謝謝你。 :) – user576785 2011-01-22 00:00:31

0

如果值可能爲空,則返回類型需要爲雙倍? (可空雙精度)。

+0

正是如此。 NULL是缺少的值。從概念上講,這是一種超出該類型所有可能值的範疇之外的價值。修復投票。 – 2011-01-21 23:36:03

1

在設置之前,您需要檢查 ds.Tables("ShopCartItem").Rows(0))("Total") = DbNull.Value

If (ds.Tables("CartItem").Rows.Count) <> 0 Then 
    If ds.Tables("ShopCartItem").Rows(0))("Total") = DbNull.Value 
     Total = 0.0 
    else 
     Total = ds.Tables("ShopCartItem").Rows(0)("Total")** 
Else 
    Total = 0.0 
End If 
+0

有效的Orites!非常感謝!!!!!!!!!!!!我非常感謝。百萬感謝你! :)))))))) – user576785 2011-01-22 00:13:41

0

你可以改爲使用Nullable(雙)作爲你的VB數據類型?

0

您必須檢查是否Nullable(of Double)HasValue。 總= 0.0

If (ds.Tables("CartItem").Rows.Count) <> 0 Then 
    Dim sciValue As Nullable(Of Double) = DirectCast(ds.Tables("ShopCartItem").Rows(0)("Total"), Nullable(Of Double)) 
    If sciValue.HasValue Then 
     Total = sciValue.Value 
    End If 
End If 
4

你不能施放的DBNull到兩倍。你可以,但是,它將它轉換爲可以爲空的雙(c#double?)。

我寫了一堆DataRow擴展方法(C#),對此有很多幫助。使語法更加整齊。用法很簡單。 A C#示例:

public class Hormone 
{ 
    public int   ID    { get ; private set ; } 
    public HormoneLuType Type    { get ; private set ; } 
    public int   AgeStarted  { get ; private set ; } 
    public int   AgeStopped  { get ; private set ; } 
    public int   DurationInMonths { get ; private set ; } 
    public bool   IsCurrentlyUsing { get ; private set ; } 
    public DateTime?  DateLastEdited { get ; private set ; } 

    public string Name { get { return Type.ToString() } } 

    public Hormone(DataRow dr) 
    { 
     this.ID    =     dr.CastAsInt(    "ihormoneid")  ; 
     this.Type    = new HormoneLuType(dr.CastAsIntNullable(  "ihormluid" ) ?? 0) ; 
     this.AgeStarted  = (int)    dr.CastAsDecimal(   "nstartage" )  ; 
     this.AgeStopped  = (int)    dr.CastAsDecimal(   "nendage" )  ; 
     this.DurationInMonths = (int)    dr.CastAsDecimal(   "nduration" )  ; 
     this.IsCurrentlyUsing =     dr.CastAsBool(   "lusingnow" )  ; 
     this.DateLastEdited =     dr.CastAsDateTimeNullable("tedit"  )  ; 

     return ; 
    } 
} 

這裏的擴展類:

using System; 
using System.Data; 

namespace DataAccess.Utils 
{ 
    public static class DataRowExtensions 
    { 

     #region downcast to DateTime 

     public static DateTime CastAsDateTime(this DataRow row , int index) 
     { 
      return toDateTime(row[index]) ; 
     } 
     public static DateTime CastAsDateTime(this DataRow row , string columnName) 
     { 
      return toDateTime(row[columnName]) ; 
     } 

     public static DateTime? CastAsDateTimeNullable(this DataRow row , int index) 
     { 
      return toDateTimeNullable(row[index]); 
     } 
     public static DateTime? CastAsDateTimeNullable(this DataRow row , string columnName) 
     { 
      return toDateTimeNullable(row[columnName]) ; 
     } 

     #region conversion helpers 

     private static DateTime toDateTime(object o) 
     { 
      DateTime value = (DateTime)o; 
      return value; 
     } 

     private static DateTime? toDateTimeNullable(object o) 
     { 
      bool hasValue = !(o is DBNull); 
      DateTime? value = (hasValue ? (DateTime?) o : (DateTime?) null) ; 
      return value; 
     } 

     #endregion 

     #endregion downcast to DateTime 

     #region downcast to byte[] 

     public static byte[] CastAsByteArray(this DataRow row , int index) 
     { 
      return toByteArray(row[index]); 
     } 
     public static byte[] CastAsByteArray(this DataRow row , string columnName) 
     { 
      return toByteArray(row[columnName]); 
     } 

     #region conversion helpers 

     private static byte[] toByteArray(object o) 
     { 
      bool hasValue = !(o is DBNull); 
      byte[] value = (hasValue ? (byte[]) o : (byte[]) null) ; 
      return value; 
     } 

     #endregion 

     #endregion downcast to Byte[] 

     #region downcast to int 

     public static int CastAsInt(this DataRow row , int index) 
     { 
      return toInt(row[index]) ; 
     } 
     public static int CastAsInt(this DataRow row , string columnName) 
     { 
      return toInt(row[columnName]) ; 
     } 

     public static int? CastAsIntNullable(this DataRow row , int index) 
     { 
      return toIntNullable(row[index]); 
     } 
     public static int? CastAsIntNullable(this DataRow row , string columnName) 
     { 
      return toIntNullable(row[columnName]) ; 
     } 

     #region conversion helpers 

     private static int toInt(object o) 
     { 
      int value = (int)o; 
      return value; 
     } 

     private static int? toIntNullable(object o) 
     { 
      bool hasValue = !(o is DBNull); 
      int? value = (hasValue ? (int?) o : (int?) null) ; 
      return value; 
     } 

     #endregion 

     #endregion downcast to int 

     #region downcast to int 

     public static decimal CastAsDecimal(this DataRow row , int index) 
     { 
      return toDecimal(row[index]) ; 
     } 
     public static decimal CastAsDecimal(this DataRow row , string columnName) 
     { 
      return toDecimal(row[columnName]) ; 
     } 

     public static decimal? CastAsDecimalNullable(this DataRow row , int index) 
     { 
      return toDecimalNullable(row[index]); 
     } 
     public static decimal? CastAsDecimalNullable(this DataRow row , string columnName) 
     { 
      return toDecimalNullable(row[columnName]) ; 
     } 

     #region conversion helpers 

     private static decimal toDecimal(object o) 
     { 
      decimal value = (decimal)o; 
      return value; 
     } 

     private static decimal? toDecimalNullable(object o) 
     { 
      bool  hasValue = !(o is DBNull); 
      decimal? value = (hasValue ? (decimal?) o : (decimal?) null) ; 
      return value; 
     } 

     #endregion 

     #endregion downcast to int 

     #region downcast to bool 

     public static bool CastAsBool(this DataRow row , int index) 
     { 
      return toBool(row[index]) ; 
     } 
     public static bool CastAsBool(this DataRow row , string columnName) 
     { 
      return toBool(row[columnName]) ; 
     } 

     public static bool? CastAsBoolNullable(this DataRow row , int index) 
     { 
      return toBoolNullable(row[index]); 
     } 
     public static bool? CastAsBoolNullable(this DataRow row , string columnName) 
     { 
      return toBoolNullable(row[columnName]) ; 
     } 

     #region conversion helpers 

     private static bool toBool(object o) 
     { 
      bool value = (bool)o; 
      return value; 
     } 

     private static bool? toBoolNullable(object o) 
     { 
      bool hasValue = !(o is DBNull); 
      bool? value = (hasValue ? (bool?) o : (bool?) null) ; 
      return value; 
     } 

     #endregion 

     #endregion downcast to bool 

     #region downcast to string 

     public static string CastAsString(this DataRow row , int index) 
     { 
      return toString(row[index]); 
     } 
     public static string CastAsString(this DataRow row , string columnName) 
     { 
      return toString(row[columnName]); 
     } 

     #region conversion helpers 

     private static string toString(object o) 
     { 
      bool hasValue = !(o is DBNull); 
      string value = (hasValue ? (string) o : (string) null) ; 
      return value; 
     } 

     #endregion 

     #endregion downcast to string 

    } 
} 

希望這有助於!

+0

+1傾倒一噸非常有用的擴展方法! – 2012-05-11 14:16:48