You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为ObjectDataSource配置非字符串类型的Insert参数?

No worries, let's fix this! The string parameter example works because .DefaultValue accepts strings directly, but for non-string types like Boolean, Int32, Decimal, and DateTime, you need to convert the control values to the correct data type before assigning them to your ObjectDataSource's InsertParameters. Here's how to do it properly:

<asp:GridView ID="GridView1" runat="server" ShowFooter="true">
    <FooterTemplate>
        <asp:CheckBox ID="chkInactive" runat="server" Text="Mark as Inactive" />
        <asp:DropDownList ID="ddlCategoryId" runat="server" DataTextField="CategoryName" DataValueField="CategoryId" />
        <asp:TextBox ID="txtDescription" runat="server" Placeholder="Product Description" />
        <asp:TextBox ID="txtCurrentPrice" runat="server" Placeholder="Current Price" />
        <asp:TextBox ID="txtModified" runat="server" Placeholder="Modified Date" />
        <asp:CalendarExtender ID="calModified" runat="server" TargetControlID="txtModified" />
        <asp:LinkButton ID="lbInsert" runat="server" OnClick="lbInsert_Click" Text="Add Product" />
    </FooterTemplate>
</asp:GridView>

Step 2: Updated lbInsert_Click Event Code

This code safely handles each data type conversion (using TryParse to avoid runtime errors from invalid user input):

protected void lbInsert_Click(object sender, EventArgs e)
{
    // Grab references to all footer controls first
    GridViewRow footerRow = GridView1.FooterRow;
    CheckBox chkInactive = (CheckBox)footerRow.FindControl("chkInactive");
    DropDownList ddlCategoryId = (DropDownList)footerRow.FindControl("ddlCategoryId");
    TextBox txtDescription = (TextBox)footerRow.FindControl("txtDescription");
    TextBox txtCurrentPrice = (TextBox)footerRow.FindControl("txtCurrentPrice");
    TextBox txtModified = (TextBox)footerRow.FindControl("txtModified");

    // 1. Handle Boolean parameter (Inactive)
    ObjectDataSource2.InsertParameters["Inactive"].DefaultValue = chkInactive.Checked.ToString();

    // 2. Handle Int32 parameter (CategoryId)
    if (!int.TryParse(ddlCategoryId.SelectedValue, out int categoryId))
    {
        // Show error message if CategoryId is invalid
        Response.Write("<script>alert('Invalid Category ID');</script>");
        return;
    }
    ObjectDataSource2.InsertParameters["CategoryId"].DefaultValue = categoryId.ToString();

    // 3. Handle String parameter (Description) - same as your original example
    ObjectDataSource2.InsertParameters["Description"].DefaultValue = txtDescription.Text;

    // 4. Handle Decimal parameter (CurrentPrice)
    if (!decimal.TryParse(txtCurrentPrice.Text, out decimal currentPrice))
    {
        Response.Write("<script>alert('Please enter a valid price');</script>");
        return;
    }
    ObjectDataSource2.InsertParameters["CurrentPrice"].DefaultValue = currentPrice.ToString();

    // 5. Handle DateTime parameter (Modified)
    if (!DateTime.TryParse(txtModified.Text, out DateTime modifiedDate))
    {
        Response.Write("<script>alert('Please enter a valid date');</script>");
        return;
    }
    ObjectDataSource2.InsertParameters["Modified"].DefaultValue = modifiedDate.ToString();

    // Execute the insert and refresh the GridView
    ObjectDataSource2.Insert();
    GridView1.DataBind();
}

Key Notes for Each Data Type:

  • Boolean: Use the CheckBox.Checked property directly, convert it to a string ("true"/"false") for DefaultValue — the ObjectDataSource will auto-convert it back to a bool.
  • Int32/Decimal: Always use TryParse to validate input. This prevents crashes if users enter non-numeric values, and lets you show friendly error messages.
  • DateTime: TryParse handles various date formats. If you're using a Calendar control instead of a TextBox, just use Calendar.SelectedDate.ToString() directly.

Why This Works:

Even though DefaultValue is a string property, the ObjectDataSource automatically converts the string to the parameter type you defined in <InsertParameters> (e.g., Type="Boolean" or Type="Int32") as long as the string is a valid representation of that type.

内容的提问来源于stack exchange,提问作者HillOfBeans

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 08:19:16