-1

以下代码正在生成错误:

else if (period.ToString().Equals("3 years"))
    {
        for (int i = 0; i <= 36; i++)
        {
            string strCommandText5 = "INSERT INTO AutoTrans VALUES(@loanID,@transPeriod,null,@transStatus);";

            SqlCommand myCommand5 = new SqlCommand(strCommandText5, myConnection);
            myCommand5.Parameters.AddWithValue("@loanID", Session["@loanID"].ToString());
            myCommand5.Parameters.AddWithValue("@transPeriod", numPeriod);
            myCommand5.Parameters.AddWithValue("@transStatus", status);

            numPeriod++;
            myCommand5.ExecuteNonQuery();
        }
    }

错误信息:

你调用的对象是空的。在 myCommand5.Parameters.AddWithValue("@loanID", Session["@loanID"].ToString());

请帮帮我谢谢

4

3 回答 3

0

更改您的代码如下

else if (period.ToString().Equals("3 years")&&Session["@loanID"]!=null)
{
    for (int i = 0; i <= 36; i++)
    {
        string strCommandText5 = "INSERT INTO AutoTrans VALUES(@loanID,@transPeriod,null,@transStatus);";

        SqlCommand myCommand5 = new SqlCommand(strCommandText5, myConnection);
        myCommand5.Parameters.AddWithValue("@loanID", Session["@loanID"].ToString());
        myCommand5.Parameters.AddWithValue("@transPeriod", numPeriod);
        myCommand5.Parameters.AddWithValue("@transStatus", status);

        numPeriod++;
        myCommand5.ExecuteNonQuery();
    }
}
于 2013-07-30T09:51:23.087 回答
0

Session["@loanID"] 为 null,检查 de session 中的值:

if (Session["@loanID"] == null) { throw new Exception("Session["@loanID"] is null"); } 
myCommand5.Parameters.AddWithValue("@loanID", Session["@loanID"].ToString());
于 2013-07-30T09:52:03.950 回答
0

错误是不言自明的,您的session为空或 null 这就是生成异常的原因。如果您愿意,可以使用它发送 DbNull

myCommand5.Parameters.AddWithValue("@loanID", Session["@loanID"].ToString() ?? (object)DBNull.Value);
于 2013-07-30T09:52:19.040 回答