HillOfBeans
HillOfBeans

Reputation: 109

adding parameters that are not string type to the objectdatasource

I was given the below piece of code to add Insert parameters that belong to my ObjectDataSource object's Insert method. There is an insert button\link in the footer of the grid which relies on the ObjectDataSource's Insert method.

In the method below, DefaultValue is a string type.

In the example shown to me on the link, all parameters are of string type. What would you do if the parameter was not a string type? Here is the link, incase you are interested: 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();
        }

In my case, I don't have string parameter. so I don't know how to code my insert button click that is in the footer of the grid. My scenario:

<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;

    }

Upvotes: 1

Views: 235

Answers (1)

HillOfBeans
HillOfBeans

Reputation: 109

Since DefaultValue in this code is a string type:

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

, I have to use string parameters on the Insert method use whatever casting you need to implement this way... I know of no better way to solve this question. But, I can accept a different answer if anybody posts a better one...

    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;

    }

Upvotes: 0

Related Questions