Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
protected void Page_Load(object sender, EventArgs e)
   {
       if (IsPostBack)
       {
           SqlConnection conn = new SqlConnection("Data Source=DESKTOP-06QKCFT\\SQLEXPRESS;Initial Catalog=CMS1;Integrated Security=True");
           conn.Open();
           bool exists = false;
           string chechuser = "SELECT count(*) FROM [user] where username='" + t_username.Text + "'";
           SqlCommand cmd = new SqlCommand(chechuser, conn);
           cmd.Parameters.AddWithValue("UserName", t_username.Text);
           exists = (int)cmd.ExecuteScalar() > 0;
           if (exists)
           {
               Response.Write("User Already Exists");
           }
           conn.Close();
       }
   }


What I have tried:

I have tried it in this way but not working the user still enter to DB:

<pre> if (IsPostBack)// to check if the user is already exsist
       {
           SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["regstration_ConnectionString"].ConnectionString);
           conn.Open();
           string checkuser = "selecte count(*) from Table where username='" + t_username.Text + "'";
           SqlCommand com = new SqlCommand(checkuser, conn);
           int temp = Convert.ToInt32(com.ExecuteScalar().ToString());
           if(temp == 1)
           {

               Response.Write("User Already Exists");
           }

            conn.Close();


        }
Posted
Updated 28-Feb-20 12:18pm
Comments
Tomas Takac 12-Apr-17 3:23am    
Can you explain what the problem is here?
MuhammadNaamh 12-Apr-17 3:32am    
this code does not work I have tried 2 way but nothing :
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
SqlConnection conn = new SqlConnection("Data Source=DESKTOP-06QKCFT\\SQLEXPRESS;Initial Catalog=CMS1;Integrated Security=True");
conn.Open();
bool exists = false;
string chechuser = "SELECT count(*) FROM [user] where username='" + t_username.Text + "'";
SqlCommand cmd = new SqlCommand(chechuser, conn);
cmd.Parameters.AddWithValue("UserName", t_username.Text);
exists = (int)cmd.ExecuteScalar() > 0;
if (exists)
{
Response.Write("User Already Exists");
}
conn.Close();
}
}
Richard MacCutchan 12-Apr-17 3:27am    
You have spelt "selecte" wrong in your SQL. Also, why are you converting an integer to a String just to convert it back to an integer?
MuhammadNaamh 12-Apr-17 3:39am    
no my friend in the second query it is correct but not working :
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
SqlConnection conn = new SqlConnection("Data Source=DESKTOP-06QKCFT\\SQLEXPRESS;Initial Catalog=CMS1;Integrated Security=True");
conn.Open();
bool exists = false;
string chechuser = "SELECT count(*) FROM [user] where username='" + t_username.Text + "'";
SqlCommand cmd = new SqlCommand(chechuser, conn);
cmd.Parameters.AddWithValue("UserName", t_username.Text);
exists = (int)cmd.ExecuteScalar() > 0;
if (exists)
{
Response.Write("User Already Exists");
}
conn.Close();
}
}
Richard MacCutchan 12-Apr-17 3:46am    
What does "not working" mean? Have you done any actual debugging to find out exactly what happens when you run this query?

Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

SELECT does not have an "E" at teh end:
C#
string checkuser = "selecte count(*) from ...


Your two SELECT statements are using different tables:
SQL
SELECT count(*) FROM [user] where username=
And
SQL
selecte count(*) from Table where username=
Work out wher eit is, and use the right table - I suspect teh second one is faulty as TABLE is an SQL reserved word.
Quote:
not working the user still enter to DB:

Well...not in that code.
That code just checks if a user exists, and prints a message if it does - it does nothing to prevent later code from inserting a user.

That code looks like you are guessing and hoping for the best instead of sitting down and thinking about what you are trying to do: a parameter you don't use, hard coded connection strings, no INSERT code, irrelevant conversions, all sorts of stuff.
Stop, think, and design. Then throw that lot away, and code from scratch. It'll save you a lot of time in the long run, honest.
 
Share this answer
 
Comments
MuhammadNaamh 12-Apr-17 4:11am    
have you read this code carefully? the event is page_Load inside it I am checking if page post back that mean there is a code running that code is to insert into data base and it's work the problem in this code why the message not shwing that is my question .
thanks for your advice i will work with

protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
SqlConnection conn = new SqlConnection("Data Source=DESKTOP-06QKCFT\\SQLEXPRESS;Initial Catalog=CMS1;Integrated Security=True");
conn.Open();
bool exists = false;
string chechuser = "SELECT count(*) FROM [user] where username='" + t_username.Text + "'";
SqlCommand cmd = new SqlCommand(chechuser, conn);
cmd.Parameters.AddWithValue("UserName", t_username.Text);
exists = (int)cmd.ExecuteScalar() > 0;
if (exists)
{
Response.Write("User Already Exists");
}
conn.Close();
}
}
OriginalGriff 12-Apr-17 4:21am    
Yes, I have.
Yes I can tell - the method name is a big clue...
No, you don't do any insert in that code...
Yes, you are wide open to anyone who feels like deleting your DB instead of signing up.
MuhammadNaamh 12-Apr-17 4:30am    
I don't mean to offend :) thanks for your advice i correct the code (delete the 'string')
thanks for you :)
private bool isValid()
{
bool exists = false;
SqlCommand cmd = new SqlCommand("Select count(*) from users where username=@username", con);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@username", txt_username.Text);
exists = (int)cmd.ExecuteScalar() > 0;
if (exists)
{
MessageBox.Show("Useralready exists");
return false;

}
return true;

}


tested and working perfectly all right!!!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900