Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Registration form example to insert user details in database using asp.net

Oct 24, 2010
Introduction

Here I will explain how to insert registered user details in database and how to get output parameters returned by SQL Query in asp.net.

Description

I have one registration form that give a chance to register new user .In registration form user needs to enter fields UserName, Password, Confirm Password like this I have nearly ten fields are there now I need to check whether this username already exists or not if username not exists I need to register user otherwise I need to display message like username already exists I am doing this thing using query. Here is the link it will explain how to write sql query to return output parameters 

http://www.aspdotnet-suresh.com/2010/10/introduction-here-i-will-explain-how-to.html

Now I need to display that output parameter message during user registration in asp.net. How to get that output parameter returned by SQL query.

To get output parameters in asp.net we need to write statements like this in codebehind

cmd.Parameters.Add("@ERROR", SqlDbType.Char, 500);
cmd.Parameters["@ERROR"].Direction = ParameterDirection.Output;
string message = (string) cmd.Parameters["@ERROR"].Value;
Here @ERROR is output parameter name returned from query

For sample design your aspx page like this 

aspx page

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>OutPut Pasrameters Sample</title>
</head>
<body>
<form id="form1" runat="server">  
<table align="center">
<tr>
<td></td>
<td align="right" >
</td>
<td align="center">
<b>Registration Form</b>
</td>
</tr>
<tr>
<td></td>
<td align="right" >
<asp:Label ID="lbluser" runat="server" Text="Username"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtuser" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td></td>
<td align="right" >
<asp:Label ID="lblpwd" runat="server" Text="Password"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtpwd" runat="server" TextMode="Password"></asp:TextBox>
</td>
</tr>
<tr>
<td></td>
<td align="right" >
<asp:Label ID="lblcnfmpwd" runat="server" Text="Confirm Password"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtcnmpwd" runat="server" TextMode="Password"></asp:TextBox>
</td>
</tr>
<tr>
<td></td>
<td align="right">
<asp:Label ID="lblfname" runat="server" Text="FirstName"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtfname" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td></td>
<td align="right">
<asp:Label ID="lbllname" runat="server" Text="LastName"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtlname" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td></td>
<td align="right">
<asp:Label ID="lblemail" runat="server" Text="Email"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td></td>
<td align="right" >
<asp:Label ID="lblCnt" runat="server" Text="Phone No"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtphone" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td></td>
<td align="right" >
<asp:Label ID="lbladd" runat="server" Text="Location"></asp:Label>
</td>
<td align="left">
<asp:TextBox ID="txtlocation" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td></td>
<td></td>

<td align="left" ><asp:Button ID="btnsubmit" runat="server" Text="Save"
onclick="btnsubmit_Click" />
<input type="reset" value="Reset" />
</td>
</tr>
<tr>
<td></td>
<td></td>
<td>
<span style= "color:Red; font-weight :bold"> <asp:Label ID="lblErrorMsg" runat="server"></asp:Label></span>
</td>
</tr>
</table>
</form>
</body>
</html>
In Codebehind write following code in btnsubmit_Click like this 

Codebehind


protected void btnsubmit_Click(object sender, EventArgs e)
{
if (txtpwd.Text == txtcnmpwd.Text)
{
string UserName = txtuser.Text;
string Password = txtpwd.Text;
string ConfirmPassword = txtcnmpwd.Text;
string FirstName = txtfname.Text;
string LastName = txtlname.Text;
string Email = txtEmail.Text;
string Phoneno = txtphone.Text;
string Location = txtlocation.Text;
string Created_By = txtuser.Text;
SqlConnection con = new SqlConnection("Data Source=MYCBJ017550027;Initial Catalog=MySamplesDB;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand("sp_userinformation", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@UserName", UserName);
cmd.Parameters.AddWithValue("@Password", Password);
cmd.Parameters.AddWithValue("@FirstName", FirstName);
cmd.Parameters.AddWithValue("@LastName", LastName);
cmd.Parameters.AddWithValue("@Email", Email);
cmd.Parameters.AddWithValue("@PhoneNo", Phoneno);
cmd.Parameters.AddWithValue("@Location", Location);
cmd.Parameters.AddWithValue("@Created_By", Created_By);
cmd.Parameters.Add("@ERROR", SqlDbType.Char, 500);
cmd.Parameters["@ERROR"].Direction = ParameterDirection.Output;
cmd.ExecuteNonQuery();
message = (string) cmd.Parameters["@ERROR"].Value;
con.Close();
}
else
{
Page.RegisterStartupScript("UserMsg", "<Script language='javascript'>alert('" + "Password mismatch" + "');</script>");
}
lblErrorMsg.Text = message;
}
Here sp_userinformation is name of stored procedure created in database and@ERROR is output parameter name returned by query if you want to write query to insert data and return output parameters check this link

