Click here to Skip to main content
16,016,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have created 2 pages login and password.and i am checking for duplicate entry of username.
i have written query for it.But how can prevent it from inserting if the username is same.i have tried further but it is wrong.pls help
here is my code-

C#
 protected void signupbtn_Click(object sender, EventArgs e)
    {
        try
        {
            conn.Open();
        string st;
            st = "select count(username)from login where username='" + txtname.Text + "'";
            cmd = new SqlCommand(st, conn);
            if (st > 0)
            {
                Response.Write("duplicate record");
            }
         else
{
 cmd = new SqlCommand("insert into login(name,midname,surname,username,password,contact,dob,email,address,occupation,ltype) values('" + txtfirst.Text + "','" + txtmid.Text + "','" + txtsur.Text + "','" + txtname.Text + "','" + txtpass.Text + "','" + txtcontact.Text + "','" + txtdob.Text + "','" + txtemail.Text + "','" + txtaddr.Text + "','" + txtocc.Text + "','" + typeButtonList1.SelectedValue + "')", conn);


                cmd.ExecuteNonQuery();
                Response.Write("Record inserted");
            }
        }
Posted
Updated 17-Jan-14 3:14am
v2

Be aware of SQL_injection[^] as it is not safe to frame the query in c#..


try like this..


C#
conn.Open();
           string st;
           st = "select count(username)from login where username='" + txtname.Text + "'";
           var cmd = new SqlCommand(st, conn);
           DataTable dt = new DataTable();
           SqlDataAdapter da = new SqlDataAdapter(cmd);
           da.Fill(dt);
           int count = Convert.ToInt32(dt.Rows[0][0]);
           if (count >= 1)
           {
               Response.Write("duplicate record");
           }
 
Share this answer
 
v4
Comments
Member 10523130 17-Jan-14 9:27am    
It is not working.. I think my if statement is wrong..??
Karthik_Mahalingam 17-Jan-14 9:30am    
did u check this code ??
your if statement is compile error, invalid statements...
Member 10523130 17-Jan-14 9:34am    
yes it is giving error..but how can check if that count is not 0??
Karthik_Mahalingam 17-Jan-14 9:35am    
what error ?

( count != 0 )
Member 10523130 17-Jan-14 9:41am    
ok..I have not included using system.data(). it is not giving error but it is not working also
First of all, have you ever heard about SQL injection[^]? An ASP.NET application should be created in that way which provide protection of sql injection attacks[^].

I strongly recommend to use stored procedures and call it from code behind[^]. If you would like to avoid logins duplication, set this field as unique[^]!
How to: Creating and Modifying UNIQUE Constraints[^]
 
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