Solved

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

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

786 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