循环后零位没有行

时间:2014-07-31 22:05:33

标签: c# sql asp.net

我需要以下方面的帮助:

foreach (DataRow dr in dt.Rows)
{
    PrepareDocument(dr);
}

//update table entries to processed state
try
{
    string constring = "........";
    string Query = @"update OnSiteWorkTx set Processed = 1 where VisitNumber = @company and Processed = 0";

    using (SqlConnection conDatabase = new SqlConnection(constring))
    using (SqlCommand cmdDatabase = new SqlCommand(Query, conDatabase))
    {
        cmdDatabase.Parameters.AddWithValue("@company", MyVariable);
        conDatabase.Open();
        cmdDatabase.ExecuteNonQuery();
    }
}
catch (Exception ex)
{
    Response.Write(ex.Message);
}

PrepareDocument内我有以下内容:

private void PrepareDocument(DataRow dr)
{
    string connectWrk = ".............";

    using (SqlConnection connWrk = new SqlConnection(connectWrk))
    {
        string queryWrk = "SELECT Workdone FROM OnSiteWorkTx where VisitNumber='" + MyVarible + "' ";
        SqlCommand cmdWrk = new SqlCommand(queryWrk, connWrk);
        var ds = new DataSet();
        var da = new SqlDataAdapter(cmdWrk);
        da.Fill(ds);

        var Workdone = ds.Tables[0].Rows[0]["Workdone"];

        try
        {
            connWrk.Open();

            tableWrk.AddCell(cellWrk);
            tableWrk.AddCell(Workdone.ToString());
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }
}

将表条目更改为已处理状态的代码在Preparedocument(dr)之前运行,然后语句无法找到已处理的条目现在为True并且在零位置给出错误no row。

1 个答案:

答案 0 :(得分:0)

尝试以下方法:

if (ds.Tables.Count > 0 && ds.Tables.Rows.Count > 0)
{
    var Workdone = ds.Tables[0].Rows[0][0];

    ...