简体   繁体   中英

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();
    }
}

The Stored Procedure is working on SQL Server - Updating a column1 that's on the Form. but not showing the results/data on .net with NO Error.

It looks like the stored procedure is running, but you're not using any results from it that would cause it to refresh your DataSource. You would have to get something from the database after you ran the stored procedure to actually databind to.

An simple example of bringing the data back:

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();
    }
}

I really wouldn't recommend doing this. I'm only showing this to illustrate why I think your DetailsView isn't being refreshed with the data you're expecting.

I'm just guessing here, but how about something like this:

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();
    }
}

In the above example, I am assuming that you want to return something from the sp.


If you just want to update the DetailsView with data from the database (like when you populated the form when it loaded), just run that method again.

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        
} 

You are calling ExecuteNonQuery which should be used for updating the database Insert, Delete, Update.

Try using ExecuteReader which will return the results in a SqlDataReader object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM