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

时间:2019-06-09 05:30:38

标签: c# asp.net gridview objectdatasource

为我提供了以下代码,以添加属于我的ObjectDataSource对象的Insert方法的Insert参数。网格的页脚中有一个insert button \ 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;

    }

1 个答案:

答案 0 :(得分:0)

由于此代码中的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;

    }