我正在尝试在gridview中编辑表时将空字符串转换为null。我已经尝试了几次迭代和方法但没有成功。我怀疑问题出在我的ObjectDataSource
但不知道我需要的正确语法。
<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
当我按原样运行代码时,我收到错误消息“类型'System.DBNull'的对象无法转换为'System.String'类型。”当它试图发送到Update方法并期望一个字符串时,它很可能是错误的。如果我从ObjectDataSource中取出“ConvertNulltoDBNull =”true“,它会抱怨SQL语句期望一个值(意味着它没有发送Null)。我已经尝试了很多这方面的变化,并且找不到任何信息。课程图书馆。非常感谢任何帮助!
提前致谢!
答案 0 :(得分:0)
对gridview列使用eval()
<%# Eval("DistributorName") == DBNull.Value ? "" : value.ToString()%>
(在C#中)
<asp:GridView
ID="grdMyDistributors"
DataSourceID = "srcGetMyDistributors"
CssClass="GridViewStyle"
DataKeyNames = "ID_Distributor"
AutoGenerateColumns = "false"
AutoGenerateEditButton = "true"
GridLines="None"
runat="server" >
<Columns>
<asp:TemplateField HeaderText="Seniority">
<ItemTemplate>
<%# Eval("DistributorName") == DBNull.Value ? "" :Eval("DistributorName").ToString()%>
</ItemTemplate>
</asp:TemplateField>
</Columns>