0
protected void DetailsView1_ItemInserted(object sender, EventArgs e)
{
    using (SqlCommand cmd2 = new SqlCommand("uspUpdateDisplayHours", cn))
        {
            cn.Open();
            cmd2.CommandType = CommandType.StoredProcedure;
            cmd2.ExecuteNonQuery();
            cn.Close();
        }
        DetailsView1.DataBind();
    }
}

存储过程正在 SQL Server 上运行 - 更新表单上的 column1。但没有在 .net 上显示结果/数据且没有错误。

4

3 回答 3

0

看起来存储过程正在运行,但您没有使用任何会导致它刷新数据源的结果。在运行存储过程以实际数据绑定到之后,您必须从数据库中获取一些东西。

带回数据的简单示例:

protected void DetailsView1_ItemInserted(object sender, EventArgs e)
{
    using (SqlCommand cmd2 = new SqlCommand("uspUpdateDisplayHours", cn))
    using (SqlDataAdapter adapter = new SqlDataAdapter(cmd2))        
        {
            var dataSet = new DataSet();
            cn.Open();
            cmd2.CommandType = CommandType.StoredProcedure;
            adapter.Fill(dataSet);
            var _result = //get to your result some how.
            DetailsView.DataSource = result;
            cn.Close();
        }
        DetailsView1.DataBind();
    }
}

我真的不建议这样做。我只是为了说明为什么我认为您的 DetailsView 没有被您期望的数据刷新。

于 2009-07-08T17:03:46.960 回答
0

我只是在这里猜测,但是这样的事情怎么样:

protected void DetailsView1_ItemInserted(object sender, EventArgs e)
{
    DataSet ds = new DataSet();
    using (SqlCommand cmd2 = new SqlCommand("uspUpdateDisplayHours", cn))
    {
        cmd2.CommandType = CommandType.StoredProcedure;
        new SqlDataAdapter(cmd2).Fill(ds);
    }
    DetailsView1.DataSource = ds;
    DetailsView1.DataBind();
    }
}

在上面的例子中,我假设你想从 sp.s 中返回一些东西。


如果您只想使用数据库中的数据更新 DetailsView(例如在加载表单时填充表单),只需再次运行该方法即可。

void PopulateForm() {
    //get data from database and bind it to the ListView
}

protected void DetailsView1_ItemInserted(object sender, EventArgs e)
{ 
    // <= here run the uspUpdateDisplayHours sp

    PopulateForm(); //run this method method again so that the data is refreshed        
} 
于 2009-07-08T17:09:24.187 回答
0

您正在调用ExecuteNonQuery,它应该用于更新数据库 Insert、Delete、Update。

尝试使用ExecuteReader,它将在 SqlDataReader 对象中返回结果。

于 2009-07-08T17:11:33.713 回答