Entity Framework, SaveChanges() doesnt work for specific entity - asp.net

Here is my code:
static AogStatus status;
private void loadData()//Called if Page.IsPostback=false
{
status = (from sta in dbContext.AogStatus
where sta.AOGStatus_Id == i_status_id
select sta).Single();
}
protected void UpdateButton_Click(object sender, EventArgs e)
{
status.Aircraft_Id = Int32.Parse(acReg.SelectedValue);
status.Airport_Id = Int32.Parse(Station.SelectedValue);
status.CreationDate = DateTime.Now;
dbContext.SaveChanges();
}
When I click the Update button, it doesn't save the changes. It does not throw an exception. I'm using EF 4.3.1.. I couldn't figure out what is the problem.. Any ideas?? Thanks...

Related

Access variables after initialization in page load ASP.NET Webforms

I have this webform:
public class web1{
private string target = string.Empty;
protected void Page_Load(object sender, EventsArgs e){
target = "something";
}
protected void btnSubmit_Click(object sender, EventArgs e){
//use target variable here
}
}
When I click the button which triggers btnSubmit_Click() the target variable gets reset to string.Empty because of private string target = string.Empty.
Currently I'm assigning the new value to a Session and clearing it after the button click, but was wondering if there was a way of avoiding Session.
This typcial would work:
private string target = string.Empty;
protected void Page_Load(object sender, EventsArgs e){
if (!IsPostBack)
{
target = "something";
ViewState["target"] = target;
}
else
target = (string)ViewState["target"];
}
Now any button click, event code, or whatever is free to use target.

Pass values to a button from function

I have a function which is receiving parameters from another function and I have to pass these parameters to a button when it's execution get's complete !
async void CallCompanyApi(String CompanyGp, string CompanyId)
{
First.IsVisible = false;
Second.IsVisible = true;
// Next_Clicked(CompanyGp,CompanyId)
}
private void Next_Clicked(object sender, EventArgs e)
{
}
I have to pass these 2 parameters to that button !
you need to create class level variables to store these values
string _CompanyGp;
string _CompanyId;
async void CallCompanyApi(String CompanyGp, string CompanyId)
{
First.IsVisible = false;
Second.IsVisible = true;
// store the parameters in the class level variables
_CompanyGp = CompanyGp;
_CompanyId = CompanyId;
}
private void Next_Clicked(object sender, EventArgs e)
{
// you can now just reference _CompanyGp and _CompanyId
}
FYI, this is basic C# and really has nothing specific to do with Xamarin
Welcome to SO!
If CallCompanyApi and Next_Clicked located in different class, you can store them in Xamarin Forms by using Preferences.
using Xamarin.Essentials;
async void CallCompanyApi(String CompanyGp, string CompanyId)
{
First.IsVisible = false;
Second.IsVisible = true;
//Next_Clicked(CompanyGp,CompanyId)
Preferences.Set("CompanyGp", CompanyGp);
Preferences.Set("CompanyId", CompanyId);
}
Then in another class, click button will get them:
private void Next_Clicked(object sender, EventArgs e)
{
var CompanyGp = Preferences.Get("CompanyGp", "default_value");
var CompanyId = Preferences.Get("CompanyId", "default_value");
}
Else if they are in the same class, you can use the way as Jason's said.
============================Update=========================
You can set a default value if needed, such as follow:
var CompanyGp = Preferences.Get("CompanyGp", "Company_A");
var CompanyId = Preferences.Get("CompanyId", "1");
Defalut CompanyGp is Company_A, and default CompanyId is 1.

unable to persist data on postback in dotnetnuke7

