Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
protected void btnRegister_Click(object sender, EventArgs e)
        {
            string connetionString = "Data Source=qweq;Initial Catalog=Test_DB;User ID=asd;Password=asdsf";
            SqlConnection conn = new
            SqlConnection(connetionString);
            string commText = "insert into login(User_Name,Password,Address,City,State,PIN,Phone_no,Email_id,Created_By,Created_on) values('" + txtUserName.Text + "','" + txtPassword.Text + "','" + txtAddress.Text + "','" + txtCity.Text + "','" + txtState.Text + "','" + Convert.ToDouble(txtPin.Text) + "','" + Convert.ToDouble(txtPhoneNo.Text) + "','" + txtEmailId.Text +  "','" + "Alok" + "','" + DateTime.Now + "')";
            SqlCommand cmd = new 
            SqlCommand(commText, conn);
            conn.Open();
            int no = cmd.ExecuteNonQuery();
            conn.Close();
            if(no>0)
            {
                Label1.Text= "User register register sucessfully";
            }
            else
            {
                Label1.Text = "Error";
            }
            txtUserName.Text = String.Empty;
            txtPassword.Text = String.Empty;
            txtAddress.Text = String.Empty;
            txtCity.Text = String.Empty;
            txtState.Text = String.Empty;
            txtPin.Text = String.Empty;
            txtPhoneNo.Text = String.Empty;
            txtEmailId.Text = String.Empty;

        }



Edited: Code block Added
Posted
Updated 12-Mar-15 18:45pm
v3
Comments
_Maxxx_ 13-Mar-15 0:36am    
Firstly - this is a very poor way of writing SQL & is open to SQL Injection attacks - e.g. someone could type text into one of the text boxes that contains SQL that would be executed
.net bigner 13-Mar-15 1:34am    
i m a bigner,thats why i m doing like that.

Use EXISTS
SQL
if not exists (select *from login where username=@username) 
begin
--Insert Query
end
else 
begin
--Update Query or something..
end

Refer it
http://www.java2s.com/Tutorial/SQLServer/0400__Transact-SQL/IFEXISTS.htm[^]
 
Share this answer
 
Might be this link will help you do it.

http://stackoverflow.com/questions/18114458/fastest-way-to-determine-if-record-exists[^]

NOTE: Appending string is not a good practice as it exposes to Sql injection. Either convert this to stored procedure or paramaterized query see link http://rosettacode.org/wiki/Parametrized_SQL_statement[^]
 
Share this answer
 
Just write some more sql like:

select count(*) from login where username = username

If the count is 0 then you're good to go.
 
Share this answer
 
Comments
.net bigner 13-Mar-15 1:36am    
Thanx..but i h ave one doubt that where shall i use count ?
can u tell by editing my code please?

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