2
Answers

username exist or not if exists choose differet name

Yashu Shashi

Yashu Shashi

7y
187
1
 
How to check username exists or not in sqlserver databse while registering the user if alredy exists show message alredy exists choose different name without using stored procedure .
 
asps.cs 
 
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace rummyRLodemo
{
public partial class registerex : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
SqlConnection con = new SqlConnection("Data Source=SHASHI\\SQLEXPRESS;Initial Catalog=Rummy;Integrated Security=True");
protected void btnRegister_Click(object sender, EventArgs e)
{
int userId = 0;
SqlCommand cmd = new SqlCommand();
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Parameters.AddWithValue("@Username", UserName.Text.Trim());
cmd.Parameters.AddWithValue("@Password", password.Text.Trim());
cmd.Parameters.AddWithValue("@Email", email.Text.Trim());
cmd.Connection = con;
con.Open();
userId = Convert.ToInt32(cmd.ExecuteScalar());
con.Close();
}
string message = string.Empty;
switch (userId)
{
case -1:
message = "Username already exists.\\nPlease choose a different username.";
break;
case -2:
message = "Supplied email address has already been used.";
break;
default:
message = "Registration successful.\\nUser Id: " + userId.ToString();
break;
}
ClientScript.RegisterStartupScript(GetType(), "alert", "alert('" + message + "');", true);
}
}
}
 
Answers (2)