为什么使用ASP.Net为ajax中的字符串发生“System.IndexOutOfRangeException”?

时间:2017-04-05 07:10:31

标签: javascript c# jquery asp.net ajax

实际上,如果删除一个特定的$('.list-of-link').on('click', 'a', function (e) { e.preventDefault();// add this line alert($(this).html()); //window.location.replace("ReportTotalSalesPivot.aspx"); var userFileName = $(this).html(); $.ajax({ url: 'SavedReports.aspx/getReportDetails', method: 'post', contentType: 'application/json', data: '{userFileName:"' + userFileName + '"}', dataType:'json', success: function (data) { alert('success : ReportData = ' + data.d.ReportData); }, error: function (error) { alert('Please Call Administrator'); } }) }) 变量

,我的ajax代码就能正常工作

我正在调用ajax,然后服务器将从SQL Server中选择一些数据并将其存储到字符串变量中。每个变量都完美地返回,除了一个变量。当我检查控制台时,它返回异常“ System.IndexOutOfRangeException

这是我的ajax代码

[WebMethod]
    public static SavedReport getReportDetails(string userFileName)
    {
        string cs = ConfigurationManager.ConnectionStrings["HQWebMatajer13"].ConnectionString;
        using (SqlConnection con = new SqlConnection(cs))
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = "select ReportData,ReportFilter,FromDate,ToDate,SelectedData,SelectedCoulmn,SelectedRow,HiddenTableRecord ToDate FROM [HQWebMatajer].[dbo].[ReportSave] where UserID=@UserID and UserFileName=@UserFileName";
            cmd.Parameters.AddWithValue("@UserID", UserID);
            cmd.Parameters.AddWithValue("@UserFileName", userFileName);

            con.Open();

            SavedReport savedReport = new SavedReport();

            SqlDataReader rd = cmd.ExecuteReader();

            while (rd.Read())
            {
                byte[] binaryString = (byte[])rd["ReportData"];
                savedReport.ReportData = Encoding.UTF8.GetString(binaryString);

                savedReport.ReportFilter = rd["ReportFilter"].ToString();
                savedReport.FromDate = rd["FromDate"].ToString();
                savedReport.ToDate = rd["ToDate"].ToString();
                savedReport.SelectedData = rd["SelectedData"].ToString();
                savedReport.SelectedColumn = rd["SelectedCoulmn"].ToString();
                savedReport.SelectedRow = rd["SelectedRow"].ToString();
                savedReport.HiddenTableRecord = rd["HiddenTableRecord"].ToString();
            }
            return savedReport;
        }

    }

WebMethod 代码

HiddenTableRecord

最后一个变量出现错误 HiddenTableRecord

  

以下记录是来自SQL Server tq.StoreID$$$ IN('1001')$$$

public class SavedReport { public string UserID { get; set; } public string ReportName { get; set; } public string UserFileName { get; set; } public string ReportData { get; set; } public string ReportFilter { get; set; } public string FromDate { get; set; } public string ToDate { get; set; } public string SelectedData { get; set; } public string SelectedColumn { get; set; } public string SelectedRow { get; set; } public string HiddenTableRecord { get; set; } } 的实际记录

已保存的报告类代码

savedReport.HiddenTableRecord = rd["HiddenTableRecord"].ToString();

错误消息

  

{消息:“HiddenTableRecord”,...}

     

ExceptionType: “System.IndexOutOfRangeException”

     

消息: “HiddenTableRecord”

注意

  

如果我评论此行t = (2, 4) x, y = t 。错误没有发生,它返回我期望的所有记录

1 个答案:

答案 0 :(得分:2)

问题来了,因为您的SqlDataReader不包含" HiddenTableRecord"

您的SqlRequest中可能存在问题( HiddenTableRecord ToDate 之间的示例中缺少逗号):

"选择ReportData,ReportFilter,FromDate,ToDate,SelectedData,SelectedCoulmn,SelectedRow,HiddenTableRecord ToDate FROM [HQWebMatajer]。[dbo]。[ReportSave]其中UserID = @UserID和UserFileName = @ UserFileName&#34 ;;