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
//***************************************
//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
}
Wim SturkenboomPosted Nov 21, 2014, 12:16 PM
[quote]
SqlCommand cmd=new SqlCommand
("Select * From userdetails Where firstname='"+txtfirstName.Text+"'");
[/quote]Never ever do that; that approach is prone to SQL injection.
Joginder BangerPosted Nov 21, 2014, 11:06 AM
you can do with the help of store procedure. you can call easy and pass the parameters.
for example
create proc chkuser
-- pass parameters here
as
begin
if exists(select firstname from tablename where columnname=parameters)
begin
--do stuff if you user exists
end
else
begin
-- do stuff here if user not exists
end
end
i think this is ease stuff .
priya santhiPosted Nov 21, 2014, 9:32 AM
priya santhiPosted Nov 21, 2014, 9:31 AM
Yadagiri ReddyPosted Nov 21, 2014, 9:24 AM
Vikram AgrawalPosted Nov 21, 2014, 9:09 AM
well
ExecuteScalar() mehod is used when we are getting only single value and it's return type is object.
and here we are checking for that firstname is already present or not.
priya santhiPosted Nov 21, 2014, 9:06 AM
Vikram AgrawalPosted Nov 21, 2014, 9:00 AM
Hi Priya,
Every problem has many solution, here is one of them.
You can solve your problem in following manner
SqlCommand cmd=new SqlCommand
("Select * From userdetails Where firstname='"+txtfirstName.Text+"'");
SqlDataReader reader=cmd.ExecuteReader();
if(reader.HasRows)
{
// user already exist;
}
else
{
// Code for inserting the values;
}
Be careful while writing Select Command which I have written in bold letters.
you can just copy and paste it from here.
Thanks
please Mark This answer is correct if it is Helpful to you
Yadagiri ReddyPosted Nov 21, 2014, 8:46 AM
Yadagiri ReddyPosted Nov 21, 2014, 8:39 AM
priya santhiPosted Nov 21, 2014, 8:33 AM
Wim SturkenboomPosted Nov 21, 2014, 8:29 AM
SqlConnection mycon = new SqlConnection(strcon);
mycon.Open();
string strquery = "insert into userdetails(firstname,lastname,gender,phoneno)VALUES('" + firstname + "','" + lastname + "','" + ChosenMovie + "','" + phone + "')";
SqlCommand cmdd = new SqlCommand("Select *from userdetails where firstname=@firstname",mycon);
cmdd.Parameters.AddWithValue("@firstname", VariableWithFirstname);
if(cmdd.ExecuteScalar()>0)
Yadagiri ReddyPosted Nov 21, 2014, 8:19 AM
SqlConnection mycon = new SqlConnection(strcon);
mycon.Open();
string strquery = "insert into userdetails(firstname,lastname,gender,phoneno)VALUES('" + firstname + "','" + lastname + "','" + ChosenMovie + "','" + phone + "')";
SqlCommand cmdd = new SqlCommand("Select *from userdetails where firstname=@firstname",mycon);
if(cmdd.ExecuteScalar()>0)
{
SqlCommand cmd = new SqlCommand(strquery, mycon);
cmd.ExecuteNonQuery();
MessageBox.Show("the value has been inserted");
}
else
{
MessageBox.Show("User already exists");
}
mycon.Close();
------------------------------------------------------------------------------------------------
Yadagiri Reddy(Please accept it as an answer, if it helped you)
priya santhiPosted Nov 21, 2014, 7:57 AM
Yadagiri ReddyPosted Nov 21, 2014, 7:55 AM
based upon what values you want to check whether the user exists or not