insert using stored procedure from code behind?

Posted on 2012-09-07
Last Modified: 2012-09-24
hi all,

i have a simple form that im using to insert a record, on the button "create".onclick

ive added the below

my only problem is i dont know how to pass the parameters and fields to the stored procedure?

   protected void butCreatePlan_Click(object sender, EventArgs e)
        dsCreatePlan.ConnectionString = strConnection;
        dsCreatePlan.InsertCommandType = SqlDataSourceCommandType.StoredProcedure;
        dsCreatePlan.InsertCommand = "sp_Plan_Insert";

Open in new window

Question by:awilderbeast
    LVL 29

    Expert Comment

    by:Paul Jackson
    Have you declared the InsertParameters in the aspx ?

    It would help if you posted your aspx code.

    If the parameters are declared you simply:

    dsCreatePlan.InsertParameters["myParameterName"].DefaultValue = MyTextboxName.Text
    LVL 29

    Expert Comment

    by:Paul Jackson
    LVL 1

    Author Comment

    no there is no code in the aspx, im trying to do it all from the codebehind...
    LVL 15

    Accepted Solution

    SqlConnection conn = new SqlConnection("MyConnectionString");
                SqlCommand cmd = new SqlCommand("sp_Plan_Insert");
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection = conn;
                cmd.Parameters.Add(new SqlParameter(@"MyParameter", SqlDbType.VarChar, 3)).Value = "Hello!";
                cmd.Parameters.Add(new SqlParameter(@"MyParameter2", SqlDbType.Int)).Value = 1;

    Open in new window

    LVL 26

    Expert Comment

    by:Alan Warren
    Hi awilderbeast,
    curious why code behind?

    I'm a big fan of codeless databinding, just drag a sqldatasource onto the page, right click, choose configure datasource, enter the connection defined in your web.config, choose your stored procedures from the offered lists, do the same for the edit,insert and delete tabs, test the select sproc, click ok. All your parameters will be defined appropriately in the sqldatasource parameters section. Then drag a form/gridview/datalist/whatever on the page, right click, choose the datasource, then configure datasource again, all your formview templates will be created codelessly; take it for a spin. If all works out ok, style up the formview templates for presentation. Code behind should look like:
    Partial Class Default2
        Inherits System.Web.UI.Page
    End Class

    Open in new window

    Beautiful, maintainable, scalable, manageble ...

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now