Hadeel e

Hadeel e

  • NA
  • 50
  • 6.6k

Update customer records

May 1 2020 3:00 PM
Hi,

In my app I have an update action to update the customers records in the back-end. Further, as I know it's supposed to save any updates or non (null) when one field wasn't changed. However, when I try to click save I'm getting a sql error, that the "MobileNumber" was not supplied.

Here is my data tier class:

  1. #region Update rider's info  
  2.         public bool updateRiders(RidersModel rider)  
  3.         {  
  4.   
  5.   
  6.             query = "UPDATE Users SET firstName = @firstName," +  
  7.                     " lastName = @lastName, email = @email, Address = @Address," +  
  8.                     " MobileNumber = @MobileNumber, city = @city," +  
  9.                     "state = @state, zipCode = @zipCode, UserName =@UserName, users_Id = @users_Id  " +  
  10.                     "WHERE usersId = @ID;";  
  11.   
  12.             conn = new SqlConnection(connectionString);  
  13.             cmd = new SqlCommand(query, conn);  
  14.   
  15.             cmd.Parameters.Add("@ID", SqlDbType.Int).Value = rider.usersId;  
  16.             cmd.Parameters.Add("@firstName", SqlDbType.VarChar, 255).Value = rider.firstName;  
  17.             cmd.Parameters.Add("@lastName", SqlDbType.VarChar, 255).Value = rider.lastName;  
  18.             cmd.Parameters.Add("@email", SqlDbType.VarChar, 255).Value = rider.email;  
  19.             cmd.Parameters.Add("@Address", SqlDbType.VarChar, 255).Value = rider.Address;  
  20.             cmd.Parameters.Add("@city", SqlDbType.VarChar, 255).Value = rider.city;  
  21.             cmd.Parameters.Add("@state", SqlDbType.VarChar, 255).Value = rider.state;  
  22.             cmd.Parameters.Add("@zipCode", SqlDbType.Int).Value = rider.zipCode;  
  23.             cmd.Parameters.Add("@MobileNumber", SqlDbType.VarChar, 200).Value = rider.mobileNumber;  
  24.             cmd.Parameters.Add("@UserName", SqlDbType.NVarChar, 256).Value = rider.UserName;  
  25.             cmd.Parameters.Add("@users_Id", SqlDbType.NVarChar, 128).Value = rider.userID;  
  26.   
  27.   
  28.             try  
  29.             {  
  30.                 conn.Open();  
  31.                 rows = cmd.ExecuteNonQuery();  
  32.                 if (rows > 0)  
  33.                 {  
  34.                     success = true;  
  35.                 }  
  36.   
  37.             }  
  38.             catch (SqlException ex)  
  39.             {  
  40.   
  41.                 throw new Exception(ex.Message);  
  42.   
  43.             }  
  44.             finally  
  45.             {  
  46.                 conn.Close();  
  47.             }  
  48.   
  49.             return success;  
  50.   
  51.         } 
 
 my Action is as the following :
  1. [HttpGet]  
  2.         public async Task<ActionResult> EditRiders(int id)  
  3.         {  
  4.             var userStore = new UserStore<IdentityUser>();  
  5.             var userManager = new UserManager<IdentityUser>(new UserStore<IdentityUser>(new RidesDbContext()));  
  6.   
  7.             IdentityUser theUser = new IdentityUser();  
  8.             theUser = await userManager.FindByIdAsync(User.Identity.GetUserId());  
  9.   
  10.             RidersModel rider = new RidersModel();  
  11.             RidersTier tier = new RidersTier();  
  12.             rider.email = theUser.UserName;  
  13.             rider.email = theUser.Email;  
  14.             rider.mobileNumber = theUser.PhoneNumber;  
  15.             rider.Password = theUser.PasswordHash;  
  16.             rider.userID = theUser.Id;  
  17.   
  18.   
  19.   
  20.             rider = tier.getRidersById(id);  
  21.   
  22.   
  23.   
  24.             return View(rider);  
  25.         }  
  26.   
  27.   
  28.         [HttpPost]  
  29.         public async Task<ActionResult> EditRiders(RidersModel rider)  
  30.         {  
  31.   
  32.             var userStore = new UserStore<IdentityUser>();  
  33.             var userManager = new UserManager<IdentityUser>(new UserStore<IdentityUser>(new RidesDbContext()));  
  34.   
  35.             IdentityUser theUser = new IdentityUser();  
  36.   
  37.   
  38.   
  39.             if (ModelState.IsValid)  
  40.             {  
  41.                 theUser = await userManager.FindByIdAsync(User.Identity.GetUserId());  
  42.   
  43.                 rider.email = theUser.UserName;  
  44.                 rider.email = theUser.Email;  
  45.                 rider.mobileNumber = theUser.PhoneNumber;  
  46.                 rider.Password = theUser.PasswordHash;  
  47.                 rider.userID = theUser.Id;  
  48.   
  49.                 RidersTier tier = new RidersTier();  
  50.                 tier.updateRiders(rider);  
  51.             }  
  52.   
  53.   
  54.   
  55.             TempData["ad"] = "User info has been updated";  
  56.   
  57.             return View(rider);  
  58.         } 
 

Is there any reason for that?

I tried to provide the parameter variables in the order that they appear in the query statement, but that didn't help.

I would appreciate your help. Thank you.


Answers (6)