Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
i am newbie so dont angry on my question also i am weak in english
i have edited my query which was before an insert query and i have edited to become a uodate query but the last portion is not correct as data is updated successfull but i want to show a messagebox if updation was success then it will be suceess and if updation was failed then a message box will show which will contain message of error.
here is my crazy code


SqlConnection conn = new SqlConnection();
          conn.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\project\Projects\labelnameinsecondformtry\labelnameinsecondformtry\Database1.mdf;Integrated Security=True;User Instance=True";
          try
          {
              conn.Open();
          }
          catch (Exception)
          {
              MessageBox.Show("Error with the databse connection");
          }
          string qry2 = "UPDATE Table1 SET Password =@Password WHERE UserName=@username";
          SqlCommand com = new SqlCommand(qry2, conn);
          com.Parameters.AddWithValue("@username", this.textBox1.Text);
          com.Parameters.AddWithValue("@Password", this.textBox2.Text);
          SqlDataReader dr = com.ExecuteReader();
          while (dr.Read())
          {
              if (dr.HasRows == true)
              {
                  MessageBox.Show("updated sucesfull");
              }
          }
          if (dr.HasRows == false)
          {
              MessageBox.Show("updation failed ");
          }
Posted

Hi Adil,

Please update your code as below.

C#
SqlConnection conn = new SqlConnection();
            conn.ConnectionString = @"Data Source = .\SQLEXPRESS;AttachDbFilename=E:\project\Projects\labelnameinsecondformtry\labelnameinsecondformtry\Database1.mdf;Integrated Security=True;User Instance=True";
       try
       {
            conn.Open();
            string qry2 = "UPDATE Table1 SET Password =@Password WHERE UserName=@username";
            SqlCommand com = new SqlCommand(qry2, conn);
            com.Parameters.AddWithValue("@username", this.textBox1.Text);
            com.Parameters.AddWithValue("@Password", this.textBox2.Text);
            int result = com.ExecuteNonQuery();
            if (result > 0)
            {
                MessageBox.Show("updated sucesfull");
            }
            else
                 MessageBox.Show("updated failed");
         }
         catch (Exception)
         {
                MessageBox.Show("Error with the databse connection");
         }


Hope this will help you.
 
Share this answer
 
v2
Comments
shaikh-adil 15-Nov-12 7:01am    
thanks mukhthar bhai
:)
shaikh-adil 15-Nov-12 7:09am    
but what is this
if (result > 0)??
i didint get it can you explain
Mohd. Mukhtar 15-Nov-12 7:19am    
yes Adil,

Please note that SqlCommand.ExecuteNonQuery() method returns the number of row affected by the insert, delete or update command.
Hence here we have declared int result to store the no of affected rows. It will have the value >0 if command executed successfully other wise it will have negative value.
shaikh-adil 15-Nov-12 7:27am    
thank you mohammed bhai.
for giving knowledge.
thank you once again
sariqkhan 15-Nov-12 7:29am    
+5
Try the following:

C#
int x = cmd.ExecuteNonQuery();
if (x == -2)
{
   //fail and show the message
}


please check the following for example:

http://stackoverflow.com/questions/2963703/how-to-know-whether-a-sql-update-statement-executed-successfully-or-failed[^]
 
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