C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Forums » .NET » SQL Server »

Stored Procedure..? - 1


Posted Date: 26 Aug 2008      Posted By: venkatesan      Member Level: Diamond     Points: 1   Responses: 6



Hi

Let me know suppose

i have query like check the email exist in database or not..

for that i need SP with i have to check in Stored Procedure itself

ie: if the SP return = 0 it should no show the msg like "Email does not exist "

else it shows "Email Already Exist"

Generally we write the stored procedure

and using the result of the SP we will check in Code

if i=0
msg show "Email doesnot exist"

else
Already exist

But i need the above should be get it from Stored procedure itself..

Thanks

Regards,
M.Venkatesan.
Dot Net Code Snippets





Responses

Author: Reddy    26 Aug 2008Member Level: SilverRating: 2 out of 52 out of 5     Points: 1

It means u have the column of the email in ur table(database)


Author: rajitha    26 Aug 2008Member Level: SilverRating: 2 out of 52 out of 5     Points: 6

hai,i think you are asking about check availability right?

write this stored procedure for this one:


CREATE PROCEDURE pro_registration


@dname VARCHAR(50),
@emailid VARCHAR(50),
@pwd VARCHAR(50)

AS
declare @temp int
BEGIN

select @temp = max(userid) from tbl_registration

if(@temp is null)
set @temp=1
else
set @temp=@temp+1

INSERT INTO tbl_registration(userid,displayname,Emailid,password)
VALUES(@temp,@dname,@emailid,@pwd)
insert into tb_login(username,password) values(@emailid,@pwd)
END
GO


call this stored procedure in front end:
ok

protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("initial catalog=rajitha;data source=sys01;user id=sa");
con.Open();
string str = "select Emailid from tbl_registration where Emailid='" + TextBox3.Text + "'";
SqlCommand cmd1 = new SqlCommand();
cmd1.Connection = con;
cmd1.CommandText = str;
cmd1.CommandType = CommandType.Text;
// cmd1.Parameters.Add("@eid", System.Data.SqlDbType.VarChar).Value = TextBox3.Text.ToString();
SqlDataReader dr = cmd1.ExecuteReader();
string uname = TextBox3.Text;
if(dr.Read())
{
if (uname == dr["Emailid"].ToString())

// Session["Emailid"] = TextBox3.Text;
Response.Write("username already exists");
return;
//cmd1.ExecuteReader();
}
else
{
SqlConnection conn = new SqlConnection("initial catalog=rajitha;data source=sys01;user id=sa");
conn.Open();
SqlCommand cmd = null;
using (conn)
{
using (cmd = new SqlCommand())
{
// cmd.CommandType = System.Data.CommandType.Text;
string storedprocedure = "pro_registration";
// string str = "FORUMS_LOGINREGISTRATION";
cmd.Connection = conn;
cmd.CommandText = storedprocedure;
cmd.CommandType = CommandType.StoredProcedure;


//"INSERT INTO tb_registration(userid,displayname,Emailid,passwor d) VALUES (@temp,@dname,@emailid,@pwd)";
try
{
cmd.Parameters.Add("@dname", System.Data.SqlDbType.VarChar, 50).Value = TextBox1.Text.ToString();
cmd.Parameters.Add("@emailid", System.Data.SqlDbType.VarChar, 50).Value = TextBox3.Text.ToString();
cmd.Parameters.Add("@pwd", System.Data.SqlDbType.VarChar, 50).Value = TextBox4.Text.ToString();
cmd.ExecuteScalar();
}
catch (Exception ex)
{
throw ex;
}
finally
{
cmd.Dispose();
conn.Close();
}
}


}



Server.Transfer("Default4.aspx");
}

con.Close();
}
this is for inserting data into database whatever you entered in your textboxes and also
for checking whether the user is already exists or not.

hope this will help for you



Author: rajitha    26 Aug 2008Member Level: SilverRating: 2 out of 52 out of 5     Points: 6

hai,i think you are asking about check availability right?

write this stored procedure for this one:


CREATE PROCEDURE pro_registration


@dname VARCHAR(50),
@emailid VARCHAR(50),
@pwd VARCHAR(50)

AS
declare @temp int
BEGIN

select @temp = max(userid) from tbl_registration

if(@temp is null)
set @temp=1
else
set @temp=@temp+1

INSERT INTO tbl_registration(userid,displayname,Emailid,password)
VALUES(@temp,@dname,@emailid,@pwd)
insert into tb_login(username,password) values(@emailid,@pwd)
END
GO


