[Last Call] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 436
  • Last Modified:

insert using stored procedure from code behind?

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

1 Solution
Paul JacksonCommented:
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
Paul JacksonCommented:
awilderbeastAuthor Commented:
no there is no code in the aspx, im trying to do it all from the codebehind...
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

Alan WarrenCommented:
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now