Click Here

After completion of everything run your page that will appear like this 

Demo


If you want sample code 

Download sample code attached

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

25 comments :

Nikoo said...

Hi, you have answered my question in
http://forums.asp.net/t/1648768.aspx
Thanks, I’m not sure why I get error when I try to return a value I have a new post in
http://forums.asp.net/t/1651886.aspx
with the code. Can you please help me.
My email address is: nikoo56@yahoo.com
Thanks!

Anonymous said...

u r genius if i want to learn complete asp.net from what should i do.....????

Suresh Dasari said...

hi if you want to learn complete .NET first you should start doing all the sample mentioned in this blog because i covered all the samples based on concepts. Once you complete all the samples you will get good knowledge on .NET first thing you should start doing samples.

uyir said...

hey everything is working good, but while signing in it shows no eroor but didnt move to the redirection page , please guide me

uyir said...

sorry everything is worked fine . i found out my error , thanks for your tutorial , pls don stop

sheshagiri said...

sir show about encryption/decryption value of textbox while inserting/retriving the values from database

Suresh Dasari said...

@Sheshagiri...
Check this article
http://www.aspdotnet-suresh.com/2010/12/introduction-here-i-will-explain-how-to_28.html

Anonymous said...

sir wat if i use vb.net for the same function. how the code will be like??

shalinirajapandy said...

Please Help Me How to Redirect the Page.... By Using this Coding

Anonymous said...

Sir, i have created an user registration form in html. and i want to save the details to MS SQL Server using ASP or any other technology. And i want to run the page only using html. help me. Thank you.

mpaul said...

Hi sir suresh can you please add vb.net code. thanks!

Balaji said...

Hello suresh sir
this info is very useful for me but i want one more page coding for forgot password using security question and data of birth..

Anonymous said...

sir i want to convert this registeration form into pdf..plz help me...

Anonymous said...

The request for procedure 'sp_userinformation' failed because 'sp_userinformation' is a table object.

what does it mean? if u have time plz must reply...

Anonymous said...

how to store text file into sql server database using c# windows application

Anonymous said...

how to separating a word in text file and stored by separate column into sql server database using c# windows application.. plz reply.. thank u.....

Anonymous said...

when i use this code it shows message doesn't exist in current context.what is the problem.Pls help

Anusha Perike said...

hii ..sir iam Anusha fresher , i created one asp.net page (login form contains userid , password&login button)how to execute that by using database sql server need commands plzz reply..

Anonymous said...

i want code to add comments in asp page.... just like the comment i m posting here... can any1 help....

Anonymous said...

how can I join u on facebook.

Benard Koech said...

hi sir please help me create a login form using the data from the signup database

Anonymous said...

when i try putting lblErrorMsg.Text = message;
it shows me error saying "message" does not exist in current context.

Help me with the same

Priyanka Dongare said...

sir please help me to create registration form with Radio button and datepicker

Sumi said...

it gives me an error saying 'message does not exist in the context'.....Please help

Anonymous said...

I found the error "A field initializer cannot reference the non-static field, method or property.."

Give your Valuable Comments

Other Related Posts

© 2010-2012 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.