I want to enter details of a client into a database.If the details already exists the application must inform the user and they don't then save the details

here is my code

private void btnCreate_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString="Data Source=MyComputer\\SQLEXPRESS;Initial Catalog=TIP;Integrated Security=True;Pooling=False";
            conn.Open();
            string check = "SELECT COUNT(UserID)FROM Login WHERE UserID='"+txtID.Text+"'";
            string input="INSERT INTO Login(UserID,UserName,Password,Station)VALUES('"+txtClientID.Text+"','"+txtClientName.Text+"','"+txtPass.Text+"','"+ClientRank.Text+"')";
            SqlCommand cd = new SqlCommand(check, conn);
            SqlCommand com = new SqlCommand(input, conn);
            cd.CommandText = check;
            SqlDataReader dr;
            dr = cd.ExecuteReader();
            if (dr.HasRows==false)
            {
                MessageBox.Show("UserID Already Assigned");
                dr.Close();
                Chidzima();
            }
            else if (dr.HasRows==true)
            {
                com.CommandText = input;
                com.ExecuteNonQuery();
                MessageBox.Show("Details Saved", "Valid Data", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Chidzima();
            }
            else
            {
            }
            
            
            
            conn.Close();
        }
        public void Chidzima()
        {
            txtID.Clear();
            txtPass.Clear();
            txtUser.Clear();
            cmbSta.Text = "";
        }
    }

I am new to c# help me please!!

Call the Read() method.

...
   if (dr.Read())
            {
                MessageBox.Show("UserID Already Assigned");
                dr.Close();
                Chidzima();
            }
 ....

Firstly, Please use [code]

[/code] blocks when posting code. This ensures teh correct formatting to make ti more readable.

Secondly, you are inserting the values if dr.HasRows == true. This should be the other way around. If dr.HasRows is true then it means the user was found in the database and returned by your check query.
If HasRows is false, then no user was found and you should insert it.

Thanx but its executing only the if statement which outputs "USERID ALREADY ASSIGNED" Message. Is there anything wrong with my sql statements??

Change sql text,

string check = "SELECT  UserID FROM Login WHERE UserID='"+txtID.Text+"'";

Thanx

my bad, overlooked that the check returned count, not rows the actual rows found : /

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.