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

x
  • 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";
        //?
        dsCreatePlan.Insert();
    }

Open in new window

0
awilderbeast
Asked:
awilderbeast
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
0
 
Paul JacksonCommented:
0
 
awilderbeastAuthor Commented:
no there is no code in the aspx, im trying to do it all from the codebehind...
0
 
angus_young_acdcCommented:
SqlConnection conn = new SqlConnection("MyConnectionString");
            conn.Open();
            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;
            cmd.ExecuteNonQuery();
            conn.Close();

Open in new window

0
 
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 ...
Alan
0

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