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
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(); } }