C# database

May 3 2010 2:27 PM

hi friends, i am doing windows application program with c#, for check if the data already exist in the database, below is my code.

 

 cn.Open();

            string check = "select Email from Login2 where Email= '" + emailtxt.Text + "'";
            string input = "Insert into Login2 Values ( '" + Fristtxt.Text + "', '" + lasttxt.Text + "' ,'" + gender + "', '" +          addtext.Text + "', '" + emailtxt.Text + "', '" + usertxt.Text + "', '" + psstxt.Text + "')";
            SqlCommand cd = new SqlCommand(check, cn);
            SqlCommand com = new SqlCommand(input, cn);
            cd.CommandText = check;
            SqlDataReader dr;
            dr = cd.ExecuteReader();

 if (dr.HasRows == true)

{

  MessageBox.Show("Email Already Assigned");

dr.close();

}

  else if (dr.HasRows == false)

  {

               MessageBox.Show("Email availalble");
           
                com.CommandText = input;
             
                com.ExecuteNonQuery();

}

   cn.Close();

 

 

my problem is, the above program check the email exist or not in the database, but if the email not exist in the database, its not add the full data in the database, can anyone help me on the code. thanks friends.

 


Answers (1)