错误,因为并非所有代码路径返回值 [英] error as not all code path return value

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

问题描述

你好朋友

以下是我的代码

Hi friends

The following is my code

public bool insertstudentsection1(System.Windows.Forms.DataGridView grid,business.Commonpropertis proper)
        {
            if (grid.Rows.Count != null)
            {
                for (int i = 0; i < grid.Rows.Count - 1; i++)
                {
                    string storedprocedure = "sp_insertstudentsection";
                    int admissionid = (int)grid.Rows[i].Cells[0].Value;
                    string StudentName = (string)grid.Rows[i].Cells[1].Value;
                    string Class = (string)grid.Rows[i].Cells[0].Value;
                    char section = (char)grid.Rows[i].Cells[0].Value;
                    SqlParameter[] param = new SqlParameter[6];
                    param[0] = new SqlParameter("@admissionnumber", admissionid);
                    param[1] = new SqlParameter("@name", StudentName);
                    param[2] = new SqlParameter("@classname", Class);
                    param[3] = new SqlParameter("@sectionname", section);
                    param[4] = new SqlParameter("@datetime", proper.DATETIME);
                    param[5] = new SqlParameter("@adminid", proper.ADMINID);
                    return dbconn.executeInsertStoredProcedure(storedprocedure, param);
                }
            }
            else
            {
                return false;
            }
        }




此方法显示错误,因为并非所有代码路径返回值.如何解决问题
提前致谢




this method shows an error as not all code path return value. How can solve the problem
Advance thanks

推荐答案

您正试图将数据网格中的每一行插入数据库.您不能在每一行之后返回,因此您需要重新考虑执行此方法的方式.
如果您并不真正关心家庭,那么可以成功插入很多东西,您可以像下面这样简单地进行操作:
You are trying to insert each row from your datagrid into the database. You can''t return after each row, so you''ll need to rethink the way you are doing this method.
If you don''t really care home many successful insertions take place, you could do it simply like this:
if(grid.Rows.Count > 0) // (Count will never be null)
{
   bool allInsertsAreOk = true;
   string storedprocedure = "sp_insertstudentsection"; // Just more efficient. Because it's constant, don't recreat it on every iteration.
   foreach( var row in grid.Rows )
   {
      int admissionid = (int)row.Cells[0].Value;
      string StudentName = (string)row.Cells[1].Value;
      string Class = (string)row.Cells[0].Value;
      char section = (char)row.Cells[0].Value;
      SqlParameter[] param = new SqlParameter[6];
      param[0] = new SqlParameter("@admissionnumber", admissionid);
      param[1] = new SqlParameter("@name", StudentName);
      param[2] = new SqlParameter("@classname", Class);
      param[3] = new SqlParameter("@sectionname", section);
      param[4] = new SqlParameter("@datetime", proper.DATETIME);
      param[5] = new SqlParameter("@adminid", proper.ADMINID);
      if( !dbconn.executeInsertStoredProcedure(storedprocedure, param) )
      {
         allInsertsAreOk = false;
      }
   }
   return allInsertsAreOk;
}



我还建议您不要按原样进行转换.



I would also suggest that you not do your conversions the way you are.

//To convert an integer from string. This won't crash the program if there is a problem with input.
int value;
if( !Int32.TryParse( row.Cells[0].Value, out value ) )
{
   // Now if something went wrong in the conversion you can do something about it.
}


无法保证代码将进入您的for循环,您需要在for循环之外添加更多的返回值,或者在...的外部添加另一个返回值您的for循环和拳头if语句内.


There is no guarantee that the code is going to enter your for loop, you need to more your return outside of the for loop or add another return on the outside of your for loop and inside your fist if statement.


public bool insertstudentsection1(System.Windows.Forms.DataGridView grid,business.Commonpropertis proper)
        {
            if (grid.Rows.Count != null)
            {
                for (int i = 0; i < grid.Rows.Count - 1; i++)
                {
                    string storedprocedure = "sp_insertstudentsection";
                    int admissionid = (int)grid.Rows[i].Cells[0].Value;
                    string StudentName = (string)grid.Rows[i].Cells[1].Value;
                    string Class = (string)grid.Rows[i].Cells[0].Value;
                    char section = (char)grid.Rows[i].Cells[0].Value;
                    SqlParameter[] param = new SqlParameter[6];
                    param[0] = new SqlParameter("@admissionnumber", admissionid);
                    param[1] = new SqlParameter("@name", StudentName);
                    param[2] = new SqlParameter("@classname", Class);
                    param[3] = new SqlParameter("@sectionname", section);
                    param[4] = new SqlParameter("@datetime", proper.DATETIME);
                    param[5] = new SqlParameter("@adminid", proper.ADMINID);
                    //Move this return to where I have my last comment
                    return dbconn.executeInsertStoredProcedure(storedprocedure, param);
                }
                //add a return here or more the return out here
                return false;
            }
            else
            {
                return false;
            }
        }


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

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