无法将类型“System.Int16”的对象转换为类型“System.Int32 ASP.NET Core MVC”

问题描述 投票:0回答:1

我有一个搜索 SQL 数据库并显示结果的视图,当我单击编辑链接时,它应该将我带到一个页面来更新信息。相反,我收到此错误消息

无法将“System.Int16”类型的对象转换为“System.Int32”类型。

我尝试过更改商店过程,但没有成功。我没有在程序中的任何地方使用 GetInt32 。在我的存储过程中,参数是 RQKey,它是一个大小不一的 int,因此不需要任何转换。不确定为什么程序会抛出此错误。如有任何帮助,我们将不胜感激。

控制器:

public IActionResult Update(int id)
{
    Request reqs = new Request();

    try
    {
        reqs = GetRequest(id);
    }
    catch (Exception ex)
    {
        throw ex;
    }

    return View(reqs);
}
    
public IActionResult UpdateRequests(Request updatereq)
{
   string ConnectionString = connectionStrings.Get("EquipmentConnectString");

   var objConnect = new SqlConnection(ConnectionString);
   var objReqDataSet = new DataSet();
   string UpdateQuery = "sp_UpdateRequest9";

   try
   {
     var objcommandUpdate = new SqlCommand(UpdateQuery, objConnect);
     objcommandUpdate.CommandType = CommandType.StoredProcedure;

     //Add Parameters
     objcommandUpdate.Parameters.AddWithValue("@RowCount", 1);
     objcommandUpdate.Parameters.AddWithValue("@RQKey", updatereq.Rqkey);
     objcommandUpdate.Parameters.AddWithValue("@Address1", updatereq.Address1);
     objcommandUpdate.Parameters.AddWithValue("@Address2", updatereq.Address2);
     objcommandUpdate.Parameters.AddWithValue("@Address3", updatereq.Address3);
     objcommandUpdate.Parameters.AddWithValue("@ag_ID", updatereq.AgId);
     objcommandUpdate.Parameters.AddWithValue("@ag_Guid", updatereq.AgGuid);
     objcommandUpdate.Parameters.AddWithValue("@ag_Name", updatereq.AgName);
     objcommandUpdate.Parameters.AddWithValue("@ApprovalDate", updatereq.ApprovalDate);
     objcommandUpdate.Parameters.AddWithValue("@Approved", updatereq.Approved);
     objcommandUpdate.Parameters.AddWithValue("@ApprovedBy", updatereq.ApprovedBy);
     objcommandUpdate.Parameters.AddWithValue("@AuditComments", updatereq.AuditComments);
     objcommandUpdate.Parameters.AddWithValue("@AuditDate", updatereq.AuditDate);
     objcommandUpdate.Parameters.AddWithValue("@CaseNum", updatereq.CaseNum);
     objcommandUpdate.Parameters.AddWithValue("@City", updatereq.City);
     objcommandUpdate.Parameters.AddWithValue("@Closed", updatereq.Closed);
     objcommandUpdate.Parameters.AddWithValue("@Comments", updatereq.Comments);
     objcommandUpdate.Parameters.AddWithValue("@Convictions", updatereq.Convictions);
     objcommandUpdate.Parameters.AddWithValue("@CriminalActivity", updatereq.CriminalActivity);
     objcommandUpdate.Parameters.AddWithValue("@DateAgencyReceived", updatereq.DateAgencyReceived);
     objcommandUpdate.Parameters.AddWithValue("@DateAgencyReturned", updatereq.DateAgencyReturned);
     objcommandUpdate.Parameters.AddWithValue("@DateDueBack", updatereq.DateDueBack);
     objcommandUpdate.Parameters.AddWithValue("@DateExtendedUntil", updatereq.DateExtendedUntil);
     objcommandUpdate.Parameters.AddWithValue("@DateIssued", updatereq.DateIssued);
     objcommandUpdate.Parameters.AddWithValue("@DateNeededFrom", updatereq.DateNeededFrom);
     objcommandUpdate.Parameters.AddWithValue("@DateNeededTo", updatereq.DateNeededTo);
     objcommandUpdate.Parameters.AddWithValue("@DateReturnReceived", updatereq.DateReturnReceived);
     objcommandUpdate.Parameters.AddWithValue("@DBEntry", updatereq.Dbentry);
     objcommandUpdate.Parameters.AddWithValue("@Disapproval", updatereq.Disapproval);
     objcommandUpdate.Parameters.AddWithValue("@Email", updatereq.Email);
     objcommandUpdate.Parameters.AddWithValue("@Extend", updatereq.Extend);
     objcommandUpdate.Parameters.AddWithValue("@ExtendApprovalDate", updatereq.ExtendApprovalDate);
     objcommandUpdate.Parameters.AddWithValue("@ExtendApprovedBy", updatereq.ExtendApprovedBy);
     objcommandUpdate.Parameters.AddWithValue("@ExtendComments", updatereq.ExtendComments);
     objcommandUpdate.Parameters.AddWithValue("@ExtendDisapproval", updatereq.ExtendDisapproval);
     objcommandUpdate.Parameters.AddWithValue("@ExtendReason", updatereq.ExtendReason);
     objcommandUpdate.Parameters.AddWithValue("@CellNumber", updatereq.CellNumber);
     objcommandUpdate.Parameters.AddWithValue("@Intel", updatereq.Intel);
     objcommandUpdate.Parameters.AddWithValue("@LECCode", updatereq.Leccode);
     objcommandUpdate.Parameters.AddWithValue("@LECName", updatereq.Lecname);
     objcommandUpdate.Parameters.AddWithValue("@Officer", updatereq.Officer);
     objcommandUpdate.Parameters.AddWithValue("@Refer", updatereq.Refer);
     objcommandUpdate.Parameters.AddWithValue("@ReqId", updatereq.ReqId);
     objcommandUpdate.Parameters.AddWithValue("@RequestAmt", updatereq.RequestAmt);
     objcommandUpdate.Parameters.AddWithValue("@RequestDate", updatereq.RequestDate);
     objcommandUpdate.Parameters.AddWithValue("@Results", updatereq.Results);
     objcommandUpdate.Parameters.AddWithValue("@ResultsDate", updatereq.ResultsDate);
     objcommandUpdate.Parameters.AddWithValue("@ServiceType", updatereq.ServiceType);
     objcommandUpdate.Parameters.AddWithValue("@State", updatereq.State);
     objcommandUpdate.Parameters.AddWithValue("@Technician", updatereq.Technician);
     objcommandUpdate.Parameters.AddWithValue("@Useage", updatereq.Useage);
     objcommandUpdate.Parameters.AddWithValue("@VoiceNumber", updatereq.VoiceNumber);
     objcommandUpdate.Parameters.AddWithValue("@Zip", updatereq.Zip);
     objcommandUpdate.Parameters.AddWithValue("@zzCreationDate", updatereq.ZzCreationDate);
     objcommandUpdate.Parameters.AddWithValue("@zzUpdateDate", updatereq.ZzUpdateDate);
     objcommandUpdate.Parameters.AddWithValue("@zzUserCreated", updatereq.ZzUserCreated);
     objcommandUpdate.Parameters.AddWithValue("@zzUserLastUpdated", updatereq.ZzUserLastUpdated);
     objcommandUpdate.Parameters.AddWithValue("@audioenhancements", updatereq.AudioEnhancements);
     objcommandUpdate.Parameters.AddWithValue("@videoenhancements", updatereq.VideoEnhancements);
      objcommandUpdate.Parameters.AddWithValue("@County", updatereq.County);
      objcommandUpdate.Parameters.AddWithValue("@IndividualsLocated", updatereq.IndividualsLocated);
      objcommandUpdate.Parameters.AddWithValue("@CasesResolved", updatereq.CasesResolved);
      objcommandUpdate.Parameters.AddWithValue("@CasesProsecuted", updatereq.CasesProsecuted);
      objcommandUpdate.Parameters.AddWithValue("@RetrievedAV", updatereq.RetrievedAv);
      objcommandUpdate.Parameters.AddWithValue("@CourtDate", updatereq.CourtDate);
      objcommandUpdate.Parameters.AddWithValue("@ReqsToCourtroom", updatereq.ReqsToCourtroom);
      objcommandUpdate.Parameters.AddWithValue("@OnSiteVisits", updatereq.OnSiteVisits);
      objcommandUpdate.Parameters.AddWithValue("@NewEvidences", updatereq.NewEvidences);
      objcommandUpdate.Parameters.AddWithValue("@NonCaseAVServices", updatereq.NonCaseAvservices);
      objcommandUpdate.Parameters.AddWithValue("@Consultations", updatereq.Consultations);
      objcommandUpdate.Parameters.AddWithValue("@MediumsAnalyzed", updatereq.MediumsAnalyzed);
      objcommandUpdate.Parameters.AddWithValue("@VolumeDataAnalyzedGB", updatereq.VolumeDataAnalyzedGb);

       var objDataAdpater = new SqlDataAdapter(objcommandUpdate);
       objDataAdpater.Fill(objReqDataSet, "Request"); //EquipmentV2 Request table
     }
     catch (Exception ex)
     {
       throw ex;
     }

     return Redirect("Index/" + updatereq.Rqkey); 
}

