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

74 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 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

Unknown 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.

Unknown 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

Unknown 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.."

Unknown said...

nice post...you can visit following link to get full proof code of how to store data in database using stored procedure and 3-tier architecture...

Click Here

Unknown said...
This comment has been removed by the author.
Unknown said...
This comment has been removed by the author.
Unknown said...

hey can u please provide the code on button onclick in vb,since the one you have provided is in c#

Swabhiman said...

Sir Please Help Me To Make A Project On A Social Networking Website Project For College Using Asp.net, C# in Visual Studio 2010 along with Ms Sql Server 2005. Please Provide Me The Source Code For The Website.

awey said...

suresh please i need it video and document upload and download form the database

Anonymous said...

sir this is C# or VB?

Jessy said...

Sir.. how to fix the 'message'?
The name 'message' does not exist in the current context.

http://s11.postimg.org/kh97a30r7/error_msg.jpg

Anonymous said...

I want the same code in vb

Venu said...

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

Unknown said...

Error in line using vb.nt Please help me
cmd.Parameters["@ERROR"].Direction = ParameterDirection.Output;

Unknown said...

sir i am new to asp.net c# and i want to save data from asp page to sqlserver2008 from radiobutton,dropdownlist,checkbox and datetimepicker so plz help me with the coding...

Abu-Safiyya said...

Hello Sir Suresh Dasari,
Please could you help me with the vb.net version of this code, because I got error in the first line:
cmd.Parameters.Add("@ERROR", SqlDbType.Char, 500)

Thank you very much for the help

Senthil said...

I have a form with fields outside gridview and some fields inside gridview.Is it possible to save all the fields within the form to a database???Kindly provide a solution.Thanks in advance

Unknown said...

hey i want to develop registrtion form like this we alreday have the fade text like first name as in facebook registration page and as we enter our deatils that fade text moves away ...is this possible to create this kind of registration page in asp.net??

Anonymous said...

very nice articles......

Anonymous said...

Nice article for beginn to mid level. That is what a dynamic pages do all the time. Only would like to add that when I take end user's inputs I always use Server.HtmlEncode(sring taken from user). That very little line reduces lots of risks of users entering java script injections ot SQL hacking injections.

Unknown said...

shanthi
very nice article sir....i want to know how to view the registered details...please provide registred data how to view...

protected void btnviewdetails_Click(object sender, EventArgs e)
{
Response.Redirect("~/ViewDetails.aspx");


}

Unknown said...

count = cmd.ExecuteNonQuery(); returns negative value.....
plz help me sir

Unknown said...

The name 'message' does not exist in the current context.
help me sir

Unknown said...

Warning 2 'System.Web.UI.Page.RegisterStartupScript(string, string)' is obsolete: 'The recommended alternative is ClientScript.RegisterStartupScript(Type type, string key, string script). http://go.microsoft.com/fwlink/?linkid=14202' F:\Online Gift\WebApplication1\Registrationaspx.aspx.cs 62 21 WebApplication1
Help me sir

vishnu pathare said...

i want to use also Date of birth in database can you help me how to write code in c# also using store procedure

Unknown said...

in code behind c# coding.., 4th line from bottom.., what is "Page.RegisterStartupScript". bcz it show error in my app when i try this code. pls tell me brother... what to use or this is default ???

Gourav Aggarwal said...

Sir, Registration form running good.
after that i need to retrieve registration form data and print this registration form.
For example:
I am create a page 'application-form.aspx' and need to print this application form after the form is submitted for student.
please sir, reply.

Anonymous said...

thank u......

Unknown said...

i like this
but i don't understand how to search data when enter employee id in search textbox and click on search button then get all details in one webpage and employee id show in lock or disable so we can edit all details but not edit ID

PLZ HELP ME HOW CAN I DO THIS....?

if you have url link plz give me

Anonymous said...

count = cmd.ExecuteNonQuery(); give the error as Invalid object name 'User_Information'.

Unknown said...

thank u......

Unknown said...

sir now i am create MLM PROJECT data entry web application.
E.G.: first i generate ID for 1st client then Like here IDS IME001, NOW WHEN IME001 PERSON IS BRING OTHER CLIENT, than i generate ID for 2nd person is IME002 and I set her of IME001 of left coloume, again IME001 is bring one more client his ID IS IME003 AND I SET HIS IMEOO1 OF RIGHT COLOUME

