15
Answers

check the user already exists or not

hii..

I am tried to execute this code...am getting only if parts it's not going to the else part.. I don't know what mistake I have
  

 
code:
 

//***************************************

//Insert the values into database

//***************************************

string strcon = "Data Source=.;Initial Catalog=userinformation; Integrated Security=True";//used to data connection

SqlConnection mycon = new SqlConnection(strcon);//used for new connection

mycon.Open();//open the db

//insert the values

string strquery = "insert into userdetails(firstname,lastname,gender,phoneno)VALUES('" + firstname + "','" + lastname + "','" + ChosenMovie + "','" + phone + "')";

//select the values from

SqlCommand cmdd = new SqlCommand("select * from userdetails ",mycon);

//read the values from db

SqlDataReader reader = cmdd.ExecuteReader();

if (reader.HasRows)

{

//already exists means give the error

MessageBox.Show("User already exists");

}

else {

//if it's new row means insert the values

MessageBox.Show(strquery);

SqlCommand cmd = new SqlCommand(strquery, mycon);

cmd.ExecuteNonQuery();//save the values

mycon.Close();//close the connection

MessageBox.Show("the value has been inserted");//give the success message

}

 
Answers (15)