I have my website running on dotnetnuke 7.4, i have a checklistbox which i bind on the page load, and after selecting items from it, user clicks on the submit button, the selected items should save in database, however when i click on the submit button, checklistbox gets blank, i tried to enable ViewState at :
Web.config level
Page Level
Control Level
But all in vain, it still unbinds checklistbox because of which everything disappears, i tried the same in plain .net and it works like a charm.
Is there any specific settings in dotnetnuke to support viewstate, or is there any other better option to achieve this.
Here's my code:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Entities objEntities = new Entities();
List<Entities> obj = objEntities.GetList(2);
chkBox.DataSource = obj;
chkBox.DataTextField = "Name";
chkBox.DataValueField = "ID";
chkBox.DataBind();
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
foreach (ListItem item in chkBox.Items)
Response.Write(item.Text + "<br />");
}
There's the issue. Remove that (!IsPostBack) check in your page_load event. Have your code to be like below. Else, only at first page load you are binding the datasource to control which gets lost in postback.
protected void Page_Load(object sender, EventArgs e)
{
Entities objEntities = new Entities();
List<Entities> obj = objEntities.GetList(2);
chkBox.DataSource = obj;
chkBox.DataTextField = "Name";
chkBox.DataValueField = "ID";
chkBox.DataBind();
}
OR, to be more efficient; refactor your code to a method like below and store the data object in Session variable like
private void GetDataSource()
{
List<Entities> obj = null;
if(Session["data"] != null)
{
obj = Session["data"] as List<Entities>;
}
else
{
Entities objEntities = new Entities();
obj = objEntities.GetList(2);
}
chkBox.DataSource = obj;
chkBox.DataTextField = "Name";
chkBox.DataValueField = "ID";
chkBox.DataBind();
Session["data"] = obj;
}
Call the method in your Page_Load event like
protected void Page_Load(object sender, EventArgs e)
{
GetDataSource();
}

ASP.Net Fill GridView with Entity Framework Code First

This is my page
Profiles pp;
Tasks t;
protected void Page_Load(object sender, EventArgs e)
{
if (Session["User"] != null)
{
pp = (Profiles)Session["User"];
}
else
{
Response.Redirect("PageLogin.aspx");
}
}
private void data()
{
DBCOntext db= new DBCOntext ();
var manager= db.Tasks.Where(p => p.Personnel == pp.Username).Select(q => q.Manager).FirstOrDefault();
}
I took the data with data class. But I didn't fill gridview. How do I fill gridview? pls help.
Where are you data binding the gridview? You aren't using the manger variable anywhere that I can see.
GridView.DataSource = manager;
GridView.DataBind();

Selenium-RC loading empty frame

As stated above, I am running out an automated test on a website.
I am use selenium RC to do that but I'm just not sure why I am unable to open the website (actually i did open it), but its content is not showing.
There are just a few empty frame boxes.
This originally had too much code so I'm adding some more.
Anyone know why? Thank you.
Here is my code (unrelated code removed):
private ISelenium selenium;
private StringBuilder verificationErrors;
private Process worKer = new Process();
private string
serverHost = "localhost",
browserString = #"*iexploreproxy",
startUpURL = "";
private int
portNumber = 4444;
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedItem.ToString() == "CNY")
{
startUpURL = "http://malaysia.yahoo.com/";
}
}
private void btnStartServer_Click(object sender, EventArgs e)
{
worKer.StartInfo.FileName = #"C:\LjT\SeleniumServer.bat";
worKer.Start();
}
private void WakeUpSElenium()
{
selenium = new DefaultSelenium(serverHost, portNumber, browserString, startUpURL);
selenium.Start();
verificationErrors = new StringBuilder();
}
private void ToDoList()
{
selenium.Open("/");
//selenium.SelectFrame("iframe_content");
selenium.Type("id=txtFirstName", "1");
selenium.Click("id=rbtnGender_0");
}
private void btnTest_Click(object sender, EventArgs e)
{
try
{
WakeUpSElenium();
ToDoList();
}
catch
{}
}
You are not navigating anywhere, i.e this code here, will not navigate to any page at all:
selenium.Open("/");
I assume you meant to make it this:
selenium.Open(startUpURL); // this is the value from the combobox.

Resources