Click here to Skip to main content
15,911,315 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
try
            {
                SqlConnection con = new SqlConnection(@"Data Source=LENOVO-41B3F2CE\SQLEXPRESS;Initial Catalog=IMS;Integrated Security=True;Pooling=False");
                string query = "SELECT [user].* FROM [user] where u_name='" + txtusername.Text + "' and pwd='" + txtpassword.Text + "'";
                SqlCommand com = new SqlCommand(query, con);
                con.Open();
                if (com.ExecuteNonQuery() == -1)
                {
                    Response.Redirect("WebForm1.aspx");
                    Label3.Text = "successfully";
                    
                }
                else
                {
                    Label3.Text = "not successfully";
                }
                con.Close();
            }
            catch (Exception ex)
            {
                Console.Write("" + ex.Message);
            }
Posted
Updated 17-Apr-13 19:03pm
v2
Comments
Karthik Harve 18-Apr-13 1:14am    
Do you mean, it is always redirecting to "WebForm1.aspx" page whether the password is right or wrong ?
fak_farrukh 18-Apr-13 1:14am    
yessssss

Hi,

ExecuteNoQuery Behaviour

"
Quote:
For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. When a trigger exists on a table being inserted or updated, the return value includes the number of rows affected by both the insert or update operation and the number of rows affected by the trigger or triggers. For all other types of statements, the return value is -1. If a rollback occurs, the return value is also -1.


So you ca Use ExecuteReader

if (com.ExecuteReader().HasRows)
               {
                   Response.Redirect("WebForm1.aspx");
                   Label3.Text = "successfully";

               }
               else
               {
                   Label3.Text = "not successfully";
               }


try this .

Thanks

Siva Rm K
 
Share this answer
 
Comments
fak_farrukh 18-Apr-13 1:30am    
thanxxx its done
but what is purpose of using executereader.hasrows
rmksiva 18-Apr-13 1:49am    
Welcome, Execute Reader, Reads Db Table Rows to SqlDataReader Object or Other DataReader Objects
Implement as follows:

C#
try
            {
                SqlConnection con = new SqlConnection(@"Data Source=LENOVO-41B3F2CE\SQLEXPRESS;Initial Catalog=IMS;Integrated Security=True;Pooling=False");
                string query = "SELECT [user].* FROM [user] where u_name='" + txtusername.Text + "' and pwd='" + txtpassword.Text + "'";
                SqlCommand com = new SqlCommand(query, con);
                SqlDataReader DR=com.ExecuteReader();
                 con.Open();
                 If(DR.Read())
                 {
                    Response.Redirect("WebForm1.aspx");
                    Label3.Text = "successfully";
                 }
                else
                {
                    Label3.Text = "not successfully";
                }
                con.Close();
            }
            catch (Exception ex)
            {
                Console.Write("" + ex.Message);
            }
 
Share this answer
 
C#
SqlConnection con = new SqlConnection(@"Data Source=RST1016;Initial Catalog=MDMdb;Integrated Security=True;Pooling=False");
            SqlDataReader dr;
            bool userExists = false;

            try
            {
                string query = "SELECT [user].* FROM [user] where u_name='" + txtusername.Text + "' and pwd='" + txtpassword.Text + "'";
                SqlCommand com = new SqlCommand(query, con);
                con.Open();

                dr = com.ExecuteReader();
                while (dr.Read())
                {
                    userExists = true;
                    break;
                }

                if (userExists)
                    Response.Redirect("WebForm1.aspx");
                else
                    Response.Write("not successfully");

            }
            catch (Exception ex)
            {
                Response.Write("" + ex.Message);
            }
            finally
            {
                if (con.State == ConnectionState.Open)
                    con.Close();
            }


try this out. this may help you. ExecuteNonQuery genrally return -1 hence all the time that condition is true and going to next page.
 
Share this answer
 
hi pal ,
This is another way u can try it ....

SqlConnection con = new SqlConnection(@"Data Source=RST1016;Initial Catalog=MDMdb;Integrated Security=True;Pooling=False");
SqlDataReader dr;
bool userExists = false;

try
{
string query = "SELECT [user].* FROM [user] where u_name='" + txtusername.Text + "' and pwd='" + txtpassword.Text + "'";
SqlCommand com = new SqlCommand(query, con);
con.Open();

dr = com.ExecuteReader();
if(dr.RecordsAffected==1)
{
Response.Redirect("WebForm1.aspx");
}
else
{
Response.Write("not successfully");
}

}
catch (Exception ex)
{
Response.Write("" + ex.Message);
}
finally
{
if (con.State == ConnectionState.Open)
con.Close();
}
 
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