Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2009-07-14
3
Medium Priority
?
830 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 1500 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
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…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

636 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