I have a web application which has a registration page and it asks for an user id. I have set the userid column as unique in sql server. So If a user enters a user id which is already present in the database then an error message should be thrown (an exception)
.Right now the the control is thrown back to visual studio, which is bcoz the exception is not handled
Can someone tell me how to handle the exception: the code for the registration page is below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
public partial class register : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string str = "";
for (int i = 1900; i < 2012; i++)
{
str = Convert.ToString(i);
year_ddl.Items.Add(str);
}
string str1 = "";
for (int i = 1; i < 13; i++)
{
str1 = Convert.ToString(i);
month_ddl.Items.Add(str1);
}
string str2 = "";
for (int i = 1; i < 32; i++)
{
str2 = Convert.ToString(i);
day_ddl.Items.Add(str2);
}
}
protected void register_btn_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=MUKUNDANPC\\SQLEXPRESS;Initial Catalog=lms_db;Integrated Security=True;";
SqlCommand cmd = new SqlCommand();
string year_x = Convert.ToString(year_ddl.ToString());
string month_x = Convert.ToString(month_ddl.ToString());
string day_x = Convert.ToString(day_ddl.ToString());
string dob1 = year_x + "/" + month_x + "/" + day_x;
cmd.CommandText = "INSERT into users (fname,lname,gender,dob,userid,pwd,phone_no,address,email_id) VALUES ('" + fname_tb.Text + "','" + lname_tb.Text + "','" + gender_rbl.SelectedValue.ToString() + "','" + dob1 + "','" + userid_tb.Text + "','" + pwd_tb.Text + "','" + phno_tb.Text + "','" + address_tb.Text + "','" + mail_tb.Text + "')";
cmd.Connection = conn;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
}
try{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=MUKUNDANPC\\SQLEXPRESS;Initial Catalog=lms_db;Integrated Security=True;";
SqlCommand cmd = new SqlCommand();
string year_x = Convert.ToString(year_ddl.ToString());
string month_x = Convert.ToString(month_ddl.ToString());
string day_x = Convert.ToString(day_ddl.ToString());
string dob1 = year_x + "/" + month_x + "/" + day_x;
cmd.CommandText = "INSERT into users (fname,lname,gender,dob,userid,pwd,phone_no,address,email_id) VALUES ('" + fname_tb.Text + "','" + lname_tb.Text + "','" + gender_rbl.SelectedValue.ToString() + "','" + dob1 + "','" + userid_tb.Text + "','" + pwd_tb.Text + "','" + phno_tb.Text + "','" + address_tb.Text + "','" + mail_tb.Text + "')";
cmd.Connection = conn;
conn.Open();
cmd.ExecuteNonQuery();
}
catch (Sqlexception ex)
{
<Message showing that the user id is unique>)
}
conn.Close();
<javascript code which says that registration was successfull and redirects to another page>
So after the message for exception is shown i want the user to be on the registration page so that he can enter a unique user id . How do i make sure that the user stays on the page and the next two lines are executed when the user id is unique?
NOW That is not my question. I have handled an SqlException for Unique Key constraint and the message is displayed using a line of javascript. But i want the registration page to reload and retain the same values , using sessions didnt really
help. the javascript code redirected to the same page, but didnt retain the values...why?
and just have three mode in sp as A-Add,M-Modify, D-Delete.
when db Mode is A or M then just check like this
IF Exists(SELECT Pk_ID from tbl_registeration Where user_Id=@user_Id)
BEGIN
SET @ErrMsg='user Id already exists' -- this should be your OUT parameter so that you fetch it into appl.
RETURN 0
END
try{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=MUKUNDANPC\\SQLEXPRESS;Initial Catalog=lms_db;Integrated Security=True;";
SqlCommand cmd = new SqlCommand();
string year_x = Convert.ToString(year_ddl.ToString());
string month_x = Convert.ToString(month_ddl.ToString());
string day_x = Convert.ToString(day_ddl.ToString());
string dob1 = year_x + "/" + month_x + "/" + day_x;
Session["firstname"]=fname_tb.Text;
cmd.CommandText = "INSERT into users (fname,lname,gender,dob,userid,pwd,phone_no,address,email_id) VALUES ('" + fname_tb.Text + "','" + lname_tb.Text + "','" + gender_rbl.SelectedValue.ToString() + "','" + dob1 + "','" + userid_tb.Text + "','" + pwd_tb.Text + "','" + phno_tb.Text + "','" + address_tb.Text + "','" + mail_tb.Text + "')";
cmd.Connection = conn;
conn.Open();
cmd.ExecuteNonQuery();
}
catch (Sqlexception ex)
{
this.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('User ID has to be Unique'); window.location.href = 'registration.aspx';", true);
<here some how i have to retrieve the value from the session and place it into the same textbox>
}
conn.Close();
the problem is when i store the values in a session, then the unique key exception occours and i try to retrieve the values from the session into the same page , i get a blank page . Does it have something to do with the fact that the javascript line redirects
the page to the same page?
Can you tell me how to check for an unique user id before the insertion? My original question was regarding retaining the values after the javascript message without loosing the initial values.I have tried using sessions and it doesnt work.
achuthan1988
Member
53 Points
159 Posts
Handling Unique key Exceptions.
May 07, 2012 11:39 AM|LINK
I have a web application which has a registration page and it asks for an user id. I have set the userid column as unique in sql server. So If a user enters a user id which is already present in the database then an error message should be thrown (an exception) .Right now the the control is thrown back to visual studio, which is bcoz the exception is not handled
Can someone tell me how to handle the exception: the code for the registration page is below:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.SqlClient; public partial class register : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string str = ""; for (int i = 1900; i < 2012; i++) { str = Convert.ToString(i); year_ddl.Items.Add(str); } string str1 = ""; for (int i = 1; i < 13; i++) { str1 = Convert.ToString(i); month_ddl.Items.Add(str1); } string str2 = ""; for (int i = 1; i < 32; i++) { str2 = Convert.ToString(i); day_ddl.Items.Add(str2); } } protected void register_btn_Click(object sender, EventArgs e) { SqlConnection conn = new SqlConnection(); conn.ConnectionString = "Data Source=MUKUNDANPC\\SQLEXPRESS;Initial Catalog=lms_db;Integrated Security=True;"; SqlCommand cmd = new SqlCommand(); string year_x = Convert.ToString(year_ddl.ToString()); string month_x = Convert.ToString(month_ddl.ToString()); string day_x = Convert.ToString(day_ddl.ToString()); string dob1 = year_x + "/" + month_x + "/" + day_x; cmd.CommandText = "INSERT into users (fname,lname,gender,dob,userid,pwd,phone_no,address,email_id) VALUES ('" + fname_tb.Text + "','" + lname_tb.Text + "','" + gender_rbl.SelectedValue.ToString() + "','" + dob1 + "','" + userid_tb.Text + "','" + pwd_tb.Text + "','" + phno_tb.Text + "','" + address_tb.Text + "','" + mail_tb.Text + "')"; cmd.Connection = conn; conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); } }achuthan1988
Member
53 Points
159 Posts
Re: Handling Unique key Exceptions.
May 07, 2012 12:02 PM|LINK
i tried to use a try catch but am stuck
try{ SqlConnection conn = new SqlConnection(); conn.ConnectionString = "Data Source=MUKUNDANPC\\SQLEXPRESS;Initial Catalog=lms_db;Integrated Security=True;"; SqlCommand cmd = new SqlCommand(); string year_x = Convert.ToString(year_ddl.ToString()); string month_x = Convert.ToString(month_ddl.ToString()); string day_x = Convert.ToString(day_ddl.ToString()); string dob1 = year_x + "/" + month_x + "/" + day_x; cmd.CommandText = "INSERT into users (fname,lname,gender,dob,userid,pwd,phone_no,address,email_id) VALUES ('" + fname_tb.Text + "','" + lname_tb.Text + "','" + gender_rbl.SelectedValue.ToString() + "','" + dob1 + "','" + userid_tb.Text + "','" + pwd_tb.Text + "','" + phno_tb.Text + "','" + address_tb.Text + "','" + mail_tb.Text + "')"; cmd.Connection = conn; conn.Open(); cmd.ExecuteNonQuery(); } catch (Sqlexception ex) { <Message showing that the user id is unique>) } conn.Close(); <javascript code which says that registration was successfull and redirects to another page>So after the message for exception is shown i want the user to be on the registration page so that he can enter a unique user id . How do i make sure that the user stays on the page and the next two lines are executed when the user id is unique?vikvish1
Member
748 Points
337 Posts
Re: Handling Unique key Exceptions.
May 07, 2012 12:45 PM|LINK
check and read below link
http://howtouseasp.net/how-to-check-case-sensitive-characters-while-login-in-asp-net-c/#more-57
with regards
vik
vikvish
Howtouseasp.net
Useofaspdotnet/
achuthan1988
Member
53 Points
159 Posts
Re: Handling Unique key Exceptions.
May 07, 2012 01:03 PM|LINK
NOW That is not my question. I have handled an SqlException for Unique Key constraint and the message is displayed using a line of javascript. But i want the registration page to reload and retain the same values , using sessions didnt really help. the javascript code redirected to the same page, but didnt retain the values...why?
Ruchira
All-Star
42904 Points
7022 Posts
MVP
Re: Handling Unique key Exceptions.
May 07, 2012 04:15 PM|LINK
Hello,
Try by modifying your code as below
try{ SqlConnection conn = new SqlConnection(); conn.ConnectionString = "Data Source=MUKUNDANPC\\SQLEXPRESS;Initial Catalog=lms_db;Integrated Security=True;"; SqlCommand cmd = new SqlCommand(); string year_x = Convert.ToString(year_ddl.ToString()); string month_x = Convert.ToString(month_ddl.ToString()); string day_x = Convert.ToString(day_ddl.ToString()); string dob1 = year_x + "/" + month_x + "/" + day_x; Session["UserID"] = userid_tb.Text; cmd.CommandText = "INSERT into users (fname,lname,gender,dob,userid,pwd,phone_no,address,email_id) VALUES ('" + fname_tb.Text + "','" + lname_tb.Text + "','" + gender_rbl.SelectedValue.ToString() + "','" + dob1 + "','" + userid_tb.Text + "','" + pwd_tb.Text + "','" + phno_tb.Text + "','" + address_tb.Text + "','" + mail_tb.Text + "')"; cmd.Connection = conn; conn.Open(); cmd.ExecuteNonQuery(); } catch (Sqlexception ex) { Response.Write(Session["UserID"].ToString()); } conn.Close();
My Tech blog | My YouTube ChannelPlease 'Mark as Answer' if this post helps you.ZeeshanAnsar...
Participant
878 Points
264 Posts
Re: Handling Unique key Exceptions.
May 07, 2012 04:27 PM|LINK
always use sp to communicate with database
and just have three mode in sp as A-Add,M-Modify, D-Delete.
when db Mode is A or M then just check like this
I hope you understand!
Please 'Mark as Answer' if this post helps you.
achuthan1988
Member
53 Points
159 Posts
Re: Handling Unique key Exceptions.
May 08, 2012 01:14 AM|LINK
i have given you the wrong idea this is the code:
try{ SqlConnection conn = new SqlConnection(); conn.ConnectionString = "Data Source=MUKUNDANPC\\SQLEXPRESS;Initial Catalog=lms_db;Integrated Security=True;"; SqlCommand cmd = new SqlCommand(); string year_x = Convert.ToString(year_ddl.ToString()); string month_x = Convert.ToString(month_ddl.ToString()); string day_x = Convert.ToString(day_ddl.ToString()); string dob1 = year_x + "/" + month_x + "/" + day_x; Session["firstname"]=fname_tb.Text; cmd.CommandText = "INSERT into users (fname,lname,gender,dob,userid,pwd,phone_no,address,email_id) VALUES ('" + fname_tb.Text + "','" + lname_tb.Text + "','" + gender_rbl.SelectedValue.ToString() + "','" + dob1 + "','" + userid_tb.Text + "','" + pwd_tb.Text + "','" + phno_tb.Text + "','" + address_tb.Text + "','" + mail_tb.Text + "')"; cmd.Connection = conn; conn.Open(); cmd.ExecuteNonQuery(); } catch (Sqlexception ex) { this.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('User ID has to be Unique'); window.location.href = 'registration.aspx';", true); <here some how i have to retrieve the value from the session and place it into the same textbox> } conn.Close();the problem is when i store the values in a session, then the unique key exception occours and i try to retrieve the values from the session into the same page , i get a blank page . Does it have something to do with the fact that the javascript line redirects the page to the same page?
achuthan1988
Member
53 Points
159 Posts
Re: Handling Unique key Exceptions.
May 08, 2012 04:56 AM|LINK
Any solutions?
friendster
Member
749 Points
189 Posts
Re: Handling Unique key Exceptions.
May 08, 2012 06:43 AM|LINK
Hi,
I suggest you to check for user id exists before you call your user insert method.
if user id exists. you show your custom error or do the next step. so you can suggest user also some unique values.
Regards
Raj
achuthan1988
Member
53 Points
159 Posts
Re: Handling Unique key Exceptions.
May 08, 2012 07:39 AM|LINK