private Request GetRequest(int id)
{
    string ConnectionString = connectionStrings.Get("EquipmentConnectString");

    var objConnect = new SqlConnection(ConnectionString);
    var objReqDataSet = new DataSet();
    string GetQuery = "GetEquipmentRequest";

    try
    {
        var objCommand = new SqlCommand(GetQuery, objConnect);
        objCommand.CommandType = CommandType.StoredProcedure;
        objCommand.CommandTimeout = 360;

        objCommand.Parameters.AddWithValue("@RQKey", id);

        var objDataAdapter = new SqlDataAdapter(objCommand);
        objDataAdapter.Fill(objReqDataSet, "Request"); //EquipmentV2 Request table
    }
    catch (Exception ex)
    {
        throw ex;
    }

    Request Detailsrq = new Request();
    DataRow row = objReqDataSet.Tables["Request"].Rows[0];

    if (row["RQKey"] is DBNull) 
        Detailsrq.Rqkey = 0;
    else 
        Detailsrq.Rqkey = (int)row["RQKey"];

    if (row["RequestDate"] is DBNull) 
        Detailsrq.RequestDate = System.DateTime.MinValue;
    else 
        Detailsrq.RequestDate = (System.DateTime)row["RequestDate"];

    if (row["ApprovedBy"] is DBNull) 
        Detailsrq.ApprovedBy = "";
    else 
        Detailsrq.ApprovedBy = (string)row["ApprovedBy"];

    if (row["ApprovalDate"] is DBNull) 
        Detailsrq.ApprovalDate = System.DateTime.MinValue;
    else 
        Detailsrq.ApprovalDate = (System.DateTime)row["ApprovalDate"];

    if (row["ServiceType"] is DBNull) 
        Detailsrq.ServiceType = "";
    else 
        Detailsrq.ServiceType = (string)row["ServiceType"];

    if (row["Technician"] is DBNull) 
        Detailsrq.Technician = "";
    else 
        Detailsrq.Technician = (string)row["Technician"];

    if (row["CriminalActivity"] is DBNull) 
        Detailsrq.CriminalActivity = "";
    else 
        Detailsrq.CriminalActivity = (string)row["CriminalActivity"];

    if (row["CaseNum"] is DBNull) 
        Detailsrq.CaseNum = "";
    else 
        Detailsrq.CaseNum = (string)row["CaseNum"];

    if (row["DateNeededFrom"] is DBNull) 
        Detailsrq.DateNeededFrom = System.DateTime.MinValue;
    else 
        Detailsrq.DateNeededFrom = (System.DateTime)row["DateNeededFrom"];

    if (row["DateNeededTo"] is DBNull) 
        Detailsrq.DateNeededTo = System.DateTime.MinValue;
    else 
        Detailsrq.DateNeededTo = (System.DateTime)row["DateNeededTo"];

    if (row["DateIssued"] is DBNull) 
        Detailsrq.DateIssued = System.DateTime.MinValue;
    else 
        Detailsrq.DateIssued = (System.DateTime)row["DateIssued"];

    if (row["DateDueBack"] is DBNull) 
        Detailsrq.DateDueBack = System.DateTime.MinValue;
    else 
        Detailsrq.DateDueBack = (System.DateTime)row["DateDueBack"];

    if (row["ExtendApprovalDate"] is DBNull)  
        Detailsrq.ExtendApprovalDate = System.DateTime.MinValue;
    else 
        Detailsrq.ExtendApprovalDate = (System.DateTime)row["ExtendApprovalDate"];

    if (row["DateAgencyReceived"] is DBNull) 
        Detailsrq.DateAgencyReceived = System.DateTime.MinValue;
    else 
        Detailsrq.DateAgencyReceived = (System.DateTime)row["DateAgencyReceived"];

    if (row["DateAgencyReturned"] is DBNull) 
        Detailsrq.DateAgencyReturned = System.DateTime.MinValue;
    else 
        Detailsrq.DateAgencyReturned = (System.DateTime)row["DateAgencyReturned"];

    if (row["Comments"] is DBNull) 
        Detailsrq.Comments = "";
    else 
        Detailsrq.Comments = (string)row["Comments"];

    if (row["Disapproval"] is DBNull) 
        Detailsrq.Disapproval = "";
    else 
        Detailsrq.Disapproval = (string)row["Disapproval"];

    if (row["ResultsDate"] is DBNull) 
        Detailsrq.ResultsDate = System.DateTime.MinValue;
    else 
        Detailsrq.ResultsDate = (System.DateTime)row["ResultsDate"];

    if (row["CourtDate"] is DBNull) 
        Detailsrq.CourtDate = System.DateTime.MinValue;
    else 
        Detailsrq.CourtDate = (System.DateTime)row["CourtDate"];

    if (row["Results"] is DBNull)  
        Detailsrq.Results = "";
    else 
        Detailsrq.Results = (string)row["Results"];

    if (row["Dbentry"] is DBNull) 
        Detailsrq.Dbentry = 0;
    else 
        Detailsrq.Dbentry = (int)row["Dbentry"];

    if (row["AudioEnhancements"] is DBNull) 
        Detailsrq.AudioEnhancements = 0;
    else 
        Detailsrq.AudioEnhancements = (int)row["AudioEnhancements"];

    if (row["ReqsToCourtroom"] is DBNull) 
        Detailsrq.ReqsToCourtroom = 0;
    else 
        Detailsrq.ReqsToCourtroom = (int)row["ReqsToCourtroom"];

    if (row["Convictions"] is DBNull) Detailsrq.Convictions = 0;
    else Detailsrq.Convictions = (int)row["Convictions"];

    if (row["VideoEnhancements"] is DBNull) Detailsrq.VideoEnhancements = 0;
    else Detailsrq.VideoEnhancements = (int)row["VideoEnhancements"];

    if (row["OnSiteVisits"] is DBNull) Detailsrq.OnSiteVisits = 0;
    else Detailsrq.OnSiteVisits = (int)row["OnSiteVisits"];

    if (row["IndividualsLocated"] is DBNull)    Detailsrq.IndividualsLocated = 0;
    else Detailsrq.IndividualsLocated = (int)row["IndividualsLocated"];

    if (row["RetrievedAv"] is DBNull) Detailsrq.RetrievedAv = 0;
    else Detailsrq.RetrievedAv = (int)row["RetrievedAv"];

    if (row["NewEvidences"] is DBNull) Detailsrq.NewEvidences = 0;
    else Detailsrq.NewEvidences = (int)row["NewEvidences"];

    if (row["CasesResolved"] is DBNull) Detailsrq.CasesResolved = 0;
    else Detailsrq.CasesResolved = (int)row["CasesResolved"];

    if (row["MediumsAnalyzed"] is DBNull) Detailsrq.MediumsAnalyzed = 0;
    else Detailsrq.MediumsAnalyzed = (int)row["MediumsAnalyzed"];

    if (row["NonCaseAvservices"] is DBNull) Detailsrq.NonCaseAvservices = 0;
    else Detailsrq.NonCaseAvservices = (int)row["NonCaseAvservices"];

    if (row["CasesProsecuted"] is DBNull) Detailsrq.CasesProsecuted = 0;
    else Detailsrq.CasesProsecuted = (int)row["CasesProsecuted"];

    if (row["VolumeDataAnalyzedGb"] is DBNull)   Detailsrq.VolumeDataAnalyzedGb = 0;
    else Detailsrq.VolumeDataAnalyzedGb = (int)row["VolumeDataAnalyzedGb"];

    if (row["Consultations"] is DBNull) Detailsrq.Consultations = 0;
    else Detailsrq.Consultations = (int)row["Consultations"];

    if (row["Intel"] is DBNull) Detailsrq.Intel = 0;
    else Detailsrq.Intel = (byte)row["Intel"];

    if (row["Refer"] is DBNull) Detailsrq.Refer = 0;
    else Detailsrq.Refer = (byte)row["Refer"];

    if (row["ReqId"] is DBNull) Detailsrq.ReqId = "";
    else Detailsrq.ReqId = (string)row["ReqId"];

    if (row["ag_Name"] is DBNull) Detailsrq.AgName = "";
    else Detailsrq.AgName = (string)row["ag_Name"];

    if (row["Officer"] is DBNull) Detailsrq.Officer = "";
    else Detailsrq.Officer = (string)row["Officer"];

    if (row["Address1"] is DBNull) Detailsrq.Address1 = "";
    else Detailsrq.Address1 = (string)row["Address1"];

    if (row["Address2"] is DBNull) Detailsrq.Address2 = "";
    else Detailsrq.Address2 = (string)row["Address2"];

    if (row["Address3"] is DBNull) Detailsrq.Address3 = "";
    else Detailsrq.Address3 = (string)row["Address3"];

    if (row["City"] is DBNull) Detailsrq.City = "";
    else Detailsrq.City = (string)row["City"];

    if (row["State"] is DBNull) Detailsrq.State = "";
    else Detailsrq.State = (string)row["State"];

    if (row["Zip"] is DBNull) Detailsrq.Zip = "";
    else Detailsrq.Zip = (string)row["Zip"];

    if (row["Email"] is DBNull) Detailsrq.Email = "";
    else Detailsrq.Email = (string)row["Email"];

    if (row["County"] is DBNull) Detailsrq.County = "";
    else Detailsrq.County = (string)row["County"];

    if (row["VoiceNumber"] is DBNull) Detailsrq.VoiceNumber = "";
    else Detailsrq.VoiceNumber = (string)row["VoiceNumber"];

    if (row["CellNumber"] is DBNull) Detailsrq.CellNumber = "";
    else Detailsrq.CellNumber = (string)row["CellNumber"];

    if (row["ExtendApprovedBy"] is DBNull) Detailsrq.ExtendApprovedBy = "";
    else Detailsrq.ExtendApprovedBy = (string)row["ExtendApprovedBy"];

    if (row["ExtendApprovalDate"] is DBNull) Detailsrq.ExtendApprovalDate = System.DateTime.MinValue;
    else Detailsrq.ExtendApprovalDate = (System.DateTime)row["ExtendApprovalDate"];

    if (row["ExtendReason"] is DBNull) Detailsrq.ExtendReason = "";
    else Detailsrq.ExtendReason = (string)row["ExtendReason"];

    if (row["ExtendComments"] is DBNull) Detailsrq.ExtendComments = "";
    else Detailsrq.ExtendComments = (string)row["ExtendComments"];

    if (row["ExtendDisapproval"] is DBNull) Detailsrq.ExtendDisapproval = "";
    else Detailsrq.ExtendDisapproval = (string)row["ExtendDisapproval"];

    return Detailsrq;
}

