locked
creating updateparameters by code RRS feed

  • Question

  • User1256512446 posted

    Hi all:

    I want to know if it is possible to create a parameter of a updateparameters of a objectdatasource by code, dynmically, and how to do that.

    TIA

    Sunday, August 16, 2015 9:02 AM

Answers

  • User281315223 posted

    You should be able to do this by simply using the Add() or AddWithValue() methods as seen below :

    YourDataSource.UpdateParameters.AddWithValue("@ParameterName","Example Value");

    This would add a parameter to your UpdateParameters collection.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, August 16, 2015 10:41 AM
  • User-271186128 posted

    Hi TIA,

    Agree with Rion, you could use the Add() or AddWithValue() methods to add parameters for ObjectDataSource control. You could refer to the following code:

    [DataObjectMethod(DataObjectMethodType.Update)]
    public static bool UpdateEmployee(int EmployeeID, string FirstName, string LastName, 
                                      string Address, string City, string Region, string PostalCode)
    {
      if (String.IsNullOrEmpty(FirstName))
        throw new ArgumentException("FirstName cannot be null or an empty string.");
      if (String.IsNullOrEmpty(LastName))
        throw new ArgumentException("LastName cannot be null or an empty string.");
    
      if (Address    == null) { Address    = String.Empty; }
      if (City       == null) { City       = String.Empty; }
      if (Region     == null) { Region     = String.Empty; }
      if (PostalCode == null) { PostalCode = String.Empty; }
    
      if (!_initialized) { Initialize(); }
    
      SqlConnection conn = new SqlConnection(_connectionString);
      SqlCommand    cmd  = new SqlCommand("UPDATE Employees " + 
                                          "  SET FirstName=@FirstName, LastName=@LastName, " + 
                                          "  Address=@Address, City=@City, Region=@Region, " +
                                          "  PostalCode=@PostalCode " +
                                          "  WHERE EmployeeID=@EmployeeID", conn);  
    
      cmd.Parameters.Add("@FirstName",  SqlDbType.VarChar, 10).Value = FirstName;
      cmd.Parameters.Add("@LastName",   SqlDbType.VarChar, 20).Value = LastName;
      cmd.Parameters.Add("@Address",    SqlDbType.VarChar, 60).Value = Address;
      cmd.Parameters.Add("@City",       SqlDbType.VarChar, 15).Value = City;
      cmd.Parameters.Add("@Region",     SqlDbType.VarChar, 15).Value = Region;
      cmd.Parameters.Add("@PostalCode", SqlDbType.VarChar, 10).Value = PostalCode;
      cmd.Parameters.Add("@EmployeeID", SqlDbType.Int).Value = EmployeeID;
    
      try
      {
        conn.Open();
    
        if (cmd.ExecuteNonQuery() == 0)
          return false;
      }
      catch (SqlException e)
      {
        // Handle exception.
      }
      finally
      {
        conn.Close();
      }
    
      return true;
    }
    

    More details, please refer to this link: https://msdn.microsoft.com/en-US/library/57hkzhy5.aspx

    Best Regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 17, 2015 3:00 AM

All replies

  • User281315223 posted

    You should be able to do this by simply using the Add() or AddWithValue() methods as seen below :

    YourDataSource.UpdateParameters.AddWithValue("@ParameterName","Example Value");

    This would add a parameter to your UpdateParameters collection.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, August 16, 2015 10:41 AM
  • User-271186128 posted

    Hi TIA,

    Agree with Rion, you could use the Add() or AddWithValue() methods to add parameters for ObjectDataSource control. You could refer to the following code:

    [DataObjectMethod(DataObjectMethodType.Update)]
    public static bool UpdateEmployee(int EmployeeID, string FirstName, string LastName, 
                                      string Address, string City, string Region, string PostalCode)
    {
      if (String.IsNullOrEmpty(FirstName))
        throw new ArgumentException("FirstName cannot be null or an empty string.");
      if (String.IsNullOrEmpty(LastName))
        throw new ArgumentException("LastName cannot be null or an empty string.");
    
      if (Address    == null) { Address    = String.Empty; }
      if (City       == null) { City       = String.Empty; }
      if (Region     == null) { Region     = String.Empty; }
      if (PostalCode == null) { PostalCode = String.Empty; }
    
      if (!_initialized) { Initialize(); }
    
      SqlConnection conn = new SqlConnection(_connectionString);
      SqlCommand    cmd  = new SqlCommand("UPDATE Employees " + 
                                          "  SET FirstName=@FirstName, LastName=@LastName, " + 
                                          "  Address=@Address, City=@City, Region=@Region, " +
                                          "  PostalCode=@PostalCode " +
                                          "  WHERE EmployeeID=@EmployeeID", conn);  
    
      cmd.Parameters.Add("@FirstName",  SqlDbType.VarChar, 10).Value = FirstName;
      cmd.Parameters.Add("@LastName",   SqlDbType.VarChar, 20).Value = LastName;
      cmd.Parameters.Add("@Address",    SqlDbType.VarChar, 60).Value = Address;
      cmd.Parameters.Add("@City",       SqlDbType.VarChar, 15).Value = City;
      cmd.Parameters.Add("@Region",     SqlDbType.VarChar, 15).Value = Region;
      cmd.Parameters.Add("@PostalCode", SqlDbType.VarChar, 10).Value = PostalCode;
      cmd.Parameters.Add("@EmployeeID", SqlDbType.Int).Value = EmployeeID;
    
      try
      {
        conn.Open();
    
        if (cmd.ExecuteNonQuery() == 0)
          return false;
      }
      catch (SqlException e)
      {
        // Handle exception.
      }
      finally
      {
        conn.Close();
      }
    
      return true;
    }
    

    More details, please refer to this link: https://msdn.microsoft.com/en-US/library/57hkzhy5.aspx

    Best Regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 17, 2015 3:00 AM