Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 并非所有代码路径都返回值问题_C#_Asp.net_.net - Fatal编程技术网

C# 并非所有代码路径都返回值问题

C# 并非所有代码路径都返回值问题,c#,asp.net,.net,C#,Asp.net,.net,我在一个类数据库中有这段代码 并非所有路径都返回值 任何帮助都将不胜感激 public static DataSet DELETE_PDT(String rowid) { SqlConnection con = new SqlConnection(); SqlCommand cmd = new SqlCommand("sp_DELETE_PDT", con); cmd.CommandType = CommandType.StoredProcedure; cmd.P

我在一个类数据库中有这段代码

并非所有路径都返回值

任何帮助都将不胜感激

public static DataSet DELETE_PDT(String rowid)
{
    SqlConnection con = new SqlConnection();
    SqlCommand cmd = new SqlCommand("sp_DELETE_PDT", con);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.Add("@rowid", SqlDbType.Int).Value = rowid;

    con.ConnectionString = ConfigurationManager.ConnectionStrings["WMS"].ConnectionString;

    try
    {
        con.Open();
        cmd.ExecuteNonQuery();
        cmd.Dispose();
        con.Close();
    }
    catch (Exception ex)
    {
        throw new Exception("Error while deleting record. Please contact your System Administrator", ex);
    }
}
您的方法应该有一个返回值,因为您已将其声明为返回Dataset

例如

return new Dataset();
如果不想看到错误消息,请更改

public static DataSet DELETE_PDT(String rowid)
{
   // statements
}

更新1

根据您的评论,我将修改它以再次返回字符串值

public static string DELETE_PDT(String rowid)
{
    using (SqlConnection con = new SqlConnection())
    {
        con.ConnectionString = ConfigurationManager.ConnectionStrings["WMS"].ConnectionString;
        using (SqlCommand cmd = new SqlCommand("sp_DELETE_PDT", con))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@rowid", SqlDbType.Int).Value = rowid;
            try
            {
                if (con.State == ConnectionState.Open)
                con.Close();
                con.Open();
                cmd.ExecuteNonQuery();
                return "Record has been deleted";
            }
            catch (Exception ex)
            {
                return "Error while deleting record. Please contact your System Administrator";
            }
        }
    }
}

您已经声明了返回数据集的方法。。。但你什么也不回。所以您需要返回DataSet的实例或更改方法签名。

您已声明该方法应返回DataSet,但它不会返回任何与该方法所做操作符合逻辑的内容。将签名更改为:

public static void DELETE_PDT(String rowid)
旁注:为了避免出现异常而留下未关闭的连接,可以使用。它确保无论发生什么情况,都始终释放命令和连接:

public static void DELETE_PDT(String rowid) {
  using (SqlConnection con = new SqlConnection()) {
    con.ConnectionString = ConfigurationManager.ConnectionStrings["WMS"].ConnectionString;
    using (SqlCommand cmd = new SqlCommand("sp_DELETE_PDT", con)) {
      cmd.CommandType = CommandType.StoredProcedure;
      cmd.Parameters.Add("@rowid", SqlDbType.Int).Value = rowid;
      try {
        con.Open();
        cmd.ExecuteNonQuery();
      } catch (Exception ex) {
        throw new Exception("Error while deleting record. Please contact your System Administrator", ex);
      }
    }
  }
}

我已经重构了你的代码;试试这个:

public static void DELETE_PDT(String rowid) {
    using(SqlConnection con = new SqlConnection()) {
        con.ConnectionString = ConfigurationManager.ConnectionStrings["WMS"].ConnectionString;
        using(SqlCommand cmd = new SqlCommand("sp_DELETE_PDT", con)) {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@rowid", SqlDbType.Int).Value = rowid;

            try
            {
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
            }
            catch (Exception ex)
            {
                throw new Exception("Error while deleting record. Please contact your System Administrator", ex);
            }
        }
    }
}

我能把它改成什么?Thanks@jorame:将返回类型更改为void,即不返回任何内容,或实际返回一个数据集。再次感谢您John。John问题,我现在如何从主代码调用此方法?这是我之前的“DataBase.DELETE_PDTrowid;”@我也一样。您需要指定类名,因为该方法是静态的。DataBase.DELETE_PDTrowid;我想我明白了。我用了一个HiddenField作为rowid。让我试试John question,我正在从sp_DELETE_PDT返回一条消息。如何将其分配给主代码中的标签。我收到了这条消息。Text=DataBase.DELETE\u pdt并使用以下方法进行重新分解:
public static void DELETE_PDT(String rowid) {
    using(SqlConnection con = new SqlConnection()) {
        con.ConnectionString = ConfigurationManager.ConnectionStrings["WMS"].ConnectionString;
        using(SqlCommand cmd = new SqlCommand("sp_DELETE_PDT", con)) {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@rowid", SqlDbType.Int).Value = rowid;

            try
            {
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
            }
            catch (Exception ex)
            {
                throw new Exception("Error while deleting record. Please contact your System Administrator", ex);
            }
        }
    }
}