Solved

Insert data in asp.net C# using a stored procedure

Posted on 2009-07-14
3
823 Views
Last Modified: 2012-05-07
Hi I am new to asp.net and c# I am looking to do a simple insert into a datbase using a stored procedure. I have the following code in the front end of my file. Is anything missing from this data source what would the code in the code behind file need to be to execute this stored procedure. I am very new to this so please if possible send me the code.

<asp:SqlDataSource ID="sqlinsert" runat="server" 
            ConnectionString="<%$ ConnectionStrings:PTConnectionString %>"
                 InsertCommand="sp_EventsInsert"
                 InsertCommandType="StoredProcedure" 
                 <insertparameters>
                  <asp:Parameter Name="cid" Type="Int16" />
                  <asp:Parameter Name="EventStartDate" Type="DateTime" />
                  <asp:Parameter Name="EvendtEndDate" Type="DateTime" />
                  <asp:Parameter Name="PatientId" Type="Int16" />
                  <asp:Parameter Name="EventId" Type="Int16" />
                  <asp:Parameter Name="PatientStatusChange" Type="Int16" />
                  <asp:Parameter Name="Reason" Type="Int16" />
                  <asp:Parameter Name="Memo"  Type="String" />
                  <asp:Parameter Name="EventTypeID" Type="Int16" />
                  <asp:Parameter Name="LoginID" Type="Int16" />
                 
                 </insertparameters>
                 </asp:SqlDataSource>

Open in new window

0
Comment
Question by:susan1978
3 Comments
 
LVL 28

Expert Comment

by:strickdd
ID: 24852952
You will probably want to bind this SQL Datasource to either a DetailsView or a FormView control. These controls will take care of the majority of the work.

1) Drag Data component to the form
2) Click the smart tag and select this SqlDataSource as your datasource (smart tag is the little arrow at the top right when the control is selected)
3) Set the DefaultMode to be Insert.

These controls will take care of the code to execute the Insert automatically.
0
 
LVL 13

Accepted Solution

by:
drypz earned 500 total points
ID: 24856500
Hi! Do it something like this.

    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlDataSource1.InsertCommand = "YourStoredProcedureName";
        SqlDataSource1.InsertCommandType = SqlDataSourceCommandType.StoredProcedure;
        SqlDataSource1.InsertParameters.Add("@Parameter1", DbType.String, "value1");
        SqlDataSource1.Insert();
    }
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
RLDC Reporting in Visual studio 11 29
Winforms -- Capture CTRL+SHIFT+D at the form not working? 3 25
innerHTML 7 34
vb.net convert long time to mm:ss 23 10
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…

809 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