我試圖在我的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)。我嘗試了很多這種變化,並且無法找到任何信息類庫。任何幫助,將不勝感激!
提前感謝!
當我設置參數時,我能在類中做些什麼嗎? – 2012-04-14 01:55:51
我認爲它來自我,將過程中的類型設置爲字符串「ByVal Distributor_Email_2 As String」。因此它期望一個字符串而不是null。有沒有解決的辦法? – 2012-04-14 02:04:05
我發現這個資源,但我仍然沒有答案:http://msdn.microsoft.com/en-us/library/ms366709.aspx – 2012-04-14 15:19:49