Asp.net 并非所有路径在模型类中都返回值错误
我的模型类如下所示:Asp.net 并非所有路径在模型类中都返回值错误,asp.net,asp.net-mvc-4,ado.net,Asp.net,Asp.net Mvc 4,Ado.net,我的模型类如下所示: using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.Drawing.Imaging; using System.Drawing.Drawing2D; using System.Linq; using System
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.Mvc;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace OEWebPortalMVCVer5.Models
{
class DenialModel
{
public Dictionary< string, DataSet> DataSets { get; set; }
public static DataSet ReadRows(DataSet dataset)
{
try
{
SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["demo10ConnectionString"].ConnectionString);
string startDate = "1/1/1900";
string endDate = "12/31/2020";
conn.Open();
DataSet dtDS = new DataSet();
SqlCommand dtCom = new SqlCommand("dbo.cusGenDenialReport", conn);
dtCom.Parameters.AddWithValue("@StartDate", SqlDbType.DateTime).Value = Convert.ToDateTime(startDate);
dtCom.Parameters.AddWithValue("@EndDate", SqlDbType.DateTime).Value = Convert.ToDateTime(endDate);
dtCom.CommandType = CommandType.StoredProcedure;
SqlDataAdapter dtDa = new SqlDataAdapter(dtCom);
dtDa.Fill(dtDS);
conn.Close();
dtCom.Dispose();
}
catch (Exception ex)
{
string message = "It didn't work because: " + ex.Message.ToString();
}
}
}
}
我的第一个想法是,我没有处理参数startDate和endDate的空值,所以我硬编码了它们,稍后将解决这个问题。事实并非如此,因此我添加了一个try-catch块来获取错误。那当然没用(我一定很累了,因为我想试一试能神奇地解决这个问题)。我用谷歌搜索了这个错误,搜索了这个网站,没有找到这个问题的任何相关答案
我不知道如何让代码在此时正确编译。谢谢你的帮助
谢谢错误准确地说明了问题所在:函数中没有
return
语句
public static DataSet ReadRows(DataSet dataset)
{
DataSet dtDS = null;
try
{
SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["demo10ConnectionString"].ConnectionString);
string startDate = "1/1/1900";
string endDate = "12/31/2020";
conn.Open();
dtDS = new DataSet();
SqlCommand dtCom = new SqlCommand("dbo.cusGenDenialReport", conn);
dtCom.Parameters.AddWithValue("@StartDate", SqlDbType.DateTime).Value = Convert.ToDateTime(startDate);
dtCom.Parameters.AddWithValue("@EndDate", SqlDbType.DateTime).Value = Convert.ToDateTime(endDate);
dtCom.CommandType = CommandType.StoredProcedure;
SqlDataAdapter dtDa = new SqlDataAdapter(dtCom);
dtDa.Fill(dtDS);
conn.Close();
dtCom.Dispose();
}
catch (Exception ex)
{
//You probably should let it break...
string message = "It didn't work because: " + ex.Message.ToString();
}
return dtDS;
}
错误正好说明了问题所在:函数中没有
return
语句。@Johnny5答案是这样的,您应该这样发布它。即使在使用视图时也是这样吗?请原谅我的问题,如果它是愚蠢的,但我只是了解到,错误在任何情况下都与MVC或任何东西无关。在C#中,函数必须返回值,除非其返回类型设置为void,否则在这种情况下它永远不会返回值。此外,不能在不同的情况下返回不同的类型,就像在javascript或(我认为)php等非强类型语言中一样。
public static DataSet ReadRows(DataSet dataset)
{
DataSet dtDS = null;
try
{
SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["demo10ConnectionString"].ConnectionString);
string startDate = "1/1/1900";
string endDate = "12/31/2020";
conn.Open();
dtDS = new DataSet();
SqlCommand dtCom = new SqlCommand("dbo.cusGenDenialReport", conn);
dtCom.Parameters.AddWithValue("@StartDate", SqlDbType.DateTime).Value = Convert.ToDateTime(startDate);
dtCom.Parameters.AddWithValue("@EndDate", SqlDbType.DateTime).Value = Convert.ToDateTime(endDate);
dtCom.CommandType = CommandType.StoredProcedure;
SqlDataAdapter dtDa = new SqlDataAdapter(dtCom);
dtDa.Fill(dtDS);
conn.Close();
dtCom.Dispose();
}
catch (Exception ex)
{
//You probably should let it break...
string message = "It didn't work because: " + ex.Message.ToString();
}
return dtDS;
}