Senin, 08 April 2013

How to Insert data with StoreProcedure sqlserver 2008 and code asp.net

To insert data in database with using storeprocedure and code c# we must instal sqlserver 2008.has we instal sqlserver 2008 we must make database,table and storeprocedure.you can see this below.



we must make table.example i make table name is TStoreProcedure with design below:

[idNama] [int] IDENTITY(1,1) NOT NULL,
[Nama] [nvarchar](50) NULL,
[Alamat] [nvarchar](50) NULL,
[Hobby] [nvarchar](50) NULL,

and now we open programmability=>storeprocedure and we make storeprocedure with names=storeproceduretest and content this below:


CREATE PROCEDURE [dbo].[storeproceduretest]
(

@Nama AS nvarchar(50),
@Alamat AS nvarchar(50),
@Hobby AS nvarchar(50)
)
AS
begin
Insert into [TStoreProcedure]
(Nama,Alamat,Hobby)values(@Nama,@Alamat,@Hobby)
end
go

and to coding in visual studio we must open visual studio 2008 =>new project=>asp.net webaplication
 you can see this below:


we coding in default.ascx this below

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="default.ascx.cs" Inherits="Module.StoreProcedure._default" %>
<asp:Panel ID="pnl1" runat="server">
    <table>
        <tr>
            <td>
                Nama
            </td>
            <td>
                :
            </td>
            <td>
                <asp:TextBox ID="txtNAMA" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                Alamat
            </td>
            <td>
                :
            </td>
            <td>
                <asp:TextBox ID="txtalamat" runat="server" TextMode="MultiLine"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                Hobby
            </td>
            <td>
                :
            </td>
            <td>
                <asp:TextBox ID="txthoby" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Button ID="btnsimpan" runat="server" OnClick="btnsimpan_click" Text="SIMPAN" />
            </td>
        </tr>
    </table>
    <asp:Label ID="lbltest" runat="server" Visible="false"></asp:Label>
</asp:Panel>


and coding default.ascx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNetNuke.Entities.Modules;
using System.Data.SqlClient;
using System.Data;

namespace Module.StoreProcedure
{
    public partial class _default : PortalModuleBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void btnsimpan_click(object sender, EventArgs e)
        {
            try
            {
                string strsql = "storeproceduretest";
                SqlConnection mycon = new SqlConnection(@"Data Source=MSI\MSSQL2008R2;Initial Catalog=DB;User ID=RDO3;Password=1234567");
                SqlCommand mycomand = new SqlCommand(strsql, mycon);
                mycomand.CommandType = CommandType.StoredProcedure;
                mycomand.Parameters.Add("@Nama",SqlDbType.NVarChar,50).Value = txtNAMA.Text.ToString();
                mycomand.Parameters.Add("@Alamat",SqlDbType.NVarChar,50).Value= txtalamat.Text.ToString();
                mycomand.Parameters.Add("@Hobby",SqlDbType.NVarChar,50).Value= txthoby.Text.ToString();
                mycon.Open();
                int response = -1;
                response = mycomand.ExecuteNonQuery();
                if (response >= 1)
                {
                    lbltest.Text = "data berhasil disimpan";


                }
                else
                {
                    lbltest.Text = "data gagal";


                }


            }
            catch (Exception ex)
            {
            }

        }
    }
}


finaly result programing above you see this screenshoot below:






---oke Thank you ,I hope can be usefull---









Tidak ada komentar:

Posting Komentar