Click here to Skip to main content
15,904,156 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
execute non query is not working,two values should be added in the database emailid ,lasttime display in database

What I have tried:

protected void btnlogin_Click(object sender, EventArgs e)
 {
     erroruserpage.Text = "";

     cn.ConnectionString = ConfigurationManager.ConnectionStrings["bs"].ToString();
     cn.Open();

     SqlCommand cmtest = new SqlCommand("insert into enquiryform (EmailAddr,last_login_time)values(@EmailAddr,@last_login_time)", cn);
     cmtest.Parameters.AddWithValue("@EmailAddr", txtusername.Text);
     cmtest.Parameters.AddWithValue("@last_login_time", DateTime.Today.ToString());
      cmtest.ExecuteNonQuery();
      cn.Close();

     SqlCommand cmtest1 = new SqlCommand("insert into tbl_admincreation_bs(tur_emailid,last_login_time)values(@emailid,@time)",cn);
     cmtest1.Parameters.AddWithValue("@emailid", txtusername.Text);
     cmtest1.Parameters.AddWithValue("@last_login_time", DateTime.Today.ToString());
     cmtest1.ExecuteNonQuery();
     cn.Close();


     SqlCommand cmmail = new SqlCommand("select EmailAddr from enquiryform where EmailAddr=@EmailAddr", cn);
     cmmail.Parameters.AddWithValue("@EmailAddr", txtusername.Text);
     SqlDataReader dr5 = cmmail.ExecuteReader();
Posted
Updated 22-Mar-17 5:34am
Comments
ZurdoDev 22-Mar-17 7:07am    
This is a very, very simple thing for you to debug. And we cannot run your code so you'll have to do it. Very simple.
F-ES Sitecore 22-Mar-17 7:09am    
If you phoned a mechanic and said your car "isn't working" do you think that would be enough information for them to help you?

Use the debugger to step through your code to inspect the content of your variables to make sure they are what you think they are, and use SQL Profiler to examine the SQL calls being made to make sure they are ok too.

SqlCommand cmtest1 = new SqlCommand("insert into tbl_admincreation_bs(tur_emailid,last_login_time)values(@emailid,@time)",cn);
       cmtest1.Parameters.AddWithValue("@emailid", txtusername.Text);
       cmtest1.Parameters.AddWithValue("@last_login_time @time", DateTime.Today.ToString()); // it should be @time
       cmtest1.ExecuteNonQuery();
       cn.Close();
 
Share this answer
 
v4
Before executing the first command (cmtest) you open the connection then after you close the connection.

Again for 2nd statement you use same connection object to execute the 2nd command (cmtest1).

So Remove the con.Close() method in between execute commands. At last close the connection.

protected void btnlogin_Click(object sender, EventArgs e)
{
erroruserpage.Text = "";

cn.ConnectionString = ConfigurationManager.ConnectionStrings["bs"].ToString();
cn.Open();

SqlCommand cmtest = new SqlCommand("insert into enquiryform (EmailAddr,last_login_time)values(@EmailAddr,@last_login_time)", cn);
cmtest.Parameters.AddWithValue("@EmailAddr", txtusername.Text);
cmtest.Parameters.AddWithValue("@last_login_time", DateTime.Today.ToString());
cmtest.ExecuteNonQuery();
//cn.Close();

SqlCommand cmtest1 = new SqlCommand("insert into tbl_admincreation_bs(tur_emailid,last_login_time)values(@emailid,@time)",cn);
cmtest1.Parameters.AddWithValue("@emailid", txtusername.Text);
cmtest1.Parameters.AddWithValue("@time", DateTime.Today.ToString());
cmtest1.ExecuteNonQuery();
//cn.Close();


SqlCommand cmmail = new SqlCommand("select EmailAddr from enquiryform where EmailAddr=@EmailAddr", cn);
cmmail.Parameters.AddWithValue("@EmailAddr", txtusername.Text);
SqlDataReader dr5 = cmmail.ExecuteReader();

Add after this statement
cn.Close();

Check this statement it may help you.
 
Share this answer
 

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