并非所有代码路径都返回一个值 [英] not all code paths returns a value

查看:62
本文介绍了并非所有代码路径都返回一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  public   class  classDAL 
{
string connection = ConfigurationManager.ConnectionStrings [ ConnectionString].ConnectionString;
public string InsertUserDetails(classBLL Objbal) / / 此处出现此错误
DAL.classDAL.InsertUserDetails(BLL.classBLL) // :并非所有代码路径都返回值为什么?
{
SqlConnection con = new SqlConnection(连接);
SqlCommand cmd = new SqlCommand( sp_InsertEmpdetails ,con);
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
尝试
{
cmd.Parameters.AddWithValue( < span class =code-string> @ Name,Objbal.Name);
cmd.Parameters.AddWithValue( @ Roll_Number,Objbal.Roll_Number);
cmd.Parameters.AddWithValue( @ Email_Id,Objbal.Email_Id);
cmd.Parameters.AddWithValue( @ Mobile_Number,Objbal.Mobile_Number);
Console.WriteLine( 保存数据);
}
catch (Ex Ex)
{
Console.WriteLine(Ex);
}
}
}
}

解决方案

方法的返回类型 InsertUserDetails 是字符串,但您不会从此方法返回任何内容。您可能希望返回该字符串而不是Console.WriteLine。因此,将在您的代码中分别返回数据已保存返回Ex.Message

<试试这个

公共字符串InsertUserDetails(classBLL Objbal)//这里我收到此错误
{
SqlConnection con = new SqlConnection (连接);
SqlCommand cmd = new SqlCommand(sp_InsertEmpdetails,con);
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
尝试
{
cmd.Parameters.AddWithValue(@ Name,Objbal.Name);
cmd.Parameters.AddWithValue(@ Roll_Number,Objbal.Roll_Number);
cmd.Parameters.AddWithValue(@ Email_Id,Objbal.Email_Id);
cmd.Parameters.AddWithValue(@ Mobile_Number,Objbal.Mobile_Number);
Console.WriteLine(数据保存);
返回保存数据;
}
catch(Exception Ex)
{
Console.WriteLine(Ex);
返回Ex.Message;
}

}









 public void InsertUserDetails(classBLL Objbal)
{
SqlConnection con = new SqlConnection(connection);
SqlCommand cmd = new SqlCommand(sp_InsertEmpdetails,con);
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
尝试
{
cmd.Parameters.AddWithValue(@ Name,Objbal.Name);
cmd.Parameters.AddWithValue(@ Roll_Number,Objbal.Roll_Number);
cmd.Parameters.AddWithValue(@ Email_Id,Objbal.Email_Id);
cmd.Parameters.AddWithValue(@ Mobile_Number,Objbal.Mobile_Number);
Console.WriteLine(数据保存);
}
catch(Exception Ex)
{
Console.WriteLine(Ex);
}

}





谢谢,

-RG


public class classDAL
    {
        string connection = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
      public string InsertUserDetails(classBLL Objbal)//here iam getting this error 
DAL.classDAL.InsertUserDetails(BLL.classBLL)//:not all code paths returns a value why? 
      {
          SqlConnection con = new SqlConnection(connection);
          SqlCommand cmd = new SqlCommand("sp_InsertEmpdetails", con);
          cmd.CommandType = CommandType.StoredProcedure;
          con.Open();
          try
          {
              cmd.Parameters.AddWithValue("@Name", Objbal.Name);
              cmd.Parameters.AddWithValue("@Roll_Number", Objbal.Roll_Number);
              cmd.Parameters.AddWithValue("@Email_Id", Objbal.Email_Id);
              cmd.Parameters.AddWithValue("@Mobile_Number", Objbal.Mobile_Number);
              Console.WriteLine("data saved");
          }
          catch(Exception Ex)
          {
              Console.WriteLine(Ex);
          }
      }   
    }  
}

解决方案

The return type of your method InsertUserDetails is string but you do not return anything from this method. instead of Console.WriteLine you may want to return that string. So it will be return "data saved" and return Ex.Message respectively in your code.


Try this

public string InsertUserDetails(classBLL Objbal)//here iam getting this error
       {
           SqlConnection con = new SqlConnection(connection);
           SqlCommand cmd = new SqlCommand("sp_InsertEmpdetails", con);
           cmd.CommandType = CommandType.StoredProcedure;
           con.Open();
           try
           {
               cmd.Parameters.AddWithValue("@Name", Objbal.Name);
               cmd.Parameters.AddWithValue("@Roll_Number", Objbal.Roll_Number);
               cmd.Parameters.AddWithValue("@Email_Id", Objbal.Email_Id);
               cmd.Parameters.AddWithValue("@Mobile_Number", Objbal.Mobile_Number);
               Console.WriteLine("data saved");
               return "data saved";
           }
           catch (Exception Ex)
           {
               Console.WriteLine(Ex);
               return Ex.Message;
           }

       }



OR

public void InsertUserDetails(classBLL Objbal)
       {
           SqlConnection con = new SqlConnection(connection);
           SqlCommand cmd = new SqlCommand("sp_InsertEmpdetails", con);
           cmd.CommandType = CommandType.StoredProcedure;
           con.Open();
           try
           {
               cmd.Parameters.AddWithValue("@Name", Objbal.Name);
               cmd.Parameters.AddWithValue("@Roll_Number", Objbal.Roll_Number);
               cmd.Parameters.AddWithValue("@Email_Id", Objbal.Email_Id);
               cmd.Parameters.AddWithValue("@Mobile_Number", Objbal.Mobile_Number);
               Console.WriteLine("data saved");
           }
           catch (Exception Ex)
           {
               Console.WriteLine(Ex);
           }

       }



Thanks,
-RG


这篇关于并非所有代码路径都返回一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