0

在我的 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)。我已经尝试了很多变体,但在类库。任何帮助将不胜感激!

提前致谢!

4

1 回答 1

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>

于 2012-04-10T17:15:25.993 回答