call this stored procedure in front end:
ok

protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("initial catalog=rajitha;data source=sys01;user id=sa");
con.Open();
string str = "select Emailid from tbl_registration where Emailid='" + TextBox3.Text + "'";
SqlCommand cmd1 = new SqlCommand();
cmd1.Connection = con;
cmd1.CommandText = str;
cmd1.CommandType = CommandType.Text;
// cmd1.Parameters.Add("@eid", System.Data.SqlDbType.VarChar).Value = TextBox3.Text.ToString();
SqlDataReader dr = cmd1.ExecuteReader();
string uname = TextBox3.Text;
if(dr.Read())
{
if (uname == dr["Emailid"].ToString())

// Session["Emailid"] = TextBox3.Text;
Response.Write("username already exists");
return;
//cmd1.ExecuteReader();
}
else
{
SqlConnection conn = new SqlConnection("initial catalog=rajitha;data source=sys01;user id=sa");
conn.Open();
SqlCommand cmd = null;
using (conn)
{
using (cmd = new SqlCommand())
{
// cmd.CommandType = System.Data.CommandType.Text;
string storedprocedure = "pro_registration";
// string str = "FORUMS_LOGINREGISTRATION";
cmd.Connection = conn;
cmd.CommandText = storedprocedure;
cmd.CommandType = CommandType.StoredProcedure;


//"INSERT INTO tb_registration(userid,displayname,Emailid,passwor d) VALUES (@temp,@dname,@emailid,@pwd)";
try
{
cmd.Parameters.Add("@dname", System.Data.SqlDbType.VarChar, 50).Value = TextBox1.Text.ToString();
cmd.Parameters.Add("@emailid", System.Data.SqlDbType.VarChar, 50).Value = TextBox3.Text.ToString();
cmd.Parameters.Add("@pwd", System.Data.SqlDbType.VarChar, 50).Value = TextBox4.Text.ToString();
cmd.ExecuteScalar();
}
catch (Exception ex)
{
throw ex;
}
finally
{
cmd.Dispose();
conn.Close();
}
}


}



Server.Transfer("Default4.aspx");
}

con.Close();
}
this is for inserting data into database whatever you entered in your textboxes and also
for checking whether the user is already exists or not.

hope this will help for you



Author: venkatesan    26 Aug 2008Member Level: DiamondRating: 2 out of 52 out of 5     Points: 1

Hi

wat is the use of Temp

wat u are going to do using temp..

let u clearly explain..

Regards,
M.Venkatesan.
Dot Net Code Snippets



Author: venkatesan    26 Aug 2008Member Level: DiamondRating: 2 out of 52 out of 5     Points: 4

I think u create the stored procedure

if there is no record assigning userid=1 else

increment the userid from the Max(userid)

BUT My Requirement is If the email typed by the user

is exist or not .. ie Going to check in the Stored procedure itself..?

Regards,
M.Venkatesan.
Dot Net Code Snippets



Author: rajitha    26 Aug 2008Member Level: SilverRating: 2 out of 52 out of 5     Points: 6

check this one

CREATE PROCEDURE FORUMS_LOGINREGISTRATION
@PV_DISPLAYNAME VARCHAR(50),
@PV_EMAILID VARCHAR(50),
@PV_PASSWORD VARCHAR(50)
AS
BEGIN

DECLARE @LV_USERID INT


IF EXISTS(SELECT EMAILID FROM TBL_REGISTRATION WHERE EMAILID=@PV_EMAILID)
BEGIN

SELECT '0'
END
ELSE
BEGIN
BEGIN TRAN
INSERT INTO TB_LOGIN(USERNAME,PASSWORD)VALUES(@PV_EMAILID,@PV_PASSWORD)

IF(@@ERROR!=0)
GOTO ERRHANDLER

SET @LV_USERID=SCOPE_IDENTITY()

INSERT INTO TBL_REGISTRATION(USERID,DISPLAYNAME)
VALUES(@LV_USERID,@PV_DISPLAYNAME)

IF(@@ERROR!=0)
GOTO ERRHANDLER

ELSE
BEGIN
COMMIT TRAN
SELECT @LV_USERID
END

END


ERRHANDLER:
ROLLBACK TRAN
RETURN

END
GO

this will help you ok



Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.


Next : resolve this using pivot in sql 2005
Previous : Data Modeling
Return to Discussion Forum
Post New Message
Category: SQL Server

Related Messages



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use