Thursday, September 16, 2010

ADO.NET : Record Insert and Display Using StoredProcedure

Windows Application Screenshot :

***************************



****************************

Screenshot Of StoredProcedure in SQL Server 2005
================================================

================================================
StoredProcedure : GetEmployee
================================================
USE [ANGEL]
GO
/****** Object: StoredProcedure [dbo].[GetEmployee] Script Date: 09/17/2010 08:52:52 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE procedure [dbo].[GetEmployee]

AS SELECT EmpNo,EmpName, EmpSal,DeptNo FROM Employee
====================================================
StoredProcedure : InsertUser
====================================================

USE [ANGEL]
GO
/****** Object: StoredProcedure [dbo].[InsertUser] Script Date: 09/17/2010 08:54:08 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[InsertUser] (
@Username varchar(50), @Password varchar(50)
)
AS INSERT INTO Users VALUES(@Username, @Password)

==================================================

Code :
==================================================

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
public partial class Form1 : Form
{

SqlConnection conn;
SqlCommand command;
SqlCommand command1;
SqlDataAdapter adapter;
DataSet ds;
public Form1()
{

InitializeComponent();
conn = new SqlConnection("Data Source=localhost; Initial Catalog=ANGEL; Integrated Security=SSPI");
conn.Open();

}

private void Form1_Load(object sender, EventArgs e)
{

command = new SqlCommand("GetEmployee", conn);
adapter = new SqlDataAdapter(command);
ds = new DataSet();
adapter.Fill(ds, "Employee");
dg_Data.DataSource = ds.Tables[0];


}

private void button1_Click(object sender, EventArgs e)
{
string username=Username.Text;
string password=Password.Text;

command1= new SqlCommand("InsertUser", conn);
command1.CommandType = CommandType.StoredProcedure;
command1.Parameters.Add("@Username", SqlDbType.VarChar).Value = username;
command1.Parameters.Add("@Password", SqlDbType.VarChar).Value = password;

int rows = command1.ExecuteNonQuery();
conn.Close();
if (rows == 1)
{
MessageBox.Show("Record Inserted successfully ...");
}
else
{
MessageBox.Show("Problem in insert !!! Check coding ");
}
Username.Text = "";
Password.Text = "";
}
}
}

=====================================================================

No comments:

Post a Comment