求解无法找到表0错误

时间:2016-09-07 10:36:55

标签: c# sql sql-server-2008 dataset

我不知道我的代码有什么问题我在asp.net c#中一直收到此错误“无法找到表0”。我使用过数据集。

我写了一个存储过程,如下所示

If @mode='INSERT'
     Begin
     if exists(select 1 from COD_BLU_OPENING_VOLUME where target_dt=convert(date,getdate(),101) and JobID=@openJob)
                        Begin
                If not exists(select 1 from COD_BLU_INVENTORY with(nolock) where (JOBID=@job and STATUS='A' and TargetDt = convert(date,@targetdt,101)))
                    Begin
                        insert into COD_BLU_INVENTORY(JOBID,TargetDt,CarryOver,FreshVol,TotalVol,Status,UpdatedBy,UpdateDt) 
                        VALUES(@job,@targetdt,@CarryoverVol,@FreshVol,@TotalVol,'A',@Empcode,GETDATE())
                        select '1'
                    End
                else
                    Begin 
                        select'0'
                    End
            End     

         if not exists(select 1 from COD_BLU_OPENING_VOLUME where target_dt<=convert(date,getdate(),101) and JobID=@openJob)

            Begin
                insert into COD_BLU_OPENING_VOLUME(target_dt,JobID,Opening_Count,Updated_by,Updated_Dt,Pre_AssignedCount) 
                values(@openTarDt,@openJob,@openCnt,@openEmpcode,GETDATE(),@preOpenCnt)

                If not exists(select 1 from COD_BLU_INVENTORY with(nolock) where (JOBID=@job and STATUS='A' and TargetDt = convert(date,@targetdt,101)))
                    Begin
                        insert into COD_BLU_INVENTORY(JOBID,TargetDt,CarryOver,FreshVol,TotalVol,Status,UpdatedBy,UpdateDt) 
                        VALUES(@job,@targetdt,@CarryoverVol,@FreshVol,@TotalVol,'A',@Empcode,GETDATE())
                        select '0'
                    End
                else
                    Begin 
                        select'1'
                    End
            End

End

我的C#代码如下,

if (btnSave.Text == "Save (F9)")
            {
                objBE.mode = "INSERT";
                objBE.carryoverVol = Convert.ToInt32(txtCarryOver.Text);
                objBE.freshVol = Convert.ToInt32(txtFreshVol.Text);
                objBE.totalVol = Convert.ToInt32(txtTotalVol.Text);
                objBE.job = ddljob.SelectedValue;
                DateTime temp = DateTime.ParseExact(txtTargetDate.Text, "MM/dd/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None);
                objBE.targetDate = Convert.ToDateTime(temp);
                objBE.empcode = Session["EMPCODE"].ToString();
                DateTime opentemp = DateTime.ParseExact(txtTargetDate.Text, "MM/dd/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None);
                objBE.openTarDt = Convert.ToDateTime(opentemp);
                objBE.openJob = ddljob.SelectedValue;
                hdnPreOpenCnt.Value = txtCarryOver.Text;
                objBE.preCnt = Convert.ToInt32(hdnPreOpenCnt.Value);
                string curDate = DateTime.Now.ToString("MM'/'dd'/'yyyy");
                if (txtTargetDate.Text != curDate)
                {
                    objBE.opneCount = 0;
                }
                else
                {
                    objBE.opneCount = Convert.ToInt32(hdnOpenVol.Value);
                }
                objBE.openEmpcode = Session["EMPCODE"].ToString();
                ds = objBLL.FetchInventoryDetails(objBE);
                if (ds.Tables[0].Rows[0][0].ToString() == "0")
                {
                    ScriptManager.RegisterStartupScript(this, GetType(), "check", "alert('The JOB is already mapped. Please check');", true);
                }
                else
                {
                    ScriptManager.RegisterStartupScript(this, GetType(), "saved", "alert('Saved Successfully');", true);
                    clearControls();
                }

            }

fetchinventorydetails代码:

public DataSet FetchAssignmentDetails(BE objBE)
    {
        DataSet ds = new DataSet();
        SqlParameter[] paramValue = new SqlParameter[]
        {
        new SqlParameter("@mode",objBE.mode),
        new SqlParameter("@job",objBE.job),
        new SqlParameter("@qaname",objBE.qcName),
        new SqlParameter("@targetdt",objBE.targetDate),
        new SqlParameter("@targetvalue",objBE.targetcount),
        new SqlParameter("@codername",objBE.coderName),
        new SqlParameter("@ProjID",objBE.ProjectId),
        new SqlParameter("@Empcode",objBE.empcode),
        new SqlParameter("@id",objBE.id),
        new SqlParameter("@startdt",objBE.startDate),
        new SqlParameter("@enddt",objBE.endDate)
        };
        SQLHelper.ExecuteDataSet(SQLHelper.CONN_STRING_NON_DTC, CommandType.StoredProcedure, "sp__Assignment", ds, "Assignment", paramValue);
        return ds;
    }

请帮帮我。

1 个答案:

答案 0 :(得分:1)

您的数据集中没有从数据库中返回数据。

总是很好地在访问数据集之前检查表存在的数据集,以避免空引用的异常。此外,您需要在访问它之前检查表的行计数,以避免在位置0错误时没有行。

在访问表之前进行检查,如下所示,

ds = objBLL.FetchInventoryDetails(objBE);
if(ds.Tables.Count > 0)
{                

if(ds.Tables[0].Rows.Count > 0)
{
if (ds.Tables[0].Rows[0][0].ToString() == "0")
            {
                ScriptManager.RegisterStartupScript(this, GetType(),      "check", "alert('The JOB is already mapped. Please check');", true);
            }
            else
            {
                ScriptManager.RegisterStartupScript(this, GetType(),  "saved", "alert('Saved Successfully');", true);
                clearControls();
            }
}
}