将非字符串类型的参数添加到objectdatasource

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (12)

我得到了下面的代码来添加属于我的ObjectDataSource对象的Insert方法的Insert参数。网格的页脚中有一个插入按钮\ link,它依赖于ObjectDataSource的Insert方法。

在下面的方法中,DefaultValue是一个字符串类型。

在链接上显示给我的示例中,所有参数都是字符串类型。如果参数不是字符串类型,你会怎么做?这是链接,如果您感兴趣:http//csharp-video-tutorials.blogspot.com/2013/03/gridview-insert-update-delete-in-aspnet_13.html

protected void lbInsert_Click(object sender, EventArgs e)
{
    ObjectDataSource1.InsertParameters["Name"].DefaultValue =
        ((TextBox)GridView1.FooterRow.FindControl("txtName")).Text;
    ObjectDataSource1.InsertParameters["Gender"].DefaultValue =
        ((DropDownList)GridView1.FooterRow.FindControl("ddlInsertGender")).SelectedValue;
    ObjectDataSource1.InsertParameters["City"].DefaultValue =
        ((TextBox)GridView1.FooterRow.FindControl("txtCity")).Text;
    ObjectDataSource1.Insert();
} 



<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
    DeleteMethod="DeleteEmployee" InsertMethod="InsertEmployee" 
    SelectMethod="GetAllEmployees" TypeName="Demo.EmployeeDataAccessLayer" 
    UpdateMethod="UpdateEmployee">
    <DeleteParameters>
        <asp:Parameter Name="EmployeeId" Type="Int32" />
    </DeleteParameters>
    <InsertParameters>
        <asp:Parameter Name="Name" Type="String" />
        <asp:Parameter Name="Gender" Type="String" />
        <asp:Parameter Name="City" Type="String" />
    </InsertParameters>
    <UpdateParameters>
         ....
    </UpdateParameters>
</asp:ObjectDataSource>

// Insert Method for ObjectDataSource control
    public static int InsertEmployee(string Name, string Gender, string City)
    {
        string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
        using (SqlConnection con = new SqlConnection(CS))
        {
            string updateQuery = "Insert into tblEmployee (Name, Gender, City)" + 
                " values (@Name, @Gender, @City)";
            SqlCommand cmd = new SqlCommand(updateQuery, con);
            SqlParameter paramName = new SqlParameter("@Name", Name);
            cmd.Parameters.Add(paramName);
            SqlParameter paramGender = new SqlParameter("@Gender", Gender);
            cmd.Parameters.Add(paramGender);
            SqlParameter paramCity = new SqlParameter("@City", City);
            cmd.Parameters.Add(paramCity);
            con.Open();
            return cmd.ExecuteNonQuery();
        }

在我的情况下,我没有字符串参数。所以我不知道如何编码我的插入按钮单击位于网格的页脚。我的情景:

<asp:ObjectDataSource ID="ObjectDataSource2" runat="server" SelectMethod="GetAllProducts" TypeName="VenketGrid.DataAccessLayer2" UpdateMethod="UpdateProduct" ConflictDetection = "CompareAllValues" OldValuesParameterFormatString = "Original_{0}" DeleteMethod="Delete" OnSelecting="ObjectDataSource2_Selecting" OnUpdated="ObjectDataSource2_Updated" InsertMethod="InsertProduct">
            <DeleteParameters>
             ...
            </DeleteParameters>
            <InsertParameters>
                <asp:Parameter Name="Inactive" Type="Boolean" />
                <asp:Parameter Name="CategoryId" Type="Int32" />
                <asp:Parameter Name="Description" Type="String" />
                <asp:Parameter Name="CurrentPrice" Type="Decimal" />
                <asp:Parameter Name="Modified" Type="DateTime" />
            </InsertParameters>
            <UpdateParameters>
                ...
            </UpdateParameters>
        </asp:ObjectDataSource>


// Insert Method for ObjectDataSource control

    public static int InsertProduct(bool Inactive, int CategoryId, string Description, Decimal CurrentPrice, DateTime Modified)
    {

        string ConnectionString;
        ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["DBConn"].ToString();

        SqlConnection con = new SqlConnection(ConnectionString);

        SqlCommand cmd = new SqlCommand("_SP_Insert_RefProducts", con);

        cmd.CommandType = System.Data.CommandType.StoredProcedure;

        cmd.Parameters.Add(new SqlParameter("@Inactive", Inactive));

        cmd.Parameters.Add(new SqlParameter("@CategoryId", CategoryId));

        cmd.Parameters.Add(new SqlParameter("@Description", Description));

        cmd.Parameters.Add(new SqlParameter("@CurrentPrice", CurrentPrice));

        con.Open();

        int affected;

        affected = cmd.ExecuteNonQuery();
        con.Close();

        return affected;

    }
提问于
用户回答回答于

由于此代码中的DefaultValue是字符串类型:

ObjectDataSource1.InsertParameters["Name"].DefaultValue =  ....   

,我必须在Insert方法上使用字符串参数,使用你需要实现的任何转换...我知道没有更好的方法来解决这个问题。但是,如果有人发布更好的答案,我可以接受不同的答案......

    public static int InsertProduct(string Inactive, string CategoryId, string Description, string CurrentPrice, string Modified)
    {

        string ConnectionString;
        ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["DBConn"].ToString();

        SqlConnection con = new SqlConnection(ConnectionString);

        SqlCommand cmd = new SqlCommand("_SP_Insert_RefProducts", con);

        cmd.CommandType = System.Data.CommandType.StoredProcedure;

        cmd.Parameters.Add(new SqlParameter("@Inactive", Inactive=="1"?1:0 ));


        cmd.Parameters.Add(new SqlParameter("@CategoryId", int.Parse(CategoryId)            ));

        Description = Description + "                                                                                             ";
        cmd.Parameters.Add(new SqlParameter("@Description", Description.Substring(0,50)));

        cmd.Parameters.Add(new SqlParameter("@CurrentPrice", double.Parse(CurrentPrice)));

        con.Open();

        int affected;

        affected = cmd.ExecuteNonQuery();
        con.Close();

        return affected;

    }

扫码关注云+社区

领取腾讯云代金券