2012-04-10 59 views
0

我試圖在我的GridView中編輯表格時將空字符串轉換爲null。我嘗試了幾次迭代和方法,但沒有成功。我懷疑問題出在我的ObjectDataSource,但不知道我需要的正確語法。Convertemptystringtonull在gridview中不工作

<asp:GridView 
    ID="grdMyDistributors" 
    DataSourceID = "srcGetMyDistributors" 
    CssClass="GridViewStyle" 
    DataKeyNames = "ID_Distributor" 
    AutoGenerateColumns = "false" AutoGenerateEditButton = "true" 
    GridLines="None" 
    runat="server" > 
    <Columns> 
    <asp:BoundField 
     DataField="DistributorName" 
     HeaderText="Distributor" 
     convertemptystringtonull="true" 
     ReadOnly = "True" /> 
    <asp:BoundField 
     DataField="Distributor_Email_1" 
     HeaderText="Distributor Email 1" 
     convertemptystringtonull="true" > 
     <ItemStyle BackColor="#f6f17c"/> 
    </asp:BoundField> 
    <asp:BoundField 
     DataField="Distributor_Email_2" 
     HeaderText="Distributor Email 2" 
     convertemptystringtonull="true" > 
     <ItemStyle BackColor="#f6f17c"/> 
    </asp:BoundField> 
    <asp:BoundField 
     DataField="Minimum_Purchase_Amount" 
     HeaderText="Minimum Purchase Amount" 
     dataformatstring="{0:c}" 
     convertemptystringtonull="true" > 
     <ItemStyle BackColor="#f6f17c" /> 
    </asp:BoundField> 
    </Columns> 
</asp:GridView> 

這裏是我的ObjectDataSource

<asp:ObjectDataSource 
     ID="srcGetMyDistributors" 
     TypeName = "AAA.Distributors" 
     SelectMethod = "GetDistributorsPagingFiltered" 
     UpdateMethod = "UpdateDistributors" 
     EnableCaching="false" 
     ConvertNulltoDBNull = "true" 
     runat="server" > 
     <SelectParameters> 
      <asp:ProfileParameter Name="ID_Account" PropertyName="ID_Account" /> 
     </SelectParameters> 
     <UpdateParameters> 
      <asp:ControlParameter ControlID="grdMyDistributors" Name="ID_Distributor" PropertyName="SelectedValue" Type="Int32" /> 
     </UpdateParameters> 
    </asp:ObjectDataSource> 

這裏是我的更新方法

Public Shared Sub UpdateDistributors(ByVal ID_Distributor As Integer, ByVal Distributor_Email_1 As String, ByVal Distributor_Email_2 As String, ByVal Minimum_Purchase_Amount As Decimal) 
     ' Initialize command 
     Dim con As New SqlConnection(_connectionString) 
     Dim cmd As New SqlCommand("UpdateDistributors", con) 
     cmd.CommandType = CommandType.StoredProcedure 
     cmd.Connection = con 

     ' Initialize parameters 
     cmd.Parameters.AddWithValue("@ID_Distributor", A2AID_Restaurant_To_Distributor) 
     cmd.Parameters.AddWithValue("@Distributor_Email_1", Distributor_Email_1) 
     cmd.Parameters.AddWithValue("@Distributor_Email_2", Distributor_Email_2) 
     cmd.Parameters.AddWithValue("@Minimum_Purchase_Amount", Minimum_Purchase_Amount) 

     ' Execute command 
     Using (con) 
      con.Open() 
      cmd.ExecuteNonQuery() 
     End Using 

    End Sub 

當我作爲運行的代碼,我得到一個錯誤說「類型的對象「系統.DBNull'無法轉換爲類型'System.String'。「當它試圖發送到Update方法並期待一個字符串時,它很可能會出錯。如果我從ObjectDataSource中取出「ConvertNulltoDBNull =」true「,它會抱怨SQL語句正在等待一個值(意思是它沒有發送Null)。我嘗試了很多這種變化,並且無法找到任何信息類庫。任何幫助,將不勝感激!

提前感謝!

+0

當我設置參數時,我能在類中做些什麼嗎? – 2012-04-14 01:55:51

+0

我認爲它來自我,將過程中的類型設置爲字符串「ByVal Distributor_Email_2 As String」。因此它期望一個字符串而不是null。有沒有解決的辦法? – 2012-04-14 02:04:05

+0

我發現這個資源,但我仍然沒有答案:http://msdn.microsoft.com/en-us/library/ms366709.aspx – 2012-04-14 15:19:49

回答

0

使用eval()爲你的GridView列

<%# Eval("DistributorName") == DBNull.Value ? "" : value.ToString()%> 

(它在C#)

​​

+0

抱歉,但你能解釋這一點多一點。我玩了eval,並無法讓它工作 – 2012-04-11 01:58:32

+0

我很感激幫助,但不幸的是它沒有奏效。當我在GridView中顯示數據時,不會發生此錯誤,但在嘗試回發空字段後,會發生在編輯模式下。所以我嘗試了同樣的方法,但使用了編輯模板,我認爲我的語法不正確。 runat = server /> 2012-04-14 01:42:29

+0

我也嘗試將值設置爲「DBnull」作爲字符串(有點像你所建議的)和「NBNull」字面上顯示在數據庫中(它不只是空白) – 2012-04-14 01:58:39