You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET中出现‘Undefined is not a valid value for int64’错误的排查求助

Fixing "Undefined is not a valid value for int64" Error

Let's break down what's causing this error and how to fix it step by step:

1. Fix the Obvious JavaScript Typo

Looking at your folio_OnClick function, there's a clear mistake that's likely contributing to the issue:

field.value = reID;

reID is an undefined variable here—you probably meant ret[0] (since you're using ret[0] to populate txtfolioID.value just above it). Correct this line to:

field.value = ret[0];

Also, double-check that document.getElementById('<%= hfID.ClientID %>') is targeting the right hidden field. If you meant hfResourceID (the one you defined in your ASPX), update it to:

var field = document.getElementById('<%= hfResourceID.ClientID %>');

This ensures your hidden field gets a valid numeric value instead of an undefined one before the postback.

2. Validate Values Before Converting to long

The error happens because you're trying to convert an invalid string (like empty, undefined, or non-numeric text) to a 64-bit integer. Always validate and parse safely instead of using Convert.ToInt64 directly.

For your hidden field:

Replace this unsafe conversion:

long resourceId = Convert.ToInt64(hfResourceID.Value);

With a safe parse:

long resourceId;
if (!string.IsNullOrEmpty(hfResourceID.Value) && long.TryParse(hfResourceID.Value, out resourceId))
{
    // Use the valid resourceId here
}
else
{
    // Fallback to a default value if the input is invalid
    resourceId = 0;
}

For parameters in searchGlobal:

Check values like folioID.Text and Request.QueryString["RequestID"] before passing them to your data source. For example:

// Handle requestID safely
string requestIdStr = Request.QueryString["RequestID"];
long requestId = 0;
if (!string.IsNullOrEmpty(requestIdStr) && long.TryParse(requestIdStr, out requestId))
{
    // Use the parsed value if valid
}
string requestID = requestId.ToString();

// Handle folioID safely
string folioIdText = folioID.Text.Trim();
long folioId = 0;
if (!long.TryParse(folioIdText, out folioId))
{
    // Set a default if input is invalid
    folioId = 0;
}

Then pass the validated string to your data source:

ResourceDataSource.SelectParameters.Add("folioID", folioId.ToString());

3. Verify Your SearchGlobal Data Source Method

Make sure the parameters in your backend SearchGlobal method match the data type you're passing. For example:

  • If folioID or requestID are defined as long in the method, ensure you're passing valid numeric strings.
  • If they're defined as string, add validation inside the method to parse them safely before using them in queries.

4. Add Client-Side Validation (Optional but Helpful)

Prevent invalid values from being sent to the server by adding a quick check before the postback:

// Right before __doPostBack in folio_OnClick
var hfResource = document.getElementById('<%= hfResourceID.ClientID %>');
if (hfResource.value === '' || isNaN(parseInt(hfResource.value))) {
    hfResource.value = '0'; // Reset to valid default if needed
}

var txtfolioID = document.getElementById('<%= txtfolioID.ClientID %>');
if (txtfolioID.value === '' || isNaN(parseInt(txtfolioID.value))) {
    txtfolioID.value = '0';
}

5. Debug to Pinpoint the Exact Issue

If you're still unsure which parameter is causing the problem, add debug logs in your searchGlobal method:

// Log all parameters right before DataBind()
System.Diagnostics.Debug.WriteLine($"Name: {txtName.Text}");
System.Diagnostics.Debug.WriteLine($"RequestID: {requestID}");
System.Diagnostics.Debug.WriteLine($"ResourceType: {resourceType.ToString()}");
System.Diagnostics.Debug.WriteLine($"FolioID: {folioID.Text}");

Check the Output window in Visual Studio to see which value is empty or non-numeric—that's your culprit.

内容的提问来源于stack exchange,提问作者Maximious

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 09:27:32