簡體   English   中英

這不是刷新/數據綁定頁面。 如何刷新linq數據源?

[英]It's not refreshing/databinding the page. How to refresh linq datasource?

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上顯示結果/數據,沒有錯誤。

看起來存儲過程正在運行,但是您沒有使用任何導致存儲刷新數據源的結果。 在運行存儲過程以實際進行數據綁定之后,您將不得不從數據庫中獲取一些東西。

一個簡單的將數據帶回的示例:

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沒有被您期望的數據刷新。

我只是在這里猜測,但是這樣的事情呢:

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        
} 

您正在調用ExecuteNonQuery ,它應用於更新數據庫的插入,刪除,更新。

嘗試使用ExecuteReader ,它將在SqlDataReader對象中返回結果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM