TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
How to Insert Data 3-Tier in Asp.Net
Pintoo Yadav
Dec 15
2014
Code
1.6
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
pintoo c yadav.rar
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
/// <summary>
/// Summary description for PartsMaster
/// </summary>
public
class
AdminMaster
{
#region "Private Variable"
private
int
reg_id;
private
string
user_id;
private
string
password;
private
string
usertype;
private
string
name;
private
string
sirname;
// private string createdon;
private
string
mobile;
private
string
email;
private
string
companyname;
private
string
post;
private
int
Actions;
//postcode here
//private string name;
private
string
title;
private
string
postcode;
#endregion
public
AdminMaster()
{
//
// TODO: Add constructor logic here
//
}
#region "Properties"
public
string
P_password
{
get
{
return
password;
}
set
{
password = value;
}
}
public
int
R_reg_id
{
get
{
return
reg_id;
}
set
{
reg_id = value;
}
}
public
string
u_usertype
{
get
{
return
usertype;
}
set
{
usertype = value;
}
}
public
string
U_user_id
{
get
{
return
user_id;
}
set
{
user_id = value;
}
}
public
string
M_mobile
{
get
{
return
mobile;
}
set
{
mobile = value;
}
}
public
string
N_name
{
get
{
return
name;
}
set
{
name = value;
}
}
public
string
S_sirname
{
get
{
return
sirname;
}
set
{
sirname = value;
}
}
public
string
E_email
{
get
{
return
email;
}
set
{
email = value;
}
}
public
string
C_companyname
{
get
{
return
companyname;
}
set
{
companyname = value;
}
}
public
string
P_post
{
get
{
return
post;
}
set
{
post = value;
}
}
public
int
A_Action
{
get
{
return
Actions;
}
set
{
Actions = value;
}
}
public
string
T_title
{
get
{
return
title;
}
set
{
title = value;
}
}
public
string
P_postcode
{
get
{
return
postcode;
}
set
{
postcode = value;
}
}
#endregion
}
Del class
public
int
AddEditUser(AdminMaster objUser)
{
int
result = 0;
using
(SqlConnection myConnection =
new
SqlConnection(WebConnection.ConnectionString))
{
try
{
SqlCommand myCommand =
new
SqlCommand(
"register_insert"
, myConnection);
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue(
"reg_id"
, objUser.R_reg_id);
myCommand.Parameters.AddWithValue(
"name"
, objUser.N_name);
myCommand.Parameters.AddWithValue(
"sirname"
, objUser.S_sirname);
myCommand.Parameters.AddWithValue(
"user_id"
, objUser.U_user_id);
myCommand.Parameters.AddWithValue(
"password"
, objUser.P_password);
myCommand.Parameters.AddWithValue(
"email"
, objUser.E_email);
myCommand.Parameters.AddWithValue(
"companyname"
, objUser.C_companyname);
myCommand.Parameters.AddWithValue(
"post"
, objUser.P_post);
myCommand.Parameters.AddWithValue(
"mobile"
, objUser.M_mobile);
myCommand.Parameters.AddWithValue(
"usertype"
, objUser.u_usertype);
//myCommand.Parameters.AddWithValue("createdon", objUser.c_createdon);
myCommand.Parameters.AddWithValue(
"Actions"
, objUser.A_Action);
DbParameter returnValue;
returnValue = myCommand.CreateParameter();
returnValue.Direction = ParameterDirection.ReturnValue;
myCommand.Parameters.Add(returnValue);
myConnection.Open();
myCommand.ExecuteNonQuery();
result = Convert.ToInt32(returnValue.Value);
}
catch
(Exception ex)
{
ErrorLogDAL objErrorLogManager =
new
ErrorLogDAL();
objErrorLogManager.AddErrorLog(ex);
objErrorLogManager =
null
;
return
0;
}
finally
{
myConnection.Dispose();
}
}
return
result;
}
Cs class
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
namespace
babbook
{
public
partial
class
register : System.Web.UI.Page
{
protected
void
Page_Load(
object
sender, EventArgs e)
{
}
protected
void
btnsubmit_Click(
object
sender, EventArgs e)
{
Page.Validate();
if
(Page.IsValid)
{
AdminMaster objRegister =
new
AdminMaster();
AdminDB objDb =
new
AdminDB();
int
saved = 0;
//objRegister.c_cust_id = Convert.ToInt32(lblid.Text.Trim());
objRegister.N_name = txtname.Text.Trim();
objRegister.S_sirname = txtsirname.Text.Trim();
objRegister.U_user_id = txtuser.Text.Trim();
objRegister.P_password = txtpassword.Text.Trim();
objRegister.M_mobile = txtmobile.Text.Trim();
objRegister.E_email = txtmail.Text.Trim();
objRegister.C_companyname = txtcompany.Text.Trim();
// objRegister.u_usertype = txtuser.Text.Trim();
objRegister.P_post = txtpost.Text.Trim();
objRegister.u_usertype = lbluser.Text.Trim();
if
(btnsubmit.Text ==
"Submit"
)
{
objRegister.A_Action = 1;
}
else
{
objRegister.A_Action = 2;
}
saved = Convert.ToInt32(objDb.AddEditUser(objRegister));
if
(saved == 1)
{
lblsms.Text =
" Account created successfully!.."
;
}
else
if
(saved == 2)
{
//lblErrorMessage.Text = "Duplicate Holiday Please Enter New Hploday ";
//Page.SetFocus(txtDate);
}
else
{
lblsmss.Text =
" User Already Exist"
;
Page.SetFocus(txtname);
}
objRegister =
null
;
objDb =
null
;
}
}
}
}
SP SQL
USE [learning]
GO
/****** Object: StoredProcedure [dbo].[register_insert] Script
Date
: 10/04/2014 13:24:25 ******/
SET
ANSI_NULLS
ON
GO
SET
QUOTED_IDENTIFIER
ON
GO
ALTER
Procedure
[dbo].[register_insert]
@reg_id
bigint
,
@
name
varchar
(30),
@sirname
varchar
(30),
@mobile
varchar
(50),
@user_id
varchar
(10),
@
password
varchar
(50),
@post
varchar
(50),
@email
varchar
(40),
@companyname
varchar
(30),
@usertype
varchar
(8),
@Actions
int
As
BEGIN
IF( @Actions = 1 )
-- INSERT
BEGIN
if
not
exists(
Select
1
from
register_mst
where
CONVERT
(
varchar
,user_id)=
CONVERT
(
varchar
,@user_id))
begin
INSERT
INTO
register_mst(
name
,sirname,Email,user_id,
password
,mobile,companyname,usertype,post,Actions)
VALUES
(@
name
,@sirname,@Email,@user_id,@
password
,@mobile,@companyname,@usertype,@post,@Actions)
IF ( @@ERROR > 0 )
BEGIN
ROLLBACK
TRAN
RETURN
(0)
END
RETURN
(1)
end
ELSE
RAISERROR(
'Validation Failed: User ID already Exists'
,16,1)
BEGIN
RETURN
(2)
END
END
ELSE
IF( @Actions = 2 )
-- UPDATE
BEGIN
if
not
exists(
Select
1
from
register_mst
where
CONVERT
(
varchar
,user_id)=
CONVERT
(
varchar
,@user_id))
begin
-- UPDATE register_mst SET firstname=@firstname,lastname= @lastname,Email=@Email,password=@password,
-- gender= @gender,dob= @dob,usertype= @usertype,Actions=@Actions
-- WHERE cust_id = @cust_id
If(@@ERROR> 0)
BEGIN
ROLLBACK
TRAN
RETURN
(0)
END
RETURN
(1)
end
ELSE
RAISERROR(
'Validation Failed: User Name already Exists'
,16,1)
BEGIN
RETURN
(2)
END
END
END
Asp.Net
Insert Data 3-Tire in Asp.Net