now how can i see when I SEARCH IME001 details left and right client ..?
I want display in SQL like this BUT when i go to sql and select table then result not come like see second table for example...?

TABLE : 1 I WANT TO SHOW IN SQL LIKE THIS
ime_id name left_Join right_join this is a right table
IME001 SANDESH IME002 IME003
IME 002 AMIT
IME003 SACHIN


TABLE ; 2 : THIS RESULT SHOW IN MY TABLE....?
IME_ID NAME IME_LEFT IME_RIGHT
IME001 SANDESH
IME002 AMIT
IME003 SACHIN

sir , now what can i do ....? how can ime002 and ime003 is insert into IME001 OF left and right cololum...?

please reply sir if you have any c# code and url for MLM web application give me

i am waiting your reply

Unknown said...

sir plz reply my question

Unknown said...

I m trying to insert record into database but password & confirm password in password mode doesnt saved in the database I have tried it with different browser also but it is not working ,so what I have to do to save it.

Unknown said...

hi sir how to send a confirmation email after registeration

Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...

me rohit 27 sing male frm india city agra lived alone with mine uncle nd aunt loved to hear hindi songs in mine lonlyness on mine lappy
ny honest decent matured fem while u married or aged too wanna yrs lovly salves interested in trure longterm friendship online or in real life
add me in yrs yahoo mesenger mine yahoo id is merohit2725@yahoo.com make mine dream true pls

soundaryaharee said...

if i want to learn complete asp.net what should i do???

Unknown said...
This comment has been removed by the author.
Anonymous said...

can u provide me a final year project

aiswarya said...

Hi,
I am trying to retrieve data from database to textboxes on UI and updating the database with new values via textboxes,However it is taking values and displaying it.But when I edit and click on submit button, values are not getting updated in database.

aiswarya said...

protected void Page_Load(object sender, EventArgs e)
{
if (!((Page)System.Web.HttpContext.Current.CurrentHandler).IsPostBack)

{
getAuthDetails();
}
}
private void getAuthDetails()
{
DataTable dt = new DataTable();
objBE.ClientId = "1222";
dt = objBal.Showdata_Authorized(objBE);
tbx_title_update.Text = dt.Rows[0]["Title"].ToString();
tbx_firstname_update.Text = dt.Rows[0]["FirstName"].ToString();
tbx_lastname_update.Text = dt.Rows[0]["LastName"].ToString();
tbx_designation_update.Text = dt.Rows[0]["Designation"].ToString();
tbx_contactno_update.Text = dt.Rows[0]["ContactNo"].ToString();
tbx_faxno_update.Text = dt.Rows[0]["Fax_No"].ToString();
tbx_emailId_update.Text = dt.Rows[0]["EmailId"].ToString();
tbx_address_update.Text = dt.Rows[0]["Address"].ToString().Replace(",", Environment.NewLine);

tbx_city_update.Text = dt.Rows[0]["City"].ToString();
tbx_state_update.Text = dt.Rows[0]["State"].ToString();
// after Union ALL query

tbx_title_update02.Text = dt.Rows[1]["Title"].ToString();
tbx_firstname_update02.Text = dt.Rows[1]["FirstName"].ToString();
tbx_lastname_update02.Text = dt.Rows[1]["LastName"].ToString();
tbx_designation_update02.Text = dt.Rows[1]["Designation"].ToString();
tbx_contactno_update02.Text = dt.Rows[1]["ContactNo"].ToString();
tbx_faxno_update02.Text = dt.Rows[1]["Fax_No"].ToString();
tbx_emailId_update02.Text = dt.Rows[1]["EmailId"].ToString();
tbx_address_update02.Text = dt.Rows[1]["Address"].ToString().Replace(",", Environment.NewLine);
tbx_city_update02.Text = dt.Rows[1]["City"].ToString();
tbx_state_update02.Text = dt.Rows[1]["State"].ToString();

}