查看:

@model SearchContact
<body>
<form asp-action="ContactPages" asp-controller="Contacts" method="post">
    <div class="full-width">
        <table>
            <tr>
                <td>
                    <h6>Contact Search</h6>
                    <hr />
                </td>
            </tr>
        </table>
        <table cellpadding="3" border="0">
            <tr>
                <td align="right" style="padding:4pt">
                    Type:
                </td>
                <td align="left">
                    <select asp-for="Type">
                        <option></option>
                        <option value="Brand">Brand</option>
                        <option value="Vendor">Vendor</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td align="right" style="padding:4pt">
                    Brand/Vendor Name:
                </td>
                <td align="left">
                    <input type="text" id="brandvenName" asp-for="CompanyName"/>
                </td>
            </tr>
            <tr>
                <td align="right" style="padding:4pt">
                    Contact Name:
                </td>
                <td align="left">
                    <input type="text" id="ContactName" asp-for="ContactName"/>
                </td>
            </tr>
            <tr>
                <td align="right" style="padding-right:4pt">
                    State:
                </td>
                <td align="left">
                    <select asp-for="State">
                        @{
                            Html.RenderPartial("~/Views/StateListAbbr.cshtml");
                        }
                    </select>
                   
                </td>
            </tr>
        </table>
        <br />
        <input type="submit" value="Search"/>
        <br />
        <a asp-action="AddContact" asp-controller="Contacts">Add Contact</a>
    </div>
