Solved

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

Posted on 2009-07-14
3
812 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

705 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

19 Experts available now in Live!

Get 1:1 Help Now