2009-07-08 97 views
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上的结果/数据,没有错误。这不是刷新/数据绑定的页面。如何刷新linq数据源?

+2

存储过程是否应该返回一些东西? – 2009-07-08 16:59:44

+0

我同意w/Dreas:定义“不工作”。 – Eric 2009-07-08 17:00:16

+0

你确定该函数正在执行吗?在某处放置一个断点。 – 2009-07-08 17:00:33

回答

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没有被你期望的数据刷新。

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返回的东西。


如果你只是想从数据库中的数据以更新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   
} 
+0

你是对的我想从sp返回一些东西。从Form(.net)更新后,我更新的数据仍然不显示。但是当我回到Form(.net)时,它显示更新的数据。它不刷新窗体(.net)。帮助〜 – Yves 2009-07-08 17:19:46

0

您打电话给ExecuteNonQuery应该用于更新数据库插入,删除,更新。

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

+0

我试过ExecuteReader ...从Form(.net)更新后更新的数据仍然没有显示。但是当我回到Form(.net)时,它显示更新的数据。它不刷新窗体(.net)。帮助〜 – Yves 2009-07-08 17:24:44