本文介绍了显示异常,无法将对象从DBNull强制转换为其他类型。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void SaveData()
       {
       try
           {
               objVehicleCategory = new VehicleCategoryPresenter();
               if (Session["CorporateID"] != null)
               {
                   CorporateID = Convert.ToInt32(Session["CorporateID"]);
               }

            int result= objVehicleCategory.SaveData( txtVehicleCategory.Text,Convert.ToInt32 (txtVehicleCategory1.Text), txtDescription.Text,CorporateID);
            if (result == 1)
            {
                ucPageHeader.DisplayMessage = Resources.Messages.SaveSuccess;
            }
            else if (result == 2)
            {
                ucPageHeader.DisplayMessage = Resources.Messages.VehicleCategoryExist;
            }
            else
            {
                ucPageHeader.DisplayMessage = Resources.Messages.SaveFailed;
            }
            txtVehicleCategory.Text = string.Empty;

                   txtDescription.Text=string.Empty;
           }
           catch (Exception ex)
           {

           }
       }
  

 public int SaveData( string CategoryName,int NumberOfSeats, string Description,int CorporateID)
        {
            return objVehicleCategory.SaveData( CategoryName, NumberOfSeats, Description,CorporateID);
        }



<pre lang="cs">public int SaveData( string CategoryName,int NumberOfSeats, string Description,int CorporateID)
        {
            try
            {
                var outputParameter = new ObjectParameter("VehicleCategoryID", typeof(Int32));
                objEntity.usp_VehicleCategory_Insert(outputParameter, CategoryName, NumberOfSeats, Description, CorporateID);
                return Convert.ToInt32(outputParameter.Value);
            }
            catch (Exception ex)
            {
                return 0;
            }
        }







public virtual int usp_VehicleCategory_Insert(ObjectParameter vehicleCategoryID,string categoryName,Nullable< int> numberOfSeats,string description,Nullable< int> corporateID)

{

var categoryNameParameter = categoryName!= null?

new ObjectParameter(CategoryName,categoryName):

new ObjectParameter(CategoryName,typeof(string));



var numberOfSeatsParameter = numberOfSeats.HasValue?

new ObjectParameter(NumberOfSeats,numberOfSeats):

new ObjectParameter(NumberOfSeats,typeof(int));



var descriptionParameter = description!= null?

new ObjectParameter(Description,description):

new ObjectParameter(Description,typeof(string));



var corporateIDParameter = corporateID.HasValue?

new ObjectParameter(CorporateID,corporateID):

新的ObjectParameter(CorporateID,typeof(int));



return((IObjectContextAdapter)this).ObjectContext.ExecuteFunction(usp_VehicleCategory_Insert, vehicleCategoryID,categoryNameParameter,numberOfSeatsParameter,descriptionParameter,corporateIDParameter);

}




public virtual int usp_VehicleCategory_Insert(ObjectParameter vehicleCategoryID, string categoryName, Nullable<int> numberOfSeats, string description, Nullable<int> corporateID)
{
var categoryNameParameter = categoryName != null ?
new ObjectParameter("CategoryName", categoryName) :
new ObjectParameter("CategoryName", typeof(string));

var numberOfSeatsParameter = numberOfSeats.HasValue ?
new ObjectParameter("NumberOfSeats", numberOfSeats) :
new ObjectParameter("NumberOfSeats", typeof(int));

var descriptionParameter = description != null ?
new ObjectParameter("Description", description) :
new ObjectParameter("Description", typeof(string));

var corporateIDParameter = corporateID.HasValue ?
new ObjectParameter("CorporateID", corporateID) :
new ObjectParameter("CorporateID", typeof(int));

return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction("usp_VehicleCategory_Insert", vehicleCategoryID, categoryNameParameter, numberOfSeatsParameter, descriptionParameter, corporateIDParameter);
}

推荐答案


这篇关于显示异常,无法将对象从DBNull强制转换为其他类型。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 17:30