Hi, I have made registration page, everything works fine and inserting in to database. But what it doesn't work is check for the existing username exist or not in the database and show the message. Kindly please help me in this where I am doing wrong Thanks.
Here's my code:
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;
using System.Configuration;
public partial class registration : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString);
conn.Open();
string checkuser = "SELECT UserName FROM UserData where UserName= '" + TextBoxUN + "'";
SqlCommand com = new SqlCommand(checkuser, conn);
com .Parameters.Add(new SqlParameter("@UserName", TextBoxUN.Text));
int temp = Convert.ToInt32(com.ExecuteScalar().ToString());
if (temp == 0)
{
Session["UserName"] = TextBoxUN.Text;
Response.Write("Users already Exist");
}
conn.Close();
}
}
protected void DropDownListCountry_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void Button1_Click1(object sender, EventArgs e)
{
try
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString);
conn.Open();
string insertQuery = "insert into UserData (Username,Email,Password,Phone,Country,Company,Address) values (@Uname ,@email ,@password ,@phone ,@country ,@company ,@address)";
SqlCommand com = new SqlCommand(insertQuery, conn);
com.Parameters.Add(new SqlParameter("@Uname", TextBoxUN.Text));
com.Parameters.Add(new SqlParameter("@email", TextBoxEmail.Text));
com.Parameters.Add(new SqlParameter("@password", TextBoxPass.Text));
com.Parameters.Add(new SqlParameter("@phone", TextBoxphone.Text));
com.Parameters.Add(new SqlParameter("@country", DropDownListCountry.SelectedItem.ToString()));
com.Parameters.Add(new SqlParameter("@company", DropDownListCompany.SelectedItem.ToString()));
com.Parameters.Add(new SqlParameter("@address", TextBoxAddress.Text));
com.ExecuteNonQuery();
Response.Redirect("Manager.aspx");
conn.Close();
}
catch (Exception ex)
{
Response.Write("Error:" + ex.ToString());
}
}
}