Here is the script. Various users make changes by selecting a value from the drop-down list on the web page. Drop-down content is contained either in a DataView or by building a table. If User A makes changes to line 1, he updates the database and shows their changes after overwriting. Subsequently, user B is on the same page when user A makes their changes and makes changes to row 2. The database is updated and the gridview is restored (or the table is rebuilt). However, user B does not see the changes made by user A. I assume this is due to EF caching. If the user refreshes the page (or redirects back to it), they can see the latest data in the database.
How to get the latest data from a database without refreshing the page?
The method that is bound is called in PageLoad every time, including postback:
private void PopulateFormForDealer(DateTime BeginDate, DateTime EndDate, int DealerID, bool UnVerifiedOnly)
{
try
{
using (var DB = new NIMSModel.NIMSEntities())
{
var scheduledOrders = from r in DB.Reservations
join o in DB.Orders on r.ResID equals o.ReservationID
where r.ResDate <= EndDate && r.ResDate >= BeginDate && r.Claimed == "Y"
&& r.DealerID == DealerID
orderby r.ResDate, r.ResID
select new { r.ResID, o.ID, o.VantiveOrderID, o.CustomerFirstName, o.CustomerCity, o.CustomerState, o.CustomerZipCode, o.OrderType.Type, r.ResDate, r.TimeOfDay, r.Source, DealerInstallerID = r.DealerInstallerID == null ? 0 : r.DealerInstallerID, r.Verified, r.Notes };
GridView1.DataSource = scheduledOrders.ToList();
GridView1.DataBind();
}
}
catch (Exception ex)
{
LogError(ex);
}
}
Here is the event handler for the drop down list:
protected void ddInstaller_SelectedIndexChanged(Object sender, EventArgs e)
{
try
{
string foo = ((DropDownList)sender).SelectedValue;
Guid theg = new Guid(((DropDownList)sender).SelectedValue.Split('_')[1].ToString());
int? installerid = int.Parse(((DropDownList)sender).SelectedValue.Split('_')[0].ToString());
string installername = ((DropDownList)sender).SelectedItem.Text;
if (installerid == 0)
{
installerid = null;
}
int testvalidguidlen = theg.ToString().Replace("0", "").Length;
if (testvalidguidlen > 10)
{
string note;
using (var DB = new NIMSModel.NIMSEntities())
{
var reservatoins = DB.Reservations.Where(r => r.ResID == theg).FirstOrDefault();
if (reservatoins.DealerInstallerID != installerid)
{
var orders = DB.Orders.Where(o => o.ReservationID == theg).FirstOrDefault();
reservatoins.DealerInstallerID = installerid;
orders.InstallerID = installerid;
if (reservatoins.Notes == null || reservatoins.Notes.Length >= 1800)
{
note = "[" + DateTime.Now.ToString("yyyy.MM.dd hh:mm:ss") + "] TechChange by: " + PTNAccount.UserName + "(" + PTNAccount.LoginID + "); NewTech: " + installername + ";";
}
else
{
note = reservatoins.Notes + "[" + DateTime.Now.ToString("yyyy.MM.dd hh:mm:ss") + "] TechChange by: " + PTNAccount.UserName + "(" + PTNAccount.LoginID + "); NewTech: " + installername + ";";
}
reservatoins.Notes = note;
DB.SaveChanges();
}
}
}
}
catch (Exception ex)
{
LogError(ex);
}
}
Here we use a method that creates a drop-down list:
private DropDownList PopulateInstallerDropDownList(DropDownList ddl, String resID)
{
try
{
using (var DB = new NIMSModel.NIMSEntities())
{
var DealerInstallers = from di in DB.DealerInstallers
where di.Active == 1 && di.IsDeleted == "N" && di.DealerID == DealerID
orderby di.Name
select new { di.ID, di.Name };
var DealerInstallersArray = DealerInstallers.ToArray();
ListItem li = new ListItem("","0_" + resID);
ddl.Items.Add(li);
foreach (var installer in DealerInstallersArray)
{
ddl.Items.Add(new ListItem(installer.Name.ToString(), (installer.ID.ToString() + (string)"_" + resID.ToString())));
}
}
}
catch (Exception ex)
{
string foo = ex.Message;
}
return ddl;
}
Here is the RowDataBound event handler:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string reservationID = DataBinder.Eval(e.Row.DataItem, "ResID").ToString();
DropDownList ddl = (DropDownList)e.Row.FindControl("ddInstaller");
PopulateInstallerDropDownList(ddl, reservationID);
string dealerInstallerId = DataBinder.Eval(e.Row.DataItem, "DealerInstallerID").ToString();
if (dealerInstallerId != "0")
{
dealerInstallerId = dealerInstallerId + "_" + reservationID;
}
if (dealerInstallerId.Length > 1)
{
ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByValue(dealerInstallerId));
}
}
}
I spent several searches for a solution in the last two days. Any help is appreciated.