protected void btn_submit_Click(object sender, EventArgs e)
{
// lbl_title_update.Text = "Records updated !!!!!!!!!!!!!";
int result=0;
objBE.ClientId = "1222";
objBE.Title = tbx_title_update.Text;
objBE.FirstName = tbx_firstname_update.Text;
objBE.LastName = tbx_lastname_update.Text;
objBE.Designation = tbx_designation_update.Text;
objBE.ContactNo = tbx_contactno_update.Text;
objBE.FaxNo = tbx_faxno_update.Text;
objBE.EmailId = tbx_emailId_update.Text;
objBE.Address = tbx_address_update.Text;
objBE.City = tbx_city_update.Text;
objBE.State = tbx_state_update.Text;

result = objBal.UpdateData_Authorized(objBE);
if(result==1)
{
lbl_title_update.Text = "Records updated !!!!!!!!!!!!!";
}
}

The above is the codebehind file for my page,when i comment method in the pageload I get empty text boxes,n values from those empty text boxes are getting updated in database..
and Stored procedure to update is as follows:
CREATE procedure [dbo].[UpdateData_Authorized_GRP_SP]
(
@ClientId varchar(25),
@Title Varchar (50),
@FirstName varchar(50),
@LastName varchar (50),
@Designation Varchar (50),
@ContactNo Varchar (50),
@Fax_No varchar (50),
@EmailId Varchar (50),
@Address Varchar (100),
@City varchar (50),
@State Varchar (50)
)
as
begin
UPDATE dbo.SUD_GA_Authorized_Client SET Title=@Title,FirstName=@FirstName,LastName=@LastName,Designation=@Designation,ContactNo=@ContactNo,Fax_No=@Fax_No,EmailId=@EmailId,[Address]=@Address,City=@City,[State]=@State where ClientId=@ClientId

End

Anonymous said...

Hello,
I want to display the latest submitted details into next page on clicking submit button.
Can anyone help me with the code?

Unknown said...

where to keep @error in code behind plz let me i am new to .net plz

Unknown said...

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



Where to copy dis?in code behined means

sujjy said...

For the Error Message, you need to copy below
lblErrorMsg.Text = message;
after the con.Close();
For eg: i have pasted below the updated code:




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();
lblErrorMsg.Text = message;


=============================================================
This way you are calling the string message within the method btnsubmit_Click, hence no error

maagesan said...

sir can i get the vb code for this

Anonymous said...

ScriptManager.RegisterStartupScript(Me, [GetType](), "showalert", "alert('New Job No is.' +'" & Icode1 & "');", True)

not working plz help

Anonymous said...

can i have the sp code for this

Anonymous said...

Sir I try to change your coding to vb but the 'lblmssg' never showed up. What should I do?

Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Partial Class SignUp
Inherits System.Web.UI.Page
Dim con As New SqlConnection("Data Source=LAPTOP-O6U7LT0S\SQLEXPRESS;Initial Catalog=REGISTER; Integrated Security= True")
Dim cmd As New SqlCommand
Dim rd As SqlDataReader
Dim dt As New DataTable()
Dim message As String

Protected Sub Login_Click(sender As Object, e As EventArgs) Handles Login.Click

con.Open()
Using cmd As New SqlCommand("sp_userinformation", con)
cmd.CommandText = "Insert into UserAccount(Username,Password,Email,Phone)values ('" & txtname.Text & "','" & txtpassword.Text & "','" & txtemail.Text & "','" & txtphone.Text & "')"
cmd.Parameters.AddWithValue("@Username", txtname.Text)
cmd.Parameters.AddWithValue("@Password", txtpassword.Text)
cmd.Parameters.AddWithValue("@Email", txtemail.Text)
cmd.Parameters.AddWithValue("@Phone", txtphone.Text)
cmd.Parameters.Add("@ERROR", SqlDbType.Char, 500)
cmd.Parameters("@ERROR").Direction = ParameterDirection.Output
cmd.ExecuteNonQuery()
message = Convert.ToString(cmd.Parameters("@ERROR").Value)
lblErrorMsg.Text = message
con.Close()
End Using

End Sub

Unknown said...

Sir , Can you please tell me how to access this output parameters in 3tier architecture.
i tried i couldnt get,Can u reply me?

Anonymous said...

what is txtcnmpwd.Text?..........plz tell me

stevenjocb said...

good article.

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 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.