</form>
@if (Model.Contacts != null && Model.Contacts.Any())
{
    <br />
    <table class="table table-bordered table-striped" style="width:75%;
    background-color:white; border:1px solid black;">
        <thead>
            <tr>
                <th>Brand/Vendor Name</th>
                <th>Type</th>
                <th>Contact Name</th>
                <th>City</th>
                <th>State</th>
                <th>Purchase Note</th>
                <th>Support Note</th>
                <th><!--Edit--></th>
                
            </tr>
        </thead>
        @foreach (var con in Model.Contacts)
        {
            <tr>
                <td>@con.EntityName</td>
                <td>@con.EntityType</td>
                <td>@con.ContactName</td>
                <td>@con.City</td>
                <td>@con.State</td>
                <td>@con.NotePurchase</td>
                <td>@con.NoteSupport</td>
                <td>
                    <a asp-action="UpdateContactPage" asp-route-id="@con.Ctvdkey">Edit</a>
                </td>
                
            </tr>
        }
    </table>
}


<style>

    .body-container {
        /*remove the container default padding attribute*/
        /*padding-left: 0px !important;
                                padding-right: 0px !important;*/
        /*increase width as per your need*/
        max-width: 100%;
    }
</style>
</body>
asp.net-core-mvc integer .net-6.0
1个回答
0
投票

我发现在我的 SQL 数据库中有一列使用smallint而不是int。将此列更改为 int 解决了我的问题,我现在可以单击编辑链接并且它可以工作。

© www.soinside.com 2019 - 2024. All rights reserved.