Click here to Skip to main content
15,879,474 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 2:29am
v2
Comments
Dnyaneshwar Kondbale 17-Apr-13 8:37am    
Instead of writing com.ExecuteNonQuery() use data reader or data set.for fetching the record

ExecuteNonQuery will always return -1 in your case.

From MSDN[^]:

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.

You should use ExecuteScalar instead and have it return the row count. ExecuteNonQuery is normally used for queries where you don't care much about the returnvalue (except maybe the number of affected rows as per above text.
 
Share this answer
 
v4
Comments
fak_farrukh 17-Apr-13 8:35am    
its done
thanxx
Johnny J. 17-Apr-13 8:37am    
You're welcome
AmitGajjar 17-Apr-13 8:37am    
Perfect 5+
Hi,

You are using ExecuteNonQuery incorrectly, please have a look at http://blogs.msdn.com/b/spike/archive/2009/01/27/sqlcommand-executenonquery-returns-1-when-doing-insert-update-delete.aspx[^]

If you are counting user that match your criteria then you have to return count from the query and use ExecuteScaler to get your result count.

hope this information helps you.

best